Skip to content

Commit e485688

Browse files
committed
src/testing/benchmark.go: reorder functions to make diffing against go easier
On the theory that tinygo is tending towards smaller diffs with go... Also adds placeholder for ReportAllocs(), otherwise zero semantic changes.
1 parent 61be918 commit e485688

File tree

1 file changed

+78
-63
lines changed

1 file changed

+78
-63
lines changed

src/testing/benchmark.go

Lines changed: 78 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,26 @@ type benchTimeFlag struct {
1818
d time.Duration
1919
}
2020

21-
// B is a type passed to Benchmark functions to manage benchmark timing and to
22-
// specify the number of iterations to run.
21+
// InternalBenchmark is an internal type but exported because it is cross-package;
22+
// it is part of the implementation of the "go test" command.
23+
type InternalBenchmark struct {
24+
Name string
25+
F func(b *B)
26+
}
27+
28+
// B is a type passed to Benchmark functions to manage benchmark
29+
// timing and to specify the number of iterations to run.
30+
//
31+
// A benchmark ends when its Benchmark function returns or calls any of the methods
32+
// FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods must be called
33+
// only from the goroutine running the Benchmark function.
34+
// The other reporting methods, such as the variations of Log and Error,
35+
// may be called simultaneously from multiple goroutines.
36+
//
37+
// Like in tests, benchmark logs are accumulated during execution
38+
// and dumped to standard output when done. Unlike in tests, benchmark logs
39+
// are always printed, so as not to hide output whose existence may be
40+
// affecting benchmark results.
2341
type B struct {
2442
common
2543
hasSub bool // TODO: should be in common, and atomic
@@ -32,49 +50,6 @@ type B struct {
3250
result BenchmarkResult
3351
}
3452

35-
// InternalBenchmark is an internal type but exported because it is cross-package;
36-
// it is part of the implementation of the "go test" command.
37-
type InternalBenchmark struct {
38-
Name string
39-
F func(b *B)
40-
}
41-
42-
// BenchmarkResult contains the results of a benchmark run.
43-
type BenchmarkResult struct {
44-
N int // The number of iterations.
45-
T time.Duration // The total time taken.
46-
}
47-
48-
// NsPerOp returns the "ns/op" metric.
49-
func (r BenchmarkResult) NsPerOp() int64 {
50-
if r.N <= 0 {
51-
return 0
52-
}
53-
return r.T.Nanoseconds() / int64(r.N)
54-
}
55-
56-
// AllocsPerOp returns the "allocs/op" metric,
57-
// which is calculated as r.MemAllocs / r.N.
58-
func (r BenchmarkResult) AllocsPerOp() int64 {
59-
return 0 // Dummy version to allow running e.g. golang.org/test/fibo.go
60-
}
61-
62-
// AllocedBytesPerOp returns the "B/op" metric,
63-
// which is calculated as r.MemBytes / r.N.
64-
func (r BenchmarkResult) AllocedBytesPerOp() int64 {
65-
return 0 // Dummy version to allow running e.g. golang.org/test/fibo.go
66-
}
67-
68-
func (b *B) SetBytes(n int64) {
69-
panic("testing: unimplemented: B.SetBytes")
70-
}
71-
72-
// ReportAllocs enables malloc statistics for this benchmark.
73-
func (b *B) ReportAllocs() {
74-
// Dummy version to allow building e.g. golang.org/crypto/...
75-
panic("testing: unimplemented: B.ReportAllocs")
76-
}
77-
7853
// StartTimer starts timing a test. This function is called automatically
7954
// before a benchmark starts, but it can also be used to resume timing after
8055
// a call to StopTimer.
@@ -95,15 +70,28 @@ func (b *B) StopTimer() {
9570
}
9671
}
9772

