1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
25 func initBenchmarkFlags() {
26 matchBenchmarks = flag.String("test.bench", "", "run only benchmarks matching `regexp`")
27 benchmarkMemory = flag.Bool("test.benchmem", false, "print memory allocations for benchmarks")
28 flag.Var(&benchTime, "test.benchtime", "run each benchmark for duration `d`")
32 matchBenchmarks *string
35 benchTime = durationOrCountFlag{d: 1 * time.Second} // changed during test of testing package
38 type durationOrCountFlag struct {
44 func (f *durationOrCountFlag) String() string {
46 return fmt.Sprintf("%dx", f.n)
51 func (f *durationOrCountFlag) Set(s string) error {
52 if strings.HasSuffix(s, "x") {
53 n, err := strconv.ParseInt(s[:len(s)-1], 10, 0)
54 if err != nil || n < 0 || (!f.allowZero && n == 0) {
55 return fmt.Errorf("invalid count")
57 *f = durationOrCountFlag{n: int(n)}
60 d, err := time.ParseDuration(s)
61 if err != nil || d < 0 || (!f.allowZero && d == 0) {
62 return fmt.Errorf("invalid duration")
64 *f = durationOrCountFlag{d: d}
68 // Global lock to ensure only one benchmark runs at a time.
69 var benchmarkLock sync.Mutex
71 // Used for every benchmark for measuring memory.
72 var memStats runtime.MemStats
74 // InternalBenchmark is an internal type but exported because it is cross-package;
75 // it is part of the implementation of the "go test" command.
76 type InternalBenchmark struct {
81 // B is a type passed to Benchmark functions to manage benchmark
82 // timing and to specify the number of iterations to run.
84 // A benchmark ends when its Benchmark function returns or calls any of the methods
85 // FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods must be called
86 // only from the goroutine running the Benchmark function.
87 // The other reporting methods, such as the variations of Log and Error,
88 // may be called simultaneously from multiple goroutines.
90 // Like in tests, benchmark logs are accumulated during execution
91 // and dumped to standard output when done. Unlike in tests, benchmark logs
92 // are always printed, so as not to hide output whose existence may be
93 // affecting benchmark results.
96 importPath string // import path of the package containing the benchmark
99 previousN int // number of iterations in the previous run
100 previousDuration time.Duration // total duration of the previous run
102 benchTime durationOrCountFlag
104 missingBytes bool // one of the subbenchmarks does not have bytes set.
107 result BenchmarkResult
108 parallelism int // RunParallel creates parallelism*GOMAXPROCS goroutines
109 // The initial states of memStats.Mallocs and memStats.TotalAlloc.
112 // The net total of this test after being run.
115 // Extra metrics collected by ReportMetric.
116 extra map[string]float64
119 // StartTimer starts timing a test. This function is called automatically
120 // before a benchmark starts, but it can also be used to resume timing after
121 // a call to StopTimer.
122 func (b *B) StartTimer() {
124 runtime.ReadMemStats(&memStats)
125 b.startAllocs = memStats.Mallocs
126 b.startBytes = memStats.TotalAlloc
132 // StopTimer stops timing a test. This can be used to pause the timer
133 // while performing complex initialization that you don't
135 func (b *B) StopTimer() {
137 b.duration += time.Since(b.start)
138 runtime.ReadMemStats(&memStats)
139 b.netAllocs += memStats.Mallocs - b.startAllocs
140 b.netBytes += memStats.TotalAlloc - b.startBytes
145 // ResetTimer zeroes the elapsed benchmark time and memory allocation counters
146 // and deletes user-reported metrics.
147 // It does not affect whether the timer is running.
148 func (b *B) ResetTimer() {
150 // Allocate the extra map before reading memory stats.
151 // Pre-size it to make more allocation unlikely.
152 b.extra = make(map[string]float64, 16)
154 for k := range b.extra {
159 runtime.ReadMemStats(&memStats)
160 b.startAllocs = memStats.Mallocs
161 b.startBytes = memStats.TotalAlloc
169 // SetBytes records the number of bytes processed in a single operation.
170 // If this is called, the benchmark will report ns/op and MB/s.
171 func (b *B) SetBytes(n int64) { b.bytes = n }
173 // ReportAllocs enables malloc statistics for this benchmark.
174 // It is equivalent to setting -test.benchmem, but it only affects the
175 // benchmark function that calls ReportAllocs.
176 func (b *B) ReportAllocs() {
177 b.showAllocResult = true
180 // runN runs a single benchmark for the specified number of iterations.
181 func (b *B) runN(n int) {
183 defer benchmarkLock.Unlock()
184 defer b.runCleanup(normalPanic)
185 // Try to get a comparable environment for each run
186 // by clearing garbage from previous runs.
188 b.raceErrors = -race.Errors()
196 b.previousDuration = b.duration
197 b.raceErrors += race.Errors()
198 if b.raceErrors > 0 {
199 b.Errorf("race detected during execution of benchmark")
203 func min(x, y int64) int64 {
210 func max(x, y int64) int64 {
217 // run1 runs the first iteration of benchFunc. It reports whether more
218 // iterations of this benchmarks should be run.
219 func (b *B) run1() bool {
220 if ctx := b.context; ctx != nil {
221 // Extend maxLen, if needed.
222 if n := len(b.name) + ctx.extLen + 1; n > ctx.maxLen {
223 ctx.maxLen = n + 8 // Add additional slack to avoid too many jumps in size.
227 // Signal that we're done whether we return normally
228 // or by FailNow's runtime.Goexit.
237 fmt.Fprintf(b.w, "%s--- FAIL: %s\n%s", b.chatty.prefix(), b.name, b.output)
240 // Only print the output if we know we are not going to proceed.
241 // Otherwise it is printed in processBench.
243 finished := b.finished
245 if b.hasSub.Load() || finished {
250 if b.chatty != nil && (len(b.output) > 0 || finished) {
252 fmt.Fprintf(b.w, "%s--- %s: %s\n%s", b.chatty.prefix(), tag, b.name, b.output)
259 var labelsOnce sync.Once
261 // run executes the benchmark in a separate goroutine, including all of its
262 // subbenchmarks. b must not have subbenchmarks.
264 labelsOnce.Do(func() {
265 fmt.Fprintf(b.w, "goos: %s\n", runtime.GOOS)
266 fmt.Fprintf(b.w, "goarch: %s\n", runtime.GOARCH)
267 if b.importPath != "" {
268 fmt.Fprintf(b.w, "pkg: %s\n", b.importPath)
270 if cpu := sysinfo.CPU.Name(); cpu != "" {
271 fmt.Fprintf(b.w, "cpu: %s\n", cpu)
274 if b.context != nil {
275 // Running go test --test.bench
276 b.context.processBench(b) // Must call doBench.
278 // Running func Benchmark.
283 func (b *B) doBench() BenchmarkResult {
289 // launch launches the benchmark function. It gradually increases the number
290 // of benchmark iterations until the benchmark runs for the requested benchtime.
291 // launch is run by the doBench function as a separate goroutine.
292 // run1 must have been called on b.
293 func (b *B) launch() {
294 // Signal that we're done whether we return normally
295 // or by FailNow's runtime.Goexit.
300 // Run the benchmark for at least the specified amount of time.
301 if b.benchTime.n > 0 {
302 // We already ran a single iteration in run1.
303 // If -benchtime=1x was requested, use that result.
304 // See https://golang.org/issue/32051.
305 if b.benchTime.n > 1 {
306 b.runN(b.benchTime.n)
310 for n := int64(1); !b.failed && b.duration < d && n < 1e9; {
312 // Predict required iterations.
313 goalns := d.Nanoseconds()
314 prevIters := int64(b.N)
315 prevns := b.duration.Nanoseconds()
317 // Round up, to avoid div by zero.
320 // Order of operations matters.
321 // For very fast benchmarks, prevIters ~= prevns.
322 // If you divide first, you get 0 or 1,
323 // which can hide an order of magnitude in execution time.
324 // So multiply first, then divide.
325 n = goalns * prevIters / prevns
326 // Run more iterations than we think we'll need (1.2x).
328 // Don't grow too fast in case we had timing errors previously.
330 // Be sure to run at least one more than last time.
332 // Don't run more than 1e9 times. (This also keeps n in int range on 32 bit platforms.)
337 b.result = BenchmarkResult{b.N, b.duration, b.bytes, b.netAllocs, b.netBytes, b.extra}
340 // Elapsed returns the measured elapsed time of the benchmark.
341 // The duration reported by Elapsed matches the one measured by
342 // StartTimer, StopTimer, and ResetTimer.
343 func (b *B) Elapsed() time.Duration {
346 d += time.Since(b.start)
351 // ReportMetric adds "n unit" to the reported benchmark results.
352 // If the metric is per-iteration, the caller should divide by b.N,
353 // and by convention units should end in "/op".
354 // ReportMetric overrides any previously reported value for the same unit.
355 // ReportMetric panics if unit is the empty string or if unit contains
357 // If unit is a unit normally reported by the benchmark framework itself
358 // (such as "allocs/op"), ReportMetric will override that metric.
359 // Setting "ns/op" to 0 will suppress that built-in metric.
360 func (b *B) ReportMetric(n float64, unit string) {
362 panic("metric unit must not be empty")
364 if strings.IndexFunc(unit, unicode.IsSpace) >= 0 {
365 panic("metric unit must not contain whitespace")
370 // BenchmarkResult contains the results of a benchmark run.
371 type BenchmarkResult struct {
372 N int // The number of iterations.
373 T time.Duration // The total time taken.
374 Bytes int64 // Bytes processed in one iteration.
375 MemAllocs uint64 // The total number of memory allocations.
376 MemBytes uint64 // The total number of bytes allocated.
378 // Extra records additional metrics reported by ReportMetric.
379 Extra map[string]float64
382 // NsPerOp returns the "ns/op" metric.
383 func (r BenchmarkResult) NsPerOp() int64 {
384 if v, ok := r.Extra["ns/op"]; ok {
390 return r.T.Nanoseconds() / int64(r.N)
393 // mbPerSec returns the "MB/s" metric.
394 func (r BenchmarkResult) mbPerSec() float64 {
395 if v, ok := r.Extra["MB/s"]; ok {
398 if r.Bytes <= 0 || r.T <= 0 || r.N <= 0 {
401 return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds()
404 // AllocsPerOp returns the "allocs/op" metric,
405 // which is calculated as r.MemAllocs / r.N.
406 func (r BenchmarkResult) AllocsPerOp() int64 {
407 if v, ok := r.Extra["allocs/op"]; ok {
413 return int64(r.MemAllocs) / int64(r.N)
416 // AllocedBytesPerOp returns the "B/op" metric,
417 // which is calculated as r.MemBytes / r.N.
418 func (r BenchmarkResult) AllocedBytesPerOp() int64 {
419 if v, ok := r.Extra["B/op"]; ok {
425 return int64(r.MemBytes) / int64(r.N)
428 // String returns a summary of the benchmark results.
429 // It follows the benchmark result line format from
430 // https://golang.org/design/14313-benchmark-format, not including the
432 // Extra metrics override built-in metrics of the same name.
433 // String does not include allocs/op or B/op, since those are reported
435 func (r BenchmarkResult) String() string {
436 buf := new(strings.Builder)
437 fmt.Fprintf(buf, "%8d", r.N)
439 // Get ns/op as a float.
440 ns, ok := r.Extra["ns/op"]
442 ns = float64(r.T.Nanoseconds()) / float64(r.N)
446 prettyPrint(buf, ns, "ns/op")
449 if mbs := r.mbPerSec(); mbs != 0 {
450 fmt.Fprintf(buf, "\t%7.2f MB/s", mbs)
453 // Print extra metrics that aren't represented in the standard
455 var extraKeys []string
456 for k := range r.Extra {
458 case "ns/op", "MB/s", "B/op", "allocs/op":
459 // Built-in metrics reported elsewhere.
462 extraKeys = append(extraKeys, k)
464 sort.Strings(extraKeys)
465 for _, k := range extraKeys {
467 prettyPrint(buf, r.Extra[k], k)
472 func prettyPrint(w io.Writer, x float64, unit string) {
473 // Print all numbers with 10 places before the decimal point
474 // and small numbers with four sig figs. Field widths are
475 // chosen to fit the whole part in 10 places while aligning
476 // the decimal point of all fractional formats.
478 switch y := math.Abs(x); {
479 case y == 0 || y >= 999.95:
491 case y >= 0.00099995:
496 fmt.Fprintf(w, format, x, unit)
499 // MemString returns r.AllocedBytesPerOp and r.AllocsPerOp in the same format as 'go test'.
500 func (r BenchmarkResult) MemString() string {
501 return fmt.Sprintf("%8d B/op\t%8d allocs/op",
502 r.AllocedBytesPerOp(), r.AllocsPerOp())
505 // benchmarkName returns full name of benchmark including procs suffix.
506 func benchmarkName(name string, n int) string {
508 return fmt.Sprintf("%s-%d", name, n)
513 type benchContext struct {
516 maxLen int // The largest recorded benchmark name.
517 extLen int // Maximum extension length.
520 // RunBenchmarks is an internal function but exported because it is cross-package;
521 // it is part of the implementation of the "go test" command.
522 func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) {
523 runBenchmarks("", matchString, benchmarks)
526 func runBenchmarks(importPath string, matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) bool {
527 // If no flag was specified, don't run benchmarks.
528 if len(*matchBenchmarks) == 0 {
531 // Collect matching benchmarks and determine longest name.
533 for _, procs := range cpuList {
534 if procs > maxprocs {
538 ctx := &benchContext{
539 match: newMatcher(matchString, *matchBenchmarks, "-test.bench", *skip),
540 extLen: len(benchmarkName("", maxprocs)),
542 var bs []InternalBenchmark
543 for _, Benchmark := range benchmarks {
544 if _, matched, _ := ctx.match.fullName(nil, Benchmark.Name); matched {
545 bs = append(bs, Benchmark)
546 benchName := benchmarkName(Benchmark.Name, maxprocs)
547 if l := len(benchName) + ctx.extLen + 1; l > ctx.maxLen {
558 importPath: importPath,
559 benchFunc: func(b *B) {
560 for _, Benchmark := range bs {
561 b.Run(Benchmark.Name, Benchmark.F)
564 benchTime: benchTime,
568 main.chatty = newChattyPrinter(main.w)
574 // processBench runs bench b for the configured CPU counts and prints the results.
575 func (ctx *benchContext) processBench(b *B) {
576 for i, procs := range cpuList {
577 for j := uint(0); j < *count; j++ {
578 runtime.GOMAXPROCS(procs)
579 benchName := benchmarkName(b.name, procs)
581 // If it's chatty, we've already printed this information.
583 fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
585 // Recompute the running time for all but the first iteration.
589 signal: make(chan bool),
595 benchFunc: b.benchFunc,
596 benchTime: b.benchTime,
602 // The output could be very long here, but probably isn't.
603 // We print it all, regardless, because we don't want to trim the reason
604 // the benchmark failed.
605 fmt.Fprintf(b.w, "%s--- FAIL: %s\n%s", b.chatty.prefix(), benchName, b.output)
608 results := r.String()
610 fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
612 if *benchmarkMemory || b.showAllocResult {
613 results += "\t" + r.MemString()
615 fmt.Fprintln(b.w, results)
616 // Unlike with tests, we ignore the -chatty flag and always print output for
617 // benchmarks since the output generation time will skew the results.
618 if len(b.output) > 0 {
620 fmt.Fprintf(b.w, "%s--- BENCH: %s\n%s", b.chatty.prefix(), benchName, b.output)
622 if p := runtime.GOMAXPROCS(-1); p != procs {
623 fmt.Fprintf(os.Stderr, "testing: %s left GOMAXPROCS set to %d\n", benchName, p)
625 if b.chatty != nil && b.chatty.json {
626 b.chatty.Updatef("", "=== NAME %s\n", "")
632 // If hideStdoutForTesting is true, Run does not print the benchName.
633 // This avoids a spurious print during 'go test' on package testing itself,
634 // which invokes b.Run in its own tests (see sub_test.go).
635 var hideStdoutForTesting = false
637 // Run benchmarks f as a subbenchmark with the given name. It reports
638 // whether there were any failures.
640 // A subbenchmark is like any other benchmark. A benchmark that calls Run at
641 // least once will not be measured itself and will be called once with N=1.
642 func (b *B) Run(name string, f func(b *B)) bool {
643 // Since b has subbenchmarks, we will no longer run it as a benchmark itself.
644 // Release the lock and acquire it on exit to ensure locks stay paired.
646 benchmarkLock.Unlock()
647 defer benchmarkLock.Lock()
649 benchName, ok, partial := b.name, true, false
650 if b.context != nil {
651 benchName, ok, partial = b.context.match.fullName(&b.common, name)
656 var pc [maxStackLen]uintptr
657 n := runtime.Callers(2, pc[:])
660 signal: make(chan bool),
669 importPath: b.importPath,
671 benchTime: b.benchTime,
675 // Partial name match, like -bench=X/Y matching BenchmarkX.
676 // Only process sub-benchmarks, if any.
677 sub.hasSub.Store(true)
681 labelsOnce.Do(func() {
682 fmt.Printf("goos: %s\n", runtime.GOOS)
683 fmt.Printf("goarch: %s\n", runtime.GOARCH)
684 if b.importPath != "" {
685 fmt.Printf("pkg: %s\n", b.importPath)
687 if cpu := sysinfo.CPU.Name(); cpu != "" {
688 fmt.Printf("cpu: %s\n", cpu)
692 if !hideStdoutForTesting {
694 b.chatty.Updatef(benchName, "=== RUN %s\n", benchName)
696 fmt.Println(benchName)
707 // add simulates running benchmarks in sequence in a single iteration. It is
708 // used to give some meaningful results in case func Benchmark is used in
709 // combination with Run.
710 func (b *B) add(other BenchmarkResult) {
712 // The aggregated BenchmarkResults resemble running all subbenchmarks as
713 // in sequence in a single benchmark.
715 r.T += time.Duration(other.NsPerOp())
716 if other.Bytes == 0 {
717 // Summing Bytes is meaningless in aggregate if not all subbenchmarks
719 b.missingBytes = true
723 r.Bytes += other.Bytes
725 r.MemAllocs += uint64(other.AllocsPerOp())
726 r.MemBytes += uint64(other.AllocedBytesPerOp())
729 // trimOutput shortens the output from a benchmark, which can be very long.
730 func (b *B) trimOutput() {
731 // The output is likely to appear multiple times because the benchmark
732 // is run multiple times, but at least it will be seen. This is not a big deal
733 // because benchmarks rarely print, but just in case, we trim it if it's too long.
734 const maxNewlines = 10
735 for nlCount, j := 0, 0; j < len(b.output); j++ {
736 if b.output[j] == '\n' {
738 if nlCount >= maxNewlines {
739 b.output = append(b.output[:j], "\n\t... [output truncated]\n"...)
746 // A PB is used by RunParallel for running parallel benchmarks.
748 globalN *uint64 // shared between all worker goroutines iteration counter
749 grain uint64 // acquire that many iterations from globalN at once
750 cache uint64 // local cache of acquired iterations
751 bN uint64 // total number of iterations to execute (b.N)
754 // Next reports whether there are more iterations to execute.
755 func (pb *PB) Next() bool {
757 n := atomic.AddUint64(pb.globalN, pb.grain)
760 } else if n < pb.bN+pb.grain {
761 pb.cache = pb.bN + pb.grain - n
770 // RunParallel runs a benchmark in parallel.
771 // It creates multiple goroutines and distributes b.N iterations among them.
772 // The number of goroutines defaults to GOMAXPROCS. To increase parallelism for
773 // non-CPU-bound benchmarks, call SetParallelism before RunParallel.
774 // RunParallel is usually used with the go test -cpu flag.
776 // The body function will be run in each goroutine. It should set up any
777 // goroutine-local state and then iterate until pb.Next returns false.
778 // It should not use the StartTimer, StopTimer, or ResetTimer functions,
779 // because they have global effect. It should also not call Run.
780 func (b *B) RunParallel(body func(*PB)) {
782 return // Nothing to do when probing.
784 // Calculate grain size as number of iterations that take ~100µs.
785 // 100µs is enough to amortize the overhead and provide sufficient
786 // dynamic load balancing.
788 if b.previousN > 0 && b.previousDuration > 0 {
789 grain = 1e5 * uint64(b.previousN) / uint64(b.previousDuration)
794 // We expect the inner loop and function call to take at least 10ns,
795 // so do not do more than 100µs/10ns=1e4 iterations.
801 numProcs := b.parallelism * runtime.GOMAXPROCS(0)
802 var wg sync.WaitGroup
804 for p := 0; p < numProcs; p++ {
816 if n <= uint64(b.N) && !b.Failed() {
817 b.Fatal("RunParallel: body exited without pb.Next() == false")
821 // SetParallelism sets the number of goroutines used by RunParallel to p*GOMAXPROCS.
822 // There is usually no need to call SetParallelism for CPU-bound benchmarks.
823 // If p is less than 1, this call will have no effect.
824 func (b *B) SetParallelism(p int) {
830 // Benchmark benchmarks a single function. It is useful for creating
831 // custom benchmarks that do not use the "go test" command.
833 // If f depends on testing flags, then Init must be used to register
834 // those flags before calling Benchmark and before calling flag.Parse.
836 // If f calls Run, the result will be an estimate of running all its
837 // subbenchmarks that don't call Run in sequence in a single benchmark.
838 func Benchmark(f func(b *B)) BenchmarkResult {
841 signal: make(chan bool),
845 benchTime: benchTime,
853 type discard struct{}
855 func (discard) Write(b []byte) (n int, err error) { return len(b), nil }