mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00: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:
parent
210a9e0c7d
commit
b0ac2546b1
1 changed files with 24 additions and 0 deletions
|
|
@ -13,6 +13,30 @@ import (
|
||||||
"time"
|
"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) {
|
func BenchmarkWithTimeout(b *testing.B) {
|
||||||
for concurrency := 40; concurrency <= 4e5; concurrency *= 100 {
|
for concurrency := 40; concurrency <= 4e5; concurrency *= 100 {
|
||||||
name := fmt.Sprintf("concurrency=%d", concurrency)
|
name := fmt.Sprintf("concurrency=%d", concurrency)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue