Documentation
¶
Overview ¶
Package heaptest measures heap and allocation in tests and benchmarks.
Every measurement reads process-wide runtime counters, so a test using it must not run in parallel with others.
Index ¶
- func Allocated(fn func()) uint64
- func AssertFlat(tb testing.TB, small, large Run, f Flat)
- func BytesPerOp(fn func()) int64
- func InuseGrowth(fn func()) uint64
- func Live() uint64
- func Peak(run func(sample func())) uint64
- func Resident(tb testing.TB, s *FileSampler, run func()) uint64
- func SkipUnsupported(tb testing.TB)
- type FileSampler
- func (s *FileSampler) CreateObjectDeferred(ctx context.Context, key string) (backend.ObjectWriter, error)
- func (s *FileSampler) Read(ctx context.Context, key string) ([]byte, error)
- func (s *FileSampler) ReadAt(ctx context.Context, key string, off, n int64) ([]byte, error)
- func (s *FileSampler) WriteDeferred(ctx context.Context, key string, data []byte) error
- type Flat
- type Run
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Allocated ¶
func Allocated(fn func()) uint64
Allocated returns the bytes allocated while fn ran. It does not collect first; a caller that needs a settled heap calls runtime.GC before it.
func AssertFlat ¶
AssertFlat asserts that large, with several times small's sources, stays within f.
func BytesPerOp ¶
func BytesPerOp(fn func()) int64
BytesPerOp runs fn as a benchmark and returns its allocated bytes per call. Over a benchmark's iteration count, sync.Pool refills after a collection divide away.
func InuseGrowth ¶
func InuseGrowth(fn func()) uint64
InuseGrowth returns how far HeapInuse moved while fn ran, wrapping if it shrank. It does not collect first; a caller that needs a settled heap calls runtime.GC before it.
func Live ¶
func Live() uint64
Live collects garbage and returns the heap that survived: the resident set at this instant.
func Peak ¶
func Peak(run func(sample func())) uint64
Peak runs run and returns the most the heap held live at any point run marked by calling sample, and once more after run returns, above what it held before. Each sample collects garbage first, so it reads what is reachable at that point; a caller samples where what it measures is still referenced.
func Resident ¶
func Resident(tb testing.TB, s *FileSampler, run func()) uint64
Resident runs run with s armed and returns its peak live heap above what the process held before it. It fails tb if run read no matching key.
func SkipUnsupported ¶ added in v0.49.4
SkipUnsupported temporarily skips heap and allocation guards on macOS and Windows.
Types ¶
type FileSampler ¶
type FileSampler struct {
*file.File
// KeyContains selects the sampled keys; empty samples every key.
KeyContains string
// Writes samples whole-object and streamed writes.
Writes bool
// contains filtered or unexported fields
}
FileSampler is the file backend with a Live sample after every read of a matching key, taken while the bytes just read are still referenced. The file backend is the one that matters: it has no zero-copy view, so every byte a caller reads is a heap copy it owns.
It embeds *file.File rather than wrapping backend.Backend, so every optional capability the file backend has (backend.ReaderAt, backend.Sizer, backend.DeferredSyncer, backend.ObjectCreator, backend.NodeLocal, ...) still forwards, and the code under test takes the path it takes over a bare file backend.
func (*FileSampler) CreateObjectDeferred ¶ added in v0.53.1
func (s *FileSampler) CreateObjectDeferred(ctx context.Context, key string) (backend.ObjectWriter, error)
CreateObjectDeferred samples streamed writes as well as whole-object writes.
func (*FileSampler) Read ¶
Read implements backend.Backend.
func (*FileSampler) ReadAt ¶
ReadAt implements backend.ReaderAt.
func (*FileSampler) WriteDeferred ¶
WriteDeferred implements backend.DeferredSyncer.
type Flat ¶
type Flat struct {
// Floor is what the code holds by design. The baseline is process-wide, so heap another test
// frees during the small run can push it toward zero; growth is never taken against less.
Floor uint64
// MaxGrowth bounds large.Resident / max(small.Resident, Floor).
MaxGrowth float64
// run's baseline: holding the sources whole costs about their size.
SourceShare uint64
}
Flat bounds how a resident set may grow when its sources grow.