github.com/m3db/m3@v1.5.0/src/dbnode/client/host_queue_bench_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package client 22 23 import ( 24 "runtime" 25 "sync" 26 "testing" 27 ) 28 29 const baseline = 1024 30 31 func BenchmarkEnqueueChannel(b *testing.B) { 32 work := baseline * b.N 33 workerTarget := work / runtime.GOMAXPROCS(0) 34 actual := 0 35 // expected = (((n^2)+n)/2) * workers 36 expected := (((workerTarget * workerTarget) + workerTarget) / 2) * runtime.GOMAXPROCS(0) 37 38 q := make(chan int, work/runtime.GOMAXPROCS(0)) 39 40 flushingL := sync.Mutex{} 41 42 flushQ := func() { 43 flushingL.Lock() 44 for { 45 select { 46 case v := <-q: 47 actual += v 48 default: 49 flushingL.Unlock() 50 return 51 } 52 } 53 } 54 55 var wg sync.WaitGroup 56 57 for w := 0; w < runtime.GOMAXPROCS(0); w++ { 58 wg.Add(1) 59 go func() { 60 for i := 0; i < workerTarget; i++ { 61 select { 62 case q <- i + 1: 63 continue 64 default: 65 go flushQ() 66 q <- i + 1 67 } 68 } 69 wg.Done() 70 }() 71 } 72 73 wg.Wait() 74 75 flushQ() 76 77 if actual != expected { 78 b.Fatalf("failed to compute, actual %d, expected %d", actual, expected) 79 } 80 } 81 82 func BenchmarkEnqueueMutex(b *testing.B) { 83 work := baseline * b.N 84 workerTarget := work / runtime.GOMAXPROCS(0) 85 actual := 0 86 // expected = (((n^2)+n)/2) * workers 87 expected := (((workerTarget * workerTarget) + workerTarget) / 2) * runtime.GOMAXPROCS(0) 88 89 q := make([]int, 0, work/runtime.GOMAXPROCS(0)) 90 qCopy := make([]int, 0, work/runtime.GOMAXPROCS(0)) 91 l := sync.RWMutex{} 92 flushingL := sync.Mutex{} 93 94 flushQ := func() { 95 qCopyLen := len(qCopy) 96 for i := 0; i < qCopyLen; i++ { 97 actual += qCopy[i] 98 } 99 qCopy = qCopy[:0] 100 } 101 102 var wg sync.WaitGroup 103 104 for w := 0; w < runtime.GOMAXPROCS(0); w++ { 105 wg.Add(1) 106 go func() { 107 for i := 0; i < workerTarget; i++ { 108 l.Lock() 109 q = append(q, i+1) 110 if len(q) == workerTarget { 111 flushingL.Lock() 112 // Swap the copy and start flushing 113 q, qCopy = qCopy, q 114 go func() { 115 flushQ() 116 flushingL.Unlock() 117 }() 118 } 119 l.Unlock() 120 } 121 wg.Done() 122 }() 123 } 124 125 wg.Wait() 126 127 flushingL.Lock() 128 flushQ() 129 flushingL.Unlock() 130 131 if actual != expected { 132 b.Fatalf("failed to compute, actual %d, expected %d", actual, expected) 133 } 134 }