diff options
author | Carl Mastrangelo <notcarl@google.com> | 2018-03-15 14:15:54 -0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-05-29 21:57:30 +0000 |
commit | b0ac2546b1851a4835ad687e649dead7f610f6a9 (patch) | |
tree | 1a16c71897e59860c466b40a42f80cda2093025b /src/context | |
parent | 210a9e0c7dbe9bc16522387e7a0c902d29a5f85c (diff) | |
download | go-b0ac2546b1851a4835ad687e649dead7f610f6a9.tar.gz go-b0ac2546b1851a4835ad687e649dead7f610f6a9.zip |
context: add benchmarks for context cancellation
Change-Id: I539c9226eb7e493b52c50e1e431954567d43bcfb
Reviewed-on: https://go-review.googlesource.com/100847
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/context')
-rw-r--r-- | src/context/benchmark_test.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/context/benchmark_test.go b/src/context/benchmark_test.go index 6dd8510ff4..5d56863050 100644 --- a/src/context/benchmark_test.go +++ b/src/context/benchmark_test.go @@ -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) |