.github
accounts
build
cmd
common
consensus
console
contracts
core
crypto
dashboard
docs
eth
ethclient
ethdb
ethstats
event
graphql
internal
les
light
log
metrics
exp
influxdb
librato
FORK.md
LICENSE
README.md
counter.go
counter_test.go
debug.go
debug_test.go
disk.go
disk_linux.go
disk_nop.go
ewma.go
ewma_test.go
gauge.go
gauge_float64.go
gauge_float64_test.go
gauge_test.go
graphite.go
graphite_test.go
healthcheck.go
histogram.go
histogram_test.go
init_test.go
json.go
json_test.go
log.go
memory.md
meter.go
meter_test.go
metrics.go
metrics_test.go
opentsdb.go
opentsdb_test.go
registry.go
registry_test.go
resetting_timer.go
resetting_timer_test.go
runtime.go
runtime_cgo.go
runtime_gccpufraction.go
runtime_no_cgo.go
runtime_no_gccpufraction.go
runtime_test.go
sample.go
sample_test.go
syslog.go
timer.go
timer_test.go
validate.sh
writer.go
writer_test.go
miner
mobile
node
p2p
params
rlp
rpc
signer
swarm
tests
trie
vendor
whisper
.dockerignore
.gitattributes
.gitignore
.gitmodules
.mailmap
.travis.yml
AUTHORS
COPYING
COPYING.LESSER
Dockerfile
Dockerfile.alltools
Makefile
README.md
appveyor.yml
circle.yml
interfaces.go
306 lines
5.5 KiB
Go
306 lines
5.5 KiB
Go
![]() |
package metrics
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func BenchmarkRegistry(b *testing.B) {
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
b.ResetTimer()
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
r.Each(func(string, interface{}) {})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistry(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if "foo" != name {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if 1 != i {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
r.Unregister("foo")
|
||
|
i = 0
|
||
|
r.Each(func(string, interface{}) { i++ })
|
||
|
if 0 != i {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryDuplicate(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
if err := r.Register("foo", NewCounter()); nil != err {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if err := r.Register("foo", NewGauge()); nil == err {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if 1 != i {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryGet(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
if count := r.Get("foo").(Counter).Count(); 0 != count {
|
||
|
t.Fatal(count)
|
||
|
}
|
||
|
r.Get("foo").(Counter).Inc(1)
|
||
|
if count := r.Get("foo").(Counter).Count(); 1 != count {
|
||
|
t.Fatal(count)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryGetOrRegister(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
|
||
|
// First metric wins with GetOrRegister
|
||
|
_ = r.GetOrRegister("foo", NewCounter())
|
||
|
m := r.GetOrRegister("foo", NewGauge())
|
||
|
if _, ok := m.(Counter); !ok {
|
||
|
t.Fatal(m)
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if name != "foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
|
||
|
// First metric wins with GetOrRegister
|
||
|
_ = r.GetOrRegister("foo", NewCounter)
|
||
|
m := r.GetOrRegister("foo", NewGauge)
|
||
|
if _, ok := m.(Counter); !ok {
|
||
|
t.Fatal(m)
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if name != "foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryUnregister(t *testing.T) {
|
||
|
l := len(arbiter.meters)
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
r.Register("bar", NewMeter())
|
||
|
r.Register("baz", NewTimer())
|
||
|
if len(arbiter.meters) != l+2 {
|
||
|
t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters))
|
||
|
}
|
||
|
r.Unregister("foo")
|
||
|
r.Unregister("bar")
|
||
|
r.Unregister("baz")
|
||
|
if len(arbiter.meters) != l {
|
||
|
t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedChildRegistryGetOrRegister(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
pr := NewPrefixedChildRegistry(r, "prefix.")
|
||
|
|
||
|
_ = pr.GetOrRegister("foo", NewCounter())
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryGetOrRegister(t *testing.T) {
|
||
|
r := NewPrefixedRegistry("prefix.")
|
||
|
|
||
|
_ = r.GetOrRegister("foo", NewCounter())
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryRegister(t *testing.T) {
|
||
|
r := NewPrefixedRegistry("prefix.")
|
||
|
err := r.Register("foo", NewCounter())
|
||
|
c := NewCounter()
|
||
|
Register("bar", c)
|
||
|
if err != nil {
|
||
|
t.Fatal(err.Error())
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryUnregister(t *testing.T) {
|
||
|
r := NewPrefixedRegistry("prefix.")
|
||
|
|
||
|
_ = r.Register("foo", NewCounter())
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
|
||
|
r.Unregister("foo")
|
||
|
|
||
|
i = 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
})
|
||
|
|
||
|
if i != 0 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryGet(t *testing.T) {
|
||
|
pr := NewPrefixedRegistry("prefix.")
|
||
|
name := "foo"
|
||
|
pr.Register(name, NewCounter())
|
||
|
|
||
|
fooCounter := pr.Get(name)
|
||
|
if fooCounter == nil {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedChildRegistryGet(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
pr := NewPrefixedChildRegistry(r, "prefix.")
|
||
|
name := "foo"
|
||
|
pr.Register(name, NewCounter())
|
||
|
fooCounter := pr.Get(name)
|
||
|
if fooCounter == nil {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestChildPrefixedRegistryRegister(t *testing.T) {
|
||
|
r := NewPrefixedChildRegistry(DefaultRegistry, "prefix.")
|
||
|
err := r.Register("foo", NewCounter())
|
||
|
c := NewCounter()
|
||
|
Register("bar", c)
|
||
|
if err != nil {
|
||
|
t.Fatal(err.Error())
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestChildPrefixedRegistryOfChildRegister(t *testing.T) {
|
||
|
r := NewPrefixedChildRegistry(NewRegistry(), "prefix.")
|
||
|
r2 := NewPrefixedChildRegistry(r, "prefix2.")
|
||
|
err := r.Register("foo2", NewCounter())
|
||
|
if err != nil {
|
||
|
t.Fatal(err.Error())
|
||
|
}
|
||
|
err = r2.Register("baz", NewCounter())
|
||
|
c := NewCounter()
|
||
|
Register("bars", c)
|
||
|
|
||
|
i := 0
|
||
|
r2.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
if name != "prefix.prefix2.baz" {
|
||
|
//t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestWalkRegistries(t *testing.T) {
|
||
|
r := NewPrefixedChildRegistry(NewRegistry(), "prefix.")
|
||
|
r2 := NewPrefixedChildRegistry(r, "prefix2.")
|
||
|
err := r.Register("foo2", NewCounter())
|
||
|
if err != nil {
|
||
|
t.Fatal(err.Error())
|
||
|
}
|
||
|
err = r2.Register("baz", NewCounter())
|
||
|
c := NewCounter()
|
||
|
Register("bars", c)
|
||
|
|
||
|
_, prefix := findPrefix(r2, "")
|
||
|
if "prefix.prefix2." != prefix {
|
||
|
t.Fatal(prefix)
|
||
|
}
|
||
|
|
||
|
}
|