heaptest

package
v0.56.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func AssertFlat(tb testing.TB, small, large Run, f Flat)

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

func SkipUnsupported(tb testing.TB)

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

func (s *FileSampler) Read(ctx context.Context, key string) ([]byte, error)

Read implements backend.Backend.

func (*FileSampler) ReadAt

func (s *FileSampler) ReadAt(ctx context.Context, key string, off, n int64) ([]byte, error)

ReadAt implements backend.ReaderAt.

func (*FileSampler) WriteDeferred

func (s *FileSampler) WriteDeferred(ctx context.Context, key string, data []byte) error

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
	// SourceShare bounds large.Resident below large.Source / SourceShare, independent of the small
	// 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.

type Run

type Run struct {
	Resident, Source uint64
}

Run is one Resident measurement and the source bytes it read.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL