go.temporal.io/server@v1.23.0/common/metrics/baggage_bench_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 metrics 26 27 import ( 28 "math/rand" 29 "sync" 30 "testing" 31 "time" 32 33 "github.com/golang/mock/gomock" 34 "github.com/stretchr/testify/require" 35 "github.com/stretchr/testify/suite" 36 ) 37 38 type ( 39 // baggageBenchTest Was used to compare the behavior of sync.Map vs map+mutex in the context of usage for 40 // metricsContext. 41 // See testMapBaggage for test logic. 42 // As a summary, using mutex performed considerably faster than sync.Map. 43 // These results are to be reviewed if the usage scenario of metricsContext changes. 44 baggageBenchTest struct { 45 suite.Suite 46 *require.Assertions 47 controller *gomock.Controller 48 } 49 50 testBaggage interface { 51 Add(k string, v int64) 52 Get(k string) int64 53 } 54 55 baggageSyncMap struct { 56 data *sync.Map 57 } 58 59 baggageMutexMap struct { 60 sync.Mutex 61 data map[string]int64 62 } 63 ) 64 65 func TestBaggageBenchSuite(t *testing.T) { 66 s := new(baggageBenchTest) 67 suite.Run(t, s) 68 } 69 70 func (s *baggageBenchTest) SetupTest() { 71 s.Assertions = require.New(s.T()) 72 s.controller = gomock.NewController(s.T()) 73 } 74 75 func (s *baggageBenchTest) TearDownTest() {} 76 77 func (b *baggageSyncMap) Add(k string, v int64) { 78 for done := false; !done; { 79 metricInterface, _ := b.data.LoadAndDelete(k) 80 var newValue = v 81 if metricInterface != nil { 82 newValue += metricInterface.(int64) 83 } 84 _, loaded := b.data.LoadOrStore(k, newValue) 85 done = !loaded 86 } 87 } 88 89 func (b *baggageSyncMap) Get(k string) int64 { 90 metricInterface, _ := b.data.LoadAndDelete(k) 91 if metricInterface == nil { 92 return 0 93 } 94 return metricInterface.(int64) 95 } 96 97 func (b *baggageMutexMap) Add(k string, v int64) { 98 b.Lock() 99 defer b.Unlock() 100 101 value := b.data[k] 102 value += v 103 b.data[k] = value 104 } 105 106 func (b *baggageMutexMap) Get(k string) int64 { 107 b.Lock() 108 defer b.Unlock() 109 return b.data[k] 110 } 111 112 // roughly 1.7s/7.5s for mutex/sync 113 // baggageCount := 1000 114 // threadCount := 20 115 // updatesPerThread := 1000 116 func testMapBaggage(createTestObj func() testBaggage) { 117 baggageCount := 10 118 threadCount := 10 119 updatesPerThread := 10 120 121 keys := []string{"k1", "k2", "k3", "k4", "k5"} 122 start := time.Now() 123 sum := int64(0) 124 for bag := 0; bag < baggageCount; bag++ { 125 testObj := createTestObj() 126 wg := sync.WaitGroup{} 127 wg.Add(threadCount) 128 for th := 0; th < threadCount; th++ { 129 go func(key string) { 130 for upd := 0; upd < updatesPerThread; upd++ { 131 testObj.Add(key, rand.Int63()) 132 } 133 wg.Done() 134 }(keys[th%len(keys)]) 135 } 136 wg.Wait() 137 val := testObj.Get(keys[0]) 138 sum += val 139 } 140 println("sum: ", sum) 141 println("duration: ", time.Since(start)) 142 } 143 144 func (s *baggageBenchTest) TestSyncMapBaggage() { 145 testMapBaggage(func() testBaggage { return &baggageSyncMap{data: &sync.Map{}} }) 146 } 147 148 func (s *baggageBenchTest) TestMutexMapBaggage() { 149 testMapBaggage(func() testBaggage { return &baggageMutexMap{data: make(map[string]int64)} }) 150 }