LEFT | RIGHT |
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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 85 } |
86 b.duration = 0 | 86 b.duration = 0 |
87 b.netAllocs = 0 | 87 b.netAllocs = 0 |
88 b.netBytes = 0 | 88 b.netBytes = 0 |
89 } | 89 } |
90 | 90 |
91 // SetBytes records the number of bytes processed in a single operation. | 91 // SetBytes records the number of bytes processed in a single operation. |
92 // 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. |
93 func (b *B) SetBytes(n int64) { b.bytes = n } | 93 func (b *B) SetBytes(n int64) { b.bytes = n } |
94 | 94 |
95 // ReportAllocs enables malloc statistics for this benchmark, just | 95 // ReportAllocs enables malloc statistics for this benchmark. |
96 // like what you get when you pass -test.benchmem. | 96 // It is equivalent to setting -test.benchmem, but it only affects the |
| 97 // benchmark function that calls ReportAllocs. |
97 func (b *B) ReportAllocs() { | 98 func (b *B) ReportAllocs() { |
98 b.showAllocResult = true | 99 b.showAllocResult = true |
99 } | 100 } |
100 | 101 |
101 func (b *B) nsPerOp() int64 { | 102 func (b *B) nsPerOp() int64 { |
102 if b.N <= 0 { | 103 if b.N <= 0 { |
103 return 0 | 104 return 0 |
104 } | 105 } |
105 return b.duration.Nanoseconds() / int64(b.N) | 106 return b.duration.Nanoseconds() / int64(b.N) |
106 } | 107 } |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // custom benchmarks that do not use the "go test" command. | 344 // custom benchmarks that do not use the "go test" command. |
344 func Benchmark(f func(b *B)) BenchmarkResult { | 345 func Benchmark(f func(b *B)) BenchmarkResult { |
345 b := &B{ | 346 b := &B{ |
346 common: common{ | 347 common: common{ |
347 signal: make(chan interface{}), | 348 signal: make(chan interface{}), |
348 }, | 349 }, |
349 benchmark: InternalBenchmark{"", f}, | 350 benchmark: InternalBenchmark{"", f}, |
350 } | 351 } |
351 return b.run() | 352 return b.run() |
352 } | 353 } |
LEFT | RIGHT |