Skip to content

Commit b04064c

Browse files
rscpull[bot]
authored andcommitted
time: clean up benchmarks
Comparing BenchmarkStop against very old commits like CL 13094043, I was very confused about how timers had gotten almost 10X slower since 2013. It turns out that CL 68060043 introduced a factor of 1000 in the benchmark cost, by counting batches of 1000 as 1 op instead of 1000 ops, and timers have actually gotten dramatically faster since 2013, with the addition of per-P timer heaps and other optimizations. This CL rewrites the benchmarks to use testing.PB directly, so that the factor of 1000 disappears, and "/op" really means "/op". In the few tests that need to run in batches for one reason or another, add "1000" to the name to make clear that batches are being run. Change-Id: I27ed74d1e420934982e4205aad4f218cdfc42509 Reviewed-on: https://go-review.googlesource.com/c/go/+/570495 Auto-Submit: Russ Cox <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 4a7960d commit b04064c

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

src/time/sleep_test.go

+61-55
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ func TestAfterFuncStarvation(t *testing.T) {
148148
wg.Wait()
149149
}
150150

151-
func benchmark(b *testing.B, bench func(n int)) {
152-
151+
func benchmark(b *testing.B, bench func(*testing.PB)) {
153152
// Create equal number of garbage timers on each P before starting
154153
// the benchmark.
155154
var wg sync.WaitGroup
@@ -168,11 +167,7 @@ func benchmark(b *testing.B, bench func(n int)) {
168167
wg.Wait()
169168

170169
b.ResetTimer()
171-
b.RunParallel(func(pb *testing.PB) {
172-
for pb.Next() {
173-
bench(1000)
174-
}
175-
})
170+
b.RunParallel(bench)
176171
b.StopTimer()
177172

178173
for _, garbage := range garbageAll {
@@ -182,105 +177,116 @@ func benchmark(b *testing.B, bench func(n int)) {
182177
}
183178
}
184179

185-
func BenchmarkAfterFunc(b *testing.B) {
186-
benchmark(b, func(n int) {
187-
c := make(chan bool)
188-
var f func()
189-
f = func() {
190-
n--
191-
if n >= 0 {
192-
AfterFunc(0, f)
193-
} else {
194-
c <- true
180+
func BenchmarkAfterFunc1000(b *testing.B) {
181+
benchmark(b, func(pb *testing.PB) {
182+
for pb.Next() {
183+
n := 1000
184+
c := make(chan bool)
185+
var f func()
186+
f = func() {
187+
n--
188+
if n >= 0 {
189+
AfterFunc(0, f)
190+
} else {
191+
c <- true
192+
}
195193
}
194+
AfterFunc(0, f)
195+
<-c
196196
}
197-
198-
AfterFunc(0, f)
199-
<-c
200197
})
201198
}
202199

203200
func BenchmarkAfter(b *testing.B) {
204-
benchmark(b, func(n int) {
205-
for i := 0; i < n; i++ {
201+
benchmark(b, func(pb *testing.PB) {
202+
for pb.Next() {
206203
<-After(1)
207204
}
208205
})
209206
}
210207

211208
func BenchmarkStop(b *testing.B) {
212209
b.Run("impl=chan", func(b *testing.B) {
213-
benchmark(b, func(n int) {
214-
for i := 0; i < n; i++ {
210+
benchmark(b, func(pb *testing.PB) {
211+
for pb.Next() {
215212
NewTimer(1 * Second).Stop()
216213
}
217214
})
218215
})
219216
b.Run("impl=func", func(b *testing.B) {
220-
benchmark(b, func(n int) {
221-
for i := 0; i < n; i++ {
217+
benchmark(b, func(pb *testing.PB) {
218+
for pb.Next() {
222219
newTimerFunc(1 * Second).Stop()
223220
}
224221
})
225222
})
226223
}
227224

228-
func BenchmarkSimultaneousAfterFunc(b *testing.B) {
229-
benchmark(b, func(n int) {
230-
var wg sync.WaitGroup
231-
wg.Add(n)
232-
for i := 0; i < n; i++ {
233-
AfterFunc(0, wg.Done)
225+
func BenchmarkSimultaneousAfterFunc1000(b *testing.B) {
226+
benchmark(b, func(pb *testing.PB) {
227+
for pb.Next() {
228+
n := 1000
229+
var wg sync.WaitGroup
230+
wg.Add(n)
231+
for range n {
232+
AfterFunc(0, wg.Done)
233+
}
234+
wg.Wait()
234235
}
235-
wg.Wait()
236236
})
237237
}
238238

239-
func BenchmarkStartStop(b *testing.B) {
240-
benchmark(b, func(n int) {
241-
timers := make([]*Timer, n)
242-
for i := 0; i < n; i++ {
243-
timers[i] = AfterFunc(Hour, nil)
244-
}
239+
func BenchmarkStartStop1000(b *testing.B) {
240+
benchmark(b, func(pb *testing.PB) {
241+
for pb.Next() {
242+
const N = 1000
243+
timers := make([]*Timer, N)
244+
for i := range timers {
245+
timers[i] = AfterFunc(Hour, nil)
246+
}
245247

246-
for i := 0; i < n; i++ {
247-
timers[i].Stop()
248+
for i := range timers {
249+
timers[i].Stop()
250+
}
248251
}
249252
})
250253
}
251254

252255
func BenchmarkReset(b *testing.B) {
253256
b.Run("impl=chan", func(b *testing.B) {
254-
benchmark(b, func(n int) {
257+
benchmark(b, func(pb *testing.PB) {
255258
t := NewTimer(Hour)
256-
for i := 0; i < n; i++ {
259+
for pb.Next() {
257260
t.Reset(Hour)
258261
}
259262
t.Stop()
260263
})
261264
})
262265
b.Run("impl=func", func(b *testing.B) {
263-
benchmark(b, func(n int) {
266+
benchmark(b, func(pb *testing.PB) {
264267
t := newTimerFunc(Hour)
265-
for i := 0; i < n; i++ {
268+
for pb.Next() {
266269
t.Reset(Hour)
267270
}
268271
t.Stop()
269272
})
270273
})
271274
}
272275

273-
func BenchmarkSleep(b *testing.B) {
274-
benchmark(b, func(n int) {
275-
var wg sync.WaitGroup
276-
wg.Add(n)
277-
for i := 0; i < n; i++ {
278-
go func() {
279-
Sleep(Nanosecond)
280-
wg.Done()
281-
}()
276+
func BenchmarkSleep1000(b *testing.B) {
277+
benchmark(b, func(pb *testing.PB) {
278+
for pb.Next() {
279+
const N = 1000
280+
var wg sync.WaitGroup
281+
wg.Add(N)
282+
for range N {
283+
go func() {
284+
Sleep(Nanosecond)
285+
wg.Done()
286+
}()
287+
}
288+
wg.Wait()
282289
}
283-
wg.Wait()
284290
})
285291
}
286292

src/time/tick_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -227,29 +227,29 @@ func TestLongAdjustTimers(t *testing.T) {
227227
}
228228
}
229229
func BenchmarkTicker(b *testing.B) {
230-
benchmark(b, func(n int) {
230+
benchmark(b, func(pb *testing.PB) {
231231
ticker := NewTicker(Nanosecond)
232-
for i := 0; i < n; i++ {
232+
for pb.Next() {
233233
<-ticker.C
234234
}
235235
ticker.Stop()
236236
})
237237
}
238238

239239
func BenchmarkTickerReset(b *testing.B) {
240-
benchmark(b, func(n int) {
240+
benchmark(b, func(pb *testing.PB) {
241241
ticker := NewTicker(Nanosecond)
242-
for i := 0; i < n; i++ {
242+
for pb.Next() {
243243
ticker.Reset(Nanosecond * 2)
244244
}
245245
ticker.Stop()
246246
})
247247
}
248248

249249
func BenchmarkTickerResetNaive(b *testing.B) {
250-
benchmark(b, func(n int) {
250+
benchmark(b, func(pb *testing.PB) {
251251
ticker := NewTicker(Nanosecond)
252-
for i := 0; i < n; i++ {
252+
for pb.Next() {
253253
ticker.Stop()
254254
ticker = NewTicker(Nanosecond * 2)
255255
}

0 commit comments

Comments
 (0)