]> Cypherpunks repositories - gostls13.git/commitdiff
context: add benchmarks for context cancellation
authorCarl Mastrangelo <notcarl@google.com>
Thu, 15 Mar 2018 21:15:54 +0000 (14:15 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 29 May 2018 21:57:30 +0000 (21:57 +0000)
Change-Id: I539c9226eb7e493b52c50e1e431954567d43bcfb
Reviewed-on: https://go-review.googlesource.com/100847
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/context/benchmark_test.go

index 6dd8510ff4c437a3f3cc1eb7551024c3cf163de5..5d56863050a9adea7be7a724d47861737d13859e 100644 (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)