go.temporal.io/server@v1.23.0/common/tasks/benchmark_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package tasks 26 27 import ( 28 "math/rand" 29 "sync" 30 "testing" 31 32 "go.temporal.io/server/common/backoff" 33 "go.temporal.io/server/common/log" 34 ) 35 36 type ( 37 noopScheduler struct{} 38 noopTask struct { 39 *sync.WaitGroup 40 } 41 ) 42 43 var ( 44 _ Scheduler[*noopTask] = (*noopScheduler)(nil) 45 _ Task = (*noopTask)(nil) 46 ) 47 48 var ( 49 channelKeyToWeight = map[int]int{ 50 0: 5, 51 1: 3, 52 2: 2, 53 3: 1, 54 } 55 ) 56 57 func BenchmarkInterleavedWeightedRoundRobinScheduler_Sequential(b *testing.B) { 58 logger := log.NewTestLogger() 59 60 scheduler := NewInterleavedWeightedRoundRobinScheduler( 61 InterleavedWeightedRoundRobinSchedulerOptions[*noopTask, int]{ 62 TaskChannelKeyFn: func(nt *noopTask) int { return rand.Intn(4) }, 63 ChannelWeightFn: func(key int) int { return channelKeyToWeight[key] }, 64 }, 65 Scheduler[*noopTask](&noopScheduler{}), 66 logger, 67 ) 68 scheduler.Start() 69 defer scheduler.Stop() 70 71 waitGroup := &sync.WaitGroup{} 72 waitGroup.Add(b.N) 73 74 b.ReportAllocs() 75 b.ResetTimer() 76 77 for i := 0; i < b.N; i++ { 78 scheduler.Submit(&noopTask{WaitGroup: waitGroup}) 79 } 80 waitGroup.Wait() 81 } 82 83 func BenchmarkInterleavedWeightedRoundRobinScheduler_Parallel(b *testing.B) { 84 logger := log.NewTestLogger() 85 86 scheduler := NewInterleavedWeightedRoundRobinScheduler( 87 InterleavedWeightedRoundRobinSchedulerOptions[*noopTask, int]{ 88 TaskChannelKeyFn: func(nt *noopTask) int { return rand.Intn(4) }, 89 ChannelWeightFn: func(key int) int { return channelKeyToWeight[key] }, 90 }, 91 Scheduler[*noopTask](&noopScheduler{}), 92 logger, 93 ) 94 scheduler.Start() 95 defer scheduler.Stop() 96 97 waitGroup := &sync.WaitGroup{} 98 waitGroup.Add(b.N) 99 100 b.ReportAllocs() 101 b.ResetTimer() 102 103 b.RunParallel(func(pb *testing.PB) { 104 for pb.Next() { 105 scheduler.Submit(&noopTask{WaitGroup: waitGroup}) 106 } 107 }) 108 waitGroup.Wait() 109 } 110 111 func (n *noopScheduler) Start() {} 112 func (n *noopScheduler) Stop() {} 113 func (n *noopScheduler) Submit(task *noopTask) { task.Ack() } 114 func (n *noopScheduler) TrySubmit(task *noopTask) bool { task.Ack(); return true } 115 116 func (n *noopTask) Execute() error { panic("implement me") } 117 func (n *noopTask) HandleErr(err error) error { panic("implement me") } 118 func (n *noopTask) IsRetryableError(err error) bool { panic("implement me") } 119 func (n *noopTask) RetryPolicy() backoff.RetryPolicy { panic("implement me") } 120 func (n *noopTask) Abort() { panic("implement me") } 121 func (n *noopTask) Cancel() { panic("implement me") } 122 func (n *noopTask) Ack() { n.Done() } 123 func (n *noopTask) Nack(err error) { panic("implement me") } 124 func (n *noopTask) Reschedule() { panic("implement me") } 125 func (n *noopTask) State() State { panic("implement me") }