github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/timingwheel/timingwheel_benchmark_test.go (about) 1 package timingwheel 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func genD(i int) time.Duration { 9 return time.Duration(i%10000) * time.Millisecond 10 } 11 12 type Case struct { 13 name string 14 N int // the data size (i.e. number of existing timers) 15 } 16 17 func getTestCases() []Case { 18 cases := []Case{ 19 {"N-1m", 1000000}, 20 {"N-5m", 5000000}, 21 {"N-10m", 10000000}, 22 } 23 24 return cases 25 } 26 27 func BenchmarkTimingWheel_StartStop(b *testing.B) { 28 tw := NewTimingWheel(time.Millisecond, 20) 29 tw.Start() 30 defer tw.Stop() 31 32 cases := getTestCases() 33 for _, c := range cases { 34 b.Run(c.name, func(b *testing.B) { 35 base := make([]*Timer, c.N) 36 for i := 0; i < len(base); i++ { 37 base[i] = tw.AfterFunc(genD(i), func() {}) 38 } 39 b.ResetTimer() 40 41 for i := 0; i < b.N; i++ { 42 tw.AfterFunc(time.Second, func() {}).Stop() 43 } 44 45 b.StopTimer() 46 for i := 0; i < len(base); i++ { 47 base[i].Stop() 48 } 49 }) 50 } 51 52 } 53 54 func BenchmarkStandardTimer_StartStop(b *testing.B) { 55 cases := getTestCases() 56 for _, c := range cases { 57 b.Run(c.name, func(b *testing.B) { 58 base := make([]*time.Timer, c.N) 59 for i := 0; i < len(base); i++ { 60 base[i] = time.AfterFunc(genD(i), func() {}) 61 } 62 b.ResetTimer() 63 64 for i := 0; i < b.N; i++ { 65 time.AfterFunc(time.Second, func() {}).Stop() 66 } 67 68 b.StopTimer() 69 for i := 0; i < len(base); i++ { 70 base[i].Stop() 71 } 72 }) 73 } 74 }