github.com/m3db/m3@v1.5.0/src/cluster/shard/shard_benchmark_test.go (about) 1 // Copyright (c) 2020 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 shard 22 23 import ( 24 "fmt" 25 "runtime" 26 "testing" 27 ) 28 29 func BenchmarkNewShards(b *testing.B) { 30 for i := 16; i <= 4096; i *= 4 { 31 rndShards := makeTestShards(i) 32 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 33 for i := 0; i < b.N; i++ { 34 res := NewShards(rndShards) 35 runtime.KeepAlive(res) 36 } 37 }) 38 } 39 } 40 41 func BenchmarkShardsAllShards(b *testing.B) { 42 for i := 16; i <= 4096; i *= 4 { 43 rndShards := makeTestShards(i) 44 shards := NewShards(rndShards) 45 46 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 47 var res []Shard 48 for i := 0; i < b.N; i++ { 49 res = shards.All() 50 } 51 runtime.KeepAlive(res) 52 }) 53 } 54 } 55 56 func BenchmarkShardsAllShardIDs(b *testing.B) { 57 for i := 16; i <= 4096; i *= 4 { 58 rndShards := makeTestShards(i) 59 shards := NewShards(rndShards) 60 61 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 62 var res []uint32 63 for i := 0; i < b.N; i++ { 64 res = shards.AllIDs() 65 } 66 runtime.KeepAlive(res) 67 }) 68 } 69 } 70 71 func BenchmarkShardsAdd(b *testing.B) { 72 for i := 16; i <= 4096; i *= 4 { 73 rndShards := makeTestShards(i) 74 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 75 for i := 0; i < b.N; i++ { 76 res := NewShards(nil) 77 for j := 0; j < len(rndShards); j++ { 78 res.Add(rndShards[j]) 79 } 80 if res.NumShards() != len(rndShards) { 81 b.Fail() 82 } 83 } 84 }) 85 } 86 } 87 88 func BenchmarkShardsEquals(b *testing.B) { 89 for i := 16; i <= 4096; i *= 4 { 90 rndShards := makeTestShards(i) 91 shards := NewShards(rndShards) 92 clone := shards.Clone() 93 94 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 95 var res bool 96 for i := 0; i < b.N; i++ { 97 shards.Equals(clone) 98 } 99 runtime.KeepAlive(res) 100 }) 101 } 102 } 103 104 func BenchmarkShardsNumShardsForState(b *testing.B) { 105 for i := 16; i <= 4096; i *= 4 { 106 rndShards := makeTestShards(i) 107 shards := NewShards(rndShards) 108 109 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 110 var res int 111 for i := 0; i < b.N; i++ { 112 res = shards.NumShardsForState(defaultShardState) 113 } 114 runtime.KeepAlive(res) 115 }) 116 } 117 } 118 119 func BenchmarkShardsShard(b *testing.B) { 120 for i := 16; i <= 4096; i *= 4 { 121 ids := randomIDs(1, i*2) 122 rndShards := makeTestShards(i) 123 shards := NewShards(rndShards) 124 125 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 126 var res Shard 127 for i := 0; i < b.N; i++ { 128 res, _ = shards.Shard(ids[i%len(ids)]) 129 } 130 runtime.KeepAlive(res) 131 }) 132 } 133 } 134 135 func BenchmarkShardsContains(b *testing.B) { 136 for i := 16; i <= 4096; i *= 4 { 137 ids := randomIDs(1, i*2) 138 rndShards := makeTestShards(i) 139 shards := NewShards(rndShards) 140 141 b.Run(fmt.Sprintf("%d shards", i), func(b *testing.B) { 142 var res bool 143 for i := 0; i < b.N; i++ { 144 res = shards.Contains(ids[i%len(ids)]) 145 } 146 runtime.KeepAlive(res) 147 }) 148 } 149 }