Skip to content

Commit

Permalink
add parallel benchmark
Browse files Browse the repository at this point in the history
When many UUIDs are being generated concurrently,
contention on the atomic counter can slow things down.

There might be ways to speed this up, but for now, just
add a parallel benchmark so we can measure the baseline.

Initial results with `go test -bench  . -cpu 1,2` on my machine (two physical cores):

	BenchmarkContended     	56413502	        18.3 ns/op
	BenchmarkContended-2   	82533951	        33.5 ns/op

Note that the 33.5ns/op measure is worse than it appears, because
that's wall ns/op, not cpu-ns/op (see golang/go#31884), so the time is actually 67.0ns/op when contended.
  • Loading branch information
rogpeppe committed May 9, 2019
1 parent d61b6ae commit 0752ded
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ func BenchmarkNext(b *testing.B) {
g.Next()
}
}

func BenchmarkContended(b *testing.B) {
g := MustNewGenerator()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
g.Next()
}
})
}

0 comments on commit 0752ded

Please sign in to comment.