98-
// ResetTimer zeroes the elapsed benchmark time.
99-
// It does not affect whether the timer is running.
73+
// ResetTimer zeroes the elapsed benchmark time and memory allocation counters
74+
// and deletes user-reported metrics.
10075
func (b *B) ResetTimer() {
10176
if b.timerOn {
10277
b.start = time.Now()
10378
}
10479
b.duration = 0
10580
}
10681

82+
// SetBytes records the number of bytes processed in a single operation.
83+
// If this is called, the benchmark will report ns/op and MB/s.
84+
func (b *B) SetBytes(n int64) {
85+
panic("testing: unimplemented: B.SetBytes")
86+
}
87+
88+
// ReportAllocs enables malloc statistics for this benchmark.
89+
// It is equivalent to setting -test.benchmem, but it only affects the
90+
// benchmark function that calls ReportAllocs.
91+
func (b *B) ReportAllocs() {
92+
panic("testing: unimplemented: B.ReportAllocs")
93+
}
94+
10795
// runN runs a single benchmark for the specified number of iterations.
10896
func (b *B) runN(n int) {
10997
b.N = n
@@ -173,6 +161,32 @@ func (b *B) launch() {
173161
b.result = BenchmarkResult{b.N, b.duration}
174162
}
175163

164+
// BenchmarkResult contains the results of a benchmark run.
165+
type BenchmarkResult struct {
166+
N int // The number of iterations.
167+
T time.Duration // The total time taken.
168+
}
169+
170+
// NsPerOp returns the "ns/op" metric.
171+
func (r BenchmarkResult) NsPerOp() int64 {
172+
if r.N <= 0 {
173+
return 0
174+
}
175+
return r.T.Nanoseconds() / int64(r.N)
176+
}
177+
178+
// AllocsPerOp returns the "allocs/op" metric,
179+
// which is calculated as r.MemAllocs / r.N.
180+
func (r BenchmarkResult) AllocsPerOp() int64 {
181+
return 0 // Dummy version to allow running e.g. golang.org/test/fibo.go
182+
}
183+
184+
// AllocedBytesPerOp returns the "B/op" metric,
185+
// which is calculated as r.MemBytes / r.N.
186+
func (r BenchmarkResult) AllocedBytesPerOp() int64 {
187+
return 0 // Dummy version to allow running e.g. golang.org/test/fibo.go
188+
}
189+
176190
// Run benchmarks f as a subbenchmark with the given name. It reports
177191
// true if the subbenchmark succeeded.
178192
//
@@ -192,22 +206,6 @@ func (b *B) Run(name string, f func(b *B)) bool {
192206
return !sub.failed
193207
}
194208

195-
// Benchmark benchmarks a single function. It is useful for creating
196-
// custom benchmarks that do not use the "go test" command.
197-
//
198-
// If f calls Run, the result will be an estimate of running all its
199-
// subbenchmarks that don't call Run in sequence in a single benchmark.
200-
func Benchmark(f func(b *B)) BenchmarkResult {
201-
b := &B{
202-
benchFunc: f,
203-
benchTime: benchTime,
204-
}
205-
if b.run1() {
206-
b.run()
207-
}
208-
return b.result
209-
}
210-
211209
// add simulates running benchmarks in sequence in a single iteration. It is
212210
// used to give some meaningful results in case func Benchmark is used in
213211
// combination with Run.
@@ -234,3 +232,20 @@ func (pb *PB) Next() bool {
234232
func (b *B) RunParallel(body func(*PB)) {
235233
return
236234
}
235+
236+
// Benchmark benchmarks a single function. It is useful for creating
237+
// custom benchmarks that do not use the "go test" command.
238+
//
239+
// If f calls Run, the result will be an estimate of running all its
240+
// subbenchmarks that don't call Run in sequence in a single benchmark.
241+
func Benchmark(f func(b *B)) BenchmarkResult {
242+
b := &B{
243+
benchFunc: f,
244+
benchTime: benchTime,
245+
}
246+
if b.run1() {
247+
b.run()
248+
}
249+
return b.result
250+
}
251+

0 commit comments

Comments
 (0)