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