syncmap_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package primitives_test
  18. import (
  19. "sync"
  20. "sync/atomic"
  21. "testing"
  22. )
  23. type incrementUint64Map interface {
  24. increment(string)
  25. result(string) uint64
  26. }
  27. type mapWithLock struct {
  28. mu sync.Mutex
  29. m map[string]uint64
  30. }
  31. func newMapWithLock() incrementUint64Map {
  32. return &mapWithLock{
  33. m: make(map[string]uint64),
  34. }
  35. }
  36. func (mwl *mapWithLock) increment(c string) {
  37. mwl.mu.Lock()
  38. mwl.m[c]++
  39. mwl.mu.Unlock()
  40. }
  41. func (mwl *mapWithLock) result(c string) uint64 {
  42. return mwl.m[c]
  43. }
  44. type mapWithAtomicFastpath struct {
  45. mu sync.RWMutex
  46. m map[string]*uint64
  47. }
  48. func newMapWithAtomicFastpath() incrementUint64Map {
  49. return &mapWithAtomicFastpath{
  50. m: make(map[string]*uint64),
  51. }
  52. }
  53. func (mwaf *mapWithAtomicFastpath) increment(c string) {
  54. mwaf.mu.RLock()
  55. if p, ok := mwaf.m[c]; ok {
  56. atomic.AddUint64(p, 1)
  57. mwaf.mu.RUnlock()
  58. return
  59. }
  60. mwaf.mu.RUnlock()
  61. mwaf.mu.Lock()
  62. if p, ok := mwaf.m[c]; ok {
  63. atomic.AddUint64(p, 1)
  64. mwaf.mu.Unlock()
  65. return
  66. }
  67. var temp uint64 = 1
  68. mwaf.m[c] = &temp
  69. mwaf.mu.Unlock()
  70. }
  71. func (mwaf *mapWithAtomicFastpath) result(c string) uint64 {
  72. return atomic.LoadUint64(mwaf.m[c])
  73. }
  74. type mapWithSyncMap struct {
  75. m sync.Map
  76. }
  77. func newMapWithSyncMap() incrementUint64Map {
  78. return &mapWithSyncMap{}
  79. }
  80. func (mwsm *mapWithSyncMap) increment(c string) {
  81. p, ok := mwsm.m.Load(c)
  82. if !ok {
  83. tp := new(uint64)
  84. p, _ = mwsm.m.LoadOrStore(c, tp)
  85. }
  86. atomic.AddUint64(p.(*uint64), 1)
  87. }
  88. func (mwsm *mapWithSyncMap) result(c string) uint64 {
  89. p, _ := mwsm.m.Load(c)
  90. return atomic.LoadUint64(p.(*uint64))
  91. }
  92. func benchmarkIncrementUint64Map(b *testing.B, f func() incrementUint64Map) {
  93. const cat = "cat"
  94. benches := []struct {
  95. name string
  96. goroutineCount int
  97. }{
  98. {
  99. name: " 1",
  100. goroutineCount: 1,
  101. },
  102. {
  103. name: " 10",
  104. goroutineCount: 10,
  105. },
  106. {
  107. name: " 100",
  108. goroutineCount: 100,
  109. },
  110. {
  111. name: "1000",
  112. goroutineCount: 1000,
  113. },
  114. }
  115. for _, bb := range benches {
  116. b.Run(bb.name, func(b *testing.B) {
  117. m := f()
  118. var wg sync.WaitGroup
  119. wg.Add(bb.goroutineCount)
  120. b.ResetTimer()
  121. for i := 0; i < bb.goroutineCount; i++ {
  122. go func() {
  123. for j := 0; j < b.N; j++ {
  124. m.increment(cat)
  125. }
  126. wg.Done()
  127. }()
  128. }
  129. wg.Wait()
  130. b.StopTimer()
  131. if m.result(cat) != uint64(bb.goroutineCount*b.N) {
  132. b.Fatalf("result is %d, want %d", m.result(cat), b.N)
  133. }
  134. })
  135. }
  136. }
  137. func BenchmarkMapWithSyncMutexContetion(b *testing.B) {
  138. benchmarkIncrementUint64Map(b, newMapWithLock)
  139. }
  140. func BenchmarkMapWithAtomicFastpath(b *testing.B) {
  141. benchmarkIncrementUint64Map(b, newMapWithAtomicFastpath)
  142. }
  143. func BenchmarkMapWithSyncMap(b *testing.B) {
  144. benchmarkIncrementUint64Map(b, newMapWithSyncMap)
  145. }