Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(390)

Side by Side Diff: src/pkg/testing/benchmark.go

Issue 7027046: code review 7027046: testing: introduce (*B).EnableMallocReport() (Closed)
Patch Set: diff -r a55bd9f21507 https://code.google.com/p/go/ Created 11 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pkg/exp/html/token_test.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package testing 5 package testing
6 6
7 import ( 7 import (
8 "flag" 8 "flag"
9 "fmt" 9 "fmt"
10 "os" 10 "os"
(...skipping 16 matching lines...) Expand all
27 // of the "go test" command. 27 // of the "go test" command.
28 type InternalBenchmark struct { 28 type InternalBenchmark struct {
29 Name string 29 Name string
30 F func(b *B) 30 F func(b *B)
31 } 31 }
32 32
33 // B is a type passed to Benchmark functions to manage benchmark 33 // B is a type passed to Benchmark functions to manage benchmark
34 // timing and to specify the number of iterations to run. 34 // timing and to specify the number of iterations to run.
35 type B struct { 35 type B struct {
36 common 36 common
37 » N int 37 » N int
38 » benchmark InternalBenchmark 38 » benchmark InternalBenchmark
39 » bytes int64 39 » bytes int64
40 » timerOn bool 40 » timerOn bool
41 » result BenchmarkResult 41 » showAllocResult bool
42 » result BenchmarkResult
42 // The initial states of memStats.Mallocs and memStats.TotalAlloc. 43 // The initial states of memStats.Mallocs and memStats.TotalAlloc.
43 startAllocs uint64 44 startAllocs uint64
44 startBytes uint64 45 startBytes uint64
45 // The net total of this test after being run. 46 // The net total of this test after being run.
46 netAllocs uint64 47 netAllocs uint64
47 netBytes uint64 48 netBytes uint64
48 } 49 }
49 50
50 // StartTimer starts timing a test. This function is called automatically 51 // StartTimer starts timing a test. This function is called automatically
51 // before a benchmark starts, but it can also used to resume timing after 52 // before a benchmark starts, but it can also used to resume timing after
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 } 85 }
85 b.duration = 0 86 b.duration = 0
86 b.netAllocs = 0 87 b.netAllocs = 0
87 b.netBytes = 0 88 b.netBytes = 0
88 } 89 }
89 90
90 // SetBytes records the number of bytes processed in a single operation. 91 // SetBytes records the number of bytes processed in a single operation.
91 // If this is called, the benchmark will report ns/op and MB/s. 92 // If this is called, the benchmark will report ns/op and MB/s.
92 func (b *B) SetBytes(n int64) { b.bytes = n } 93 func (b *B) SetBytes(n int64) { b.bytes = n }
93 94
95 // ReportAllocs enables malloc statistics for this benchmark.
96 // It is equivalent to setting -test.benchmem, but it only affects the
97 // benchmark function that calls ReportAllocs.
98 func (b *B) ReportAllocs() {
99 b.showAllocResult = true
100 }
101
94 func (b *B) nsPerOp() int64 { 102 func (b *B) nsPerOp() int64 {
95 if b.N <= 0 { 103 if b.N <= 0 {
96 return 0 104 return 0
97 } 105 }
98 return b.duration.Nanoseconds() / int64(b.N) 106 return b.duration.Nanoseconds() / int64(b.N)
99 } 107 }
100 108
101 // runN runs a single benchmark for the specified number of iterations. 109 // runN runs a single benchmark for the specified number of iterations.
102 func (b *B) runN(n int) { 110 func (b *B) runN(n int) {
103 benchmarkLock.Lock() 111 benchmarkLock.Lock()
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 fmt.Printf("%s\t", benchName) 299 fmt.Printf("%s\t", benchName)
292 r := b.run() 300 r := b.run()
293 if b.failed { 301 if b.failed {
294 // The output could be very long here, but proba bly isn't. 302 // The output could be very long here, but proba bly isn't.
295 // We print it all, regardless, because we don't want to trim the reason 303 // We print it all, regardless, because we don't want to trim the reason
296 // the benchmark failed. 304 // the benchmark failed.
297 fmt.Printf("--- FAIL: %s\n%s", benchName, b.outp ut) 305 fmt.Printf("--- FAIL: %s\n%s", benchName, b.outp ut)
298 continue 306 continue
299 } 307 }
300 results := r.String() 308 results := r.String()
301 » » » if *benchmarkMemory { 309 » » » if *benchmarkMemory || b.showAllocResult {
302 results += "\t" + r.MemString() 310 results += "\t" + r.MemString()
303 } 311 }
304 fmt.Println(results) 312 fmt.Println(results)
305 // Unlike with tests, we ignore the -chatty flag and alw ays print output for 313 // Unlike with tests, we ignore the -chatty flag and alw ays print output for
306 // benchmarks since the output generation time will skew the results. 314 // benchmarks since the output generation time will skew the results.
307 if len(b.output) > 0 { 315 if len(b.output) > 0 {
308 b.trimOutput() 316 b.trimOutput()
309 fmt.Printf("--- BENCH: %s\n%s", benchName, b.out put) 317 fmt.Printf("--- BENCH: %s\n%s", benchName, b.out put)
310 } 318 }
311 if p := runtime.GOMAXPROCS(-1); p != procs { 319 if p := runtime.GOMAXPROCS(-1); p != procs {
(...skipping 24 matching lines...) Expand all
336 // custom benchmarks that do not use the "go test" command. 344 // custom benchmarks that do not use the "go test" command.
337 func Benchmark(f func(b *B)) BenchmarkResult { 345 func Benchmark(f func(b *B)) BenchmarkResult {
338 b := &B{ 346 b := &B{
339 common: common{ 347 common: common{
340 signal: make(chan interface{}), 348 signal: make(chan interface{}),
341 }, 349 },
342 benchmark: InternalBenchmark{"", f}, 350 benchmark: InternalBenchmark{"", f},
343 } 351 }
344 return b.run() 352 return b.run()
345 } 353 }
OLDNEW
« no previous file with comments | « src/pkg/exp/html/token_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b