github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/codecancel/codecancel_test.go (about) 1 package codecancel 2 3 import ( 4 "context" 5 "testing" 6 ) 7 8 func BenchmarkSelect(b *testing.B) { 9 ctx, cancel := context.WithCancel(context.Background()) 10 cancel() 11 12 for i := 0; i < b.N; i++ { 13 select { 14 case <-ctx.Done(): 15 default: 16 } 17 } 18 } 19 20 func BenchmarkErr(b *testing.B) { 21 ctx, cancel := context.WithCancel(context.Background()) 22 cancel() 23 24 for i := 0; i < b.N; i++ { 25 if ctx.Err() != nil { 26 // 27 } 28 } 29 } 30 31 func BenchmarkChan(b *testing.B) { 32 ctx, cancel := context.WithCancel(context.Background()) 33 cancel() 34 35 done := ctx.Done() 36 37 for i := 0; i < b.N; i++ { 38 select { 39 case <-done: 40 default: 41 } 42 } 43 } 44 45 func BenchmarkErrShort(b *testing.B) { 46 ctx, cancel := context.WithCancel(context.Background()) 47 cancel() 48 49 geterr := ctx.Err 50 51 for i := 0; i < b.N; i++ { 52 if geterr() != nil { 53 // 54 } 55 } 56 }