gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/registry_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package metrics 18 19 import ( 20 "testing" 21 ) 22 23 func BenchmarkRegistry(b *testing.B) { 24 r := NewRegistry() 25 r.Register("foo", NewCounter()) 26 b.ResetTimer() 27 for i := 0; i < b.N; i++ { 28 r.Each(func(string, interface{}) {}) 29 } 30 } 31 32 func TestRegistry(t *testing.T) { 33 r := NewRegistry() 34 r.Register("foo", NewCounter()) 35 i := 0 36 r.Each(func(name string, iface interface{}) { 37 i++ 38 if "foo" != name { 39 t.Fatal(name) 40 } 41 if _, ok := iface.(Counter); !ok { 42 t.Fatal(iface) 43 } 44 }) 45 if 1 != i { 46 t.Fatal(i) 47 } 48 r.Unregister("foo") 49 i = 0 50 r.Each(func(string, interface{}) { i++ }) 51 if 0 != i { 52 t.Fatal(i) 53 } 54 } 55 56 func TestRegistryDuplicate(t *testing.T) { 57 r := NewRegistry() 58 if err := r.Register("foo", NewCounter()); nil != err { 59 t.Fatal(err) 60 } 61 if err := r.Register("foo", NewGauge()); nil == err { 62 t.Fatal(err) 63 } 64 i := 0 65 r.Each(func(name string, iface interface{}) { 66 i++ 67 if _, ok := iface.(Counter); !ok { 68 t.Fatal(iface) 69 } 70 }) 71 if 1 != i { 72 t.Fatal(i) 73 } 74 } 75 76 func TestRegistryGet(t *testing.T) { 77 r := NewRegistry() 78 r.Register("foo", NewCounter()) 79 if count := r.Get("foo").(Counter).Count(); 0 != count { 80 t.Fatal(count) 81 } 82 r.Get("foo").(Counter).Inc(1) 83 if count := r.Get("foo").(Counter).Count(); 1 != count { 84 t.Fatal(count) 85 } 86 } 87 88 func TestRegistryGetOrRegister(t *testing.T) { 89 r := NewRegistry() 90 91 // First metric wins with GetOrRegister 92 _ = r.GetOrRegister("foo", NewCounter()) 93 m := r.GetOrRegister("foo", NewGauge()) 94 if _, ok := m.(Counter); !ok { 95 t.Fatal(m) 96 } 97 98 i := 0 99 r.Each(func(name string, iface interface{}) { 100 i++ 101 if name != "foo" { 102 t.Fatal(name) 103 } 104 if _, ok := iface.(Counter); !ok { 105 t.Fatal(iface) 106 } 107 }) 108 if i != 1 { 109 t.Fatal(i) 110 } 111 } 112 113 func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) { 114 r := NewRegistry() 115 116 // First metric wins with GetOrRegister 117 _ = r.GetOrRegister("foo", NewCounter) 118 m := r.GetOrRegister("foo", NewGauge) 119 if _, ok := m.(Counter); !ok { 120 t.Fatal(m) 121 } 122 123 i := 0 124 r.Each(func(name string, iface interface{}) { 125 i++ 126 if name != "foo" { 127 t.Fatal(name) 128 } 129 if _, ok := iface.(Counter); !ok { 130 t.Fatal(iface) 131 } 132 }) 133 if i != 1 { 134 t.Fatal(i) 135 } 136 } 137 138 func TestRegistryUnregister(t *testing.T) { 139 l := len(arbiter.meters) 140 r := NewRegistry() 141 r.Register("foo", NewCounter()) 142 r.Register("bar", NewMeter()) 143 r.Register("baz", NewTimer()) 144 if len(arbiter.meters) != l+2 { 145 t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters)) 146 } 147 r.Unregister("foo") 148 r.Unregister("bar") 149 r.Unregister("baz") 150 if len(arbiter.meters) != l { 151 t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters)) 152 } 153 } 154 155 func TestPrefixedChildRegistryGetOrRegister(t *testing.T) { 156 r := NewRegistry() 157 pr := NewPrefixedChildRegistry(r, "prefix.") 158 159 _ = pr.GetOrRegister("foo", NewCounter()) 160 161 i := 0 162 r.Each(func(name string, m interface{}) { 163 i++ 164 if name != "prefix.foo" { 165 t.Fatal(name) 166 } 167 }) 168 if i != 1 { 169 t.Fatal(i) 170 } 171 } 172 173 func TestPrefixedRegistryGetOrRegister(t *testing.T) { 174 r := NewPrefixedRegistry("prefix.") 175 176 _ = r.GetOrRegister("foo", NewCounter()) 177 178 i := 0 179 r.Each(func(name string, m interface{}) { 180 i++ 181 if name != "prefix.foo" { 182 t.Fatal(name) 183 } 184 }) 185 if i != 1 { 186 t.Fatal(i) 187 } 188 } 189 190 func TestPrefixedRegistryRegister(t *testing.T) { 191 r := NewPrefixedRegistry("prefix.") 192 err := r.Register("foo", NewCounter()) 193 c := NewCounter() 194 Register("bar", c) 195 if err != nil { 196 t.Fatal(err.Error()) 197 } 198 199 i := 0 200 r.Each(func(name string, m interface{}) { 201 i++ 202 if name != "prefix.foo" { 203 t.Fatal(name) 204 } 205 }) 206 if i != 1 { 207 t.Fatal(i) 208 } 209 } 210 211 func TestPrefixedRegistryUnregister(t *testing.T) { 212 r := NewPrefixedRegistry("prefix.") 213 214 _ = r.Register("foo", NewCounter()) 215 216 i := 0 217 r.Each(func(name string, m interface{}) { 218 i++ 219 if name != "prefix.foo" { 220 t.Fatal(name) 221 } 222 }) 223 if i != 1 { 224 t.Fatal(i) 225 } 226 227 r.Unregister("foo") 228 229 i = 0 230 r.Each(func(name string, m interface{}) { 231 i++ 232 }) 233 234 if i != 0 { 235 t.Fatal(i) 236 } 237 } 238 239 func TestPrefixedRegistryGet(t *testing.T) { 240 pr := NewPrefixedRegistry("prefix.") 241 name := "foo" 242 pr.Register(name, NewCounter()) 243 244 fooCounter := pr.Get(name) 245 if fooCounter == nil { 246 t.Fatal(name) 247 } 248 } 249 250 func TestPrefixedChildRegistryGet(t *testing.T) { 251 r := NewRegistry() 252 pr := NewPrefixedChildRegistry(r, "prefix.") 253 name := "foo" 254 pr.Register(name, NewCounter()) 255 fooCounter := pr.Get(name) 256 if fooCounter == nil { 257 t.Fatal(name) 258 } 259 } 260 261 func TestChildPrefixedRegistryRegister(t *testing.T) { 262 r := NewPrefixedChildRegistry(DefaultRegistry, "prefix.") 263 err := r.Register("foo", NewCounter()) 264 c := NewCounter() 265 Register("bar", c) 266 if err != nil { 267 t.Fatal(err.Error()) 268 } 269 270 i := 0 271 r.Each(func(name string, m interface{}) { 272 i++ 273 if name != "prefix.foo" { 274 t.Fatal(name) 275 } 276 }) 277 if i != 1 { 278 t.Fatal(i) 279 } 280 } 281 282 func TestChildPrefixedRegistryOfChildRegister(t *testing.T) { 283 r := NewPrefixedChildRegistry(NewRegistry(), "prefix.") 284 r2 := NewPrefixedChildRegistry(r, "prefix2.") 285 err := r.Register("foo2", NewCounter()) 286 if err != nil { 287 t.Fatal(err.Error()) 288 } 289 err = r2.Register("baz", NewCounter()) 290 c := NewCounter() 291 Register("bars", c) 292 293 i := 0 294 r2.Each(func(name string, m interface{}) { 295 i++ 296 if name != "prefix.prefix2.baz" { 297 //t.Fatal(name) 298 } 299 }) 300 if i != 1 { 301 t.Fatal(i) 302 } 303 } 304 305 func TestWalkRegistries(t *testing.T) { 306 r := NewPrefixedChildRegistry(NewRegistry(), "prefix.") 307 r2 := NewPrefixedChildRegistry(r, "prefix2.") 308 err := r.Register("foo2", NewCounter()) 309 if err != nil { 310 t.Fatal(err.Error()) 311 } 312 err = r2.Register("baz", NewCounter()) 313 c := NewCounter() 314 Register("bars", c) 315 316 _, prefix := findPrefix(r2, "") 317 if "prefix.prefix2." != prefix { 318 t.Fatal(prefix) 319 } 320 321 }