1
0
mirror of https://github.com/golang/go synced 2024-09-30 06:24:33 -06:00

context: add benchmarks for context cancellation

Change-Id: I539c9226eb7e493b52c50e1e431954567d43bcfb
Reviewed-on: https://go-review.googlesource.com/100847
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Carl Mastrangelo 2018-03-15 14:15:54 -07:00 committed by Brad Fitzpatrick
parent 210a9e0c7d
commit b0ac2546b1

View File

@ -13,6 +13,30 @@ import (
"time"
)
func BenchmarkCommonParentCancel(b *testing.B) {
root := WithValue(Background(), "key", "value")
shared, sharedcancel := WithCancel(root)
defer sharedcancel()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
x := 0
for pb.Next() {
ctx, cancel := WithCancel(shared)
if ctx.Value("key").(string) != "value" {
b.Fatal("should not be reached")
}
for i := 0; i < 100; i++ {
x /= x + 1
}
cancel()
for i := 0; i < 100; i++ {
x /= x + 1
}
}
})
}
func BenchmarkWithTimeout(b *testing.B) {
for concurrency := 40; concurrency <= 4e5; concurrency *= 100 {
name := fmt.Sprintf("concurrency=%d", concurrency)