func initBenchmarkFlags() { … } var matchBenchmarks … var benchmarkMemory … var benchTime … type durationOrCountFlag … func (f *durationOrCountFlag) String() string { … } func (f *durationOrCountFlag) Set(s string) error { … } var benchmarkLock … var memStats … type InternalBenchmark … type B … // StartTimer starts timing a test. This function is called automatically // before a benchmark starts, but it can also be used to resume timing after // a call to [B.StopTimer]. func (b *B) StartTimer() { … } // StopTimer stops timing a test. This can be used to pause the timer // while performing complex initialization that you don't // want to measure. func (b *B) StopTimer() { … } // ResetTimer zeroes the elapsed benchmark time and memory allocation counters // and deletes user-reported metrics. // It does not affect whether the timer is running. func (b *B) ResetTimer() { … } // SetBytes records the number of bytes processed in a single operation. // If this is called, the benchmark will report ns/op and MB/s. func (b *B) SetBytes(n int64) { … } // ReportAllocs enables malloc statistics for this benchmark. // It is equivalent to setting -test.benchmem, but it only affects the // benchmark function that calls ReportAllocs. func (b *B) ReportAllocs() { … } // runN runs a single benchmark for the specified number of iterations. func (b *B) runN(n int) { … } // run1 runs the first iteration of benchFunc. It reports whether more // iterations of this benchmarks should be run. func (b *B) run1() bool { … } var labelsOnce … // run executes the benchmark in a separate goroutine, including all of its // subbenchmarks. b must not have subbenchmarks. func (b *B) run() { … } func (b *B) doBench() BenchmarkResult { … } // launch launches the benchmark function. It gradually increases the number // of benchmark iterations until the benchmark runs for the requested benchtime. // launch is run by the doBench function as a separate goroutine. // run1 must have been called on b. func (b *B) launch() { … } // Elapsed returns the measured elapsed time of the benchmark. // The duration reported by Elapsed matches the one measured by // [B.StartTimer], [B.StopTimer], and [B.ResetTimer]. func (b *B) Elapsed() time.Duration { … } // ReportMetric adds "n unit" to the reported benchmark results. // If the metric is per-iteration, the caller should divide by b.N, // and by convention units should end in "/op". // ReportMetric overrides any previously reported value for the same unit. // ReportMetric panics if unit is the empty string or if unit contains // any whitespace. // If unit is a unit normally reported by the benchmark framework itself // (such as "allocs/op"), ReportMetric will override that metric. // Setting "ns/op" to 0 will suppress that built-in metric. func (b *B) ReportMetric(n float64, unit string) { … } // Loop returns true until b.N calls has been made to it. // // A benchmark should either use Loop or contain an explicit loop from 0 to b.N, but not both. // After the benchmark finishes, b.N will contain the total number of calls to op, so the benchmark // may use b.N to compute other average metrics. func (b *B) Loop() bool { … } type BenchmarkResult … // NsPerOp returns the "ns/op" metric. func (r BenchmarkResult) NsPerOp() int64 { … } // mbPerSec returns the "MB/s" metric. func (r BenchmarkResult) mbPerSec() float64 { … } // AllocsPerOp returns the "allocs/op" metric, // which is calculated as r.MemAllocs / r.N. func (r BenchmarkResult) AllocsPerOp() int64 { … } // AllocedBytesPerOp returns the "B/op" metric, // which is calculated as r.MemBytes / r.N. func (r BenchmarkResult) AllocedBytesPerOp() int64 { … } // String returns a summary of the benchmark results. // It follows the benchmark result line format from // https://golang.org/design/14313-benchmark-format, not including the // benchmark name. // Extra metrics override built-in metrics of the same name. // String does not include allocs/op or B/op, since those are reported // by [BenchmarkResult.MemString]. func (r BenchmarkResult) String() string { … } func prettyPrint(w io.Writer, x float64, unit string) { … } // MemString returns r.AllocedBytesPerOp and r.AllocsPerOp in the same format as 'go test'. func (r BenchmarkResult) MemString() string { … } // benchmarkName returns full name of benchmark including procs suffix. func benchmarkName(name string, n int) string { … } type benchState … // RunBenchmarks is an internal function but exported because it is cross-package; // it is part of the implementation of the "go test" command. func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) { … } func runBenchmarks(importPath string, matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) bool { … } // processBench runs bench b for the configured CPU counts and prints the results. func (s *benchState) processBench(b *B) { … } var hideStdoutForTesting … // Run benchmarks f as a subbenchmark with the given name. It reports // whether there were any failures. // // A subbenchmark is like any other benchmark. A benchmark that calls Run at // least once will not be measured itself and will be called once with N=1. func (b *B) Run(name string, f func(b *B)) bool { … } // add simulates running benchmarks in sequence in a single iteration. It is // used to give some meaningful results in case func Benchmark is used in // combination with Run. func (b *B) add(other BenchmarkResult) { … } // trimOutput shortens the output from a benchmark, which can be very long. func (b *B) trimOutput() { … } type PB … // Next reports whether there are more iterations to execute. func (pb *PB) Next() bool { … } // RunParallel runs a benchmark in parallel. // It creates multiple goroutines and distributes b.N iterations among them. // The number of goroutines defaults to GOMAXPROCS. To increase parallelism for // non-CPU-bound benchmarks, call [B.SetParallelism] before RunParallel. // RunParallel is usually used with the go test -cpu flag. // // The body function will be run in each goroutine. It should set up any // goroutine-local state and then iterate until pb.Next returns false. // It should not use the [B.StartTimer], [B.StopTimer], or [B.ResetTimer] functions, // because they have global effect. It should also not call [B.Run]. // // RunParallel reports ns/op values as wall time for the benchmark as a whole, // not the sum of wall time or CPU time over each parallel goroutine. func (b *B) RunParallel(body func(*PB)) { … } // SetParallelism sets the number of goroutines used by [B.RunParallel] to p*GOMAXPROCS. // There is usually no need to call SetParallelism for CPU-bound benchmarks. // If p is less than 1, this call will have no effect. func (b *B) SetParallelism(p int) { … } // Benchmark benchmarks a single function. It is useful for creating // custom benchmarks that do not use the "go test" command. // // If f depends on testing flags, then [Init] must be used to register // those flags before calling Benchmark and before calling [flag.Parse]. // // If f calls Run, the result will be an estimate of running all its // subbenchmarks that don't call Run in sequence in a single benchmark. func Benchmark(f func(b *B)) BenchmarkResult { … } type discard … func (discard) Write(b []byte) (n int, err error) { … }