github.com/theQRL/go-zond@v0.1.1/metrics/registry_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func BenchmarkRegistry(b *testing.B) {
     9  	r := NewRegistry()
    10  	r.Register("foo", NewCounter())
    11  	b.ResetTimer()
    12  	for i := 0; i < b.N; i++ {
    13  		r.Each(func(string, interface{}) {})
    14  	}
    15  }
    16  
    17  func BenchmarkRegistryGetOrRegisterParallel_8(b *testing.B) {
    18  	benchmarkRegistryGetOrRegisterParallel(b, 8)
    19  }
    20  
    21  func BenchmarkRegistryGetOrRegisterParallel_32(b *testing.B) {
    22  	benchmarkRegistryGetOrRegisterParallel(b, 32)
    23  }
    24  
    25  func benchmarkRegistryGetOrRegisterParallel(b *testing.B, amount int) {
    26  	r := NewRegistry()
    27  	b.ResetTimer()
    28  	var wg sync.WaitGroup
    29  	for i := 0; i < amount; i++ {
    30  		wg.Add(1)
    31  		go func() {
    32  			for i := 0; i < b.N; i++ {
    33  				r.GetOrRegister("foo", NewMeter)
    34  			}
    35  			wg.Done()
    36  		}()
    37  	}
    38  	wg.Wait()
    39  }
    40  
    41  func TestRegistry(t *testing.T) {
    42  	r := NewRegistry()
    43  	r.Register("foo", NewCounter())
    44  	i := 0
    45  	r.Each(func(name string, iface interface{}) {
    46  		i++
    47  		if name != "foo" {
    48  			t.Fatal(name)
    49  		}
    50  		if _, ok := iface.(Counter); !ok {
    51  			t.Fatal(iface)
    52  		}
    53  	})
    54  	if i != 1 {
    55  		t.Fatal(i)
    56  	}
    57  	r.Unregister("foo")
    58  	i = 0
    59  	r.Each(func(string, interface{}) { i++ })
    60  	if i != 0 {
    61  		t.Fatal(i)
    62  	}
    63  }
    64  
    65  func TestRegistryDuplicate(t *testing.T) {
    66  	r := NewRegistry()
    67  	if err := r.Register("foo", NewCounter()); nil != err {
    68  		t.Fatal(err)
    69  	}
    70  	if err := r.Register("foo", NewGauge()); nil == err {
    71  		t.Fatal(err)
    72  	}
    73  	i := 0
    74  	r.Each(func(name string, iface interface{}) {
    75  		i++
    76  		if _, ok := iface.(Counter); !ok {
    77  			t.Fatal(iface)
    78  		}
    79  	})
    80  	if i != 1 {
    81  		t.Fatal(i)
    82  	}
    83  }
    84  
    85  func TestRegistryGet(t *testing.T) {
    86  	r := NewRegistry()
    87  	r.Register("foo", NewCounter())
    88  	if count := r.Get("foo").(Counter).Snapshot().Count(); count != 0 {
    89  		t.Fatal(count)
    90  	}
    91  	r.Get("foo").(Counter).Inc(1)
    92  	if count := r.Get("foo").(Counter).Snapshot().Count(); count != 1 {
    93  		t.Fatal(count)
    94  	}
    95  }
    96  
    97  func TestRegistryGetOrRegister(t *testing.T) {
    98  	r := NewRegistry()
    99  
   100  	// First metric wins with GetOrRegister
   101  	_ = r.GetOrRegister("foo", NewCounter())
   102  	m := r.GetOrRegister("foo", NewGauge())
   103  	if _, ok := m.(Counter); !ok {
   104  		t.Fatal(m)
   105  	}
   106  
   107  	i := 0
   108  	r.Each(func(name string, iface interface{}) {
   109  		i++
   110  		if name != "foo" {
   111  			t.Fatal(name)
   112  		}
   113  		if _, ok := iface.(Counter); !ok {
   114  			t.Fatal(iface)
   115  		}
   116  	})
   117  	if i != 1 {
   118  		t.Fatal(i)
   119  	}
   120  }
   121  
   122  func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
   123  	r := NewRegistry()
   124  
   125  	// First metric wins with GetOrRegister
   126  	_ = r.GetOrRegister("foo", NewCounter)
   127  	m := r.GetOrRegister("foo", NewGauge)
   128  	if _, ok := m.(Counter); !ok {
   129  		t.Fatal(m)
   130  	}
   131  
   132  	i := 0
   133  	r.Each(func(name string, iface interface{}) {
   134  		i++
   135  		if name != "foo" {
   136  			t.Fatal(name)
   137  		}
   138  		if _, ok := iface.(Counter); !ok {
   139  			t.Fatal(iface)
   140  		}
   141  	})
   142  	if i != 1 {
   143  		t.Fatal(i)
   144  	}
   145  }
   146  
   147  func TestRegistryUnregister(t *testing.T) {
   148  	l := len(arbiter.meters)
   149  	r := NewRegistry()
   150  	r.Register("foo", NewCounter())
   151  	r.Register("bar", NewMeter())
   152  	r.Register("baz", NewTimer())
   153  	if len(arbiter.meters) != l+2 {
   154  		t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters))
   155  	}
   156  	r.Unregister("foo")
   157  	r.Unregister("bar")
   158  	r.Unregister("baz")
   159  	if len(arbiter.meters) != l {
   160  		t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters))
   161  	}
   162  }
   163  
   164  func TestPrefixedChildRegistryGetOrRegister(t *testing.T) {
   165  	r := NewRegistry()
   166  	pr := NewPrefixedChildRegistry(r, "prefix.")
   167  
   168  	_ = pr.GetOrRegister("foo", NewCounter())
   169  
   170  	i := 0
   171  	r.Each(func(name string, m interface{}) {
   172  		i++
   173  		if name != "prefix.foo" {
   174  			t.Fatal(name)
   175  		}
   176  	})
   177  	if i != 1 {
   178  		t.Fatal(i)
   179  	}
   180  }
   181  
   182  func TestPrefixedRegistryGetOrRegister(t *testing.T) {
   183  	r := NewPrefixedRegistry("prefix.")
   184  
   185  	_ = r.GetOrRegister("foo", NewCounter())
   186  
   187  	i := 0
   188  	r.Each(func(name string, m interface{}) {
   189  		i++
   190  		if name != "prefix.foo" {
   191  			t.Fatal(name)
   192  		}
   193  	})
   194  	if i != 1 {
   195  		t.Fatal(i)
   196  	}
   197  }
   198  
   199  func TestPrefixedRegistryRegister(t *testing.T) {
   200  	r := NewPrefixedRegistry("prefix.")
   201  	err := r.Register("foo", NewCounter())
   202  	c := NewCounter()
   203  	Register("bar", c)
   204  	if err != nil {
   205  		t.Fatal(err.Error())
   206  	}
   207  
   208  	i := 0
   209  	r.Each(func(name string, m interface{}) {
   210  		i++
   211  		if name != "prefix.foo" {
   212  			t.Fatal(name)
   213  		}
   214  	})
   215  	if i != 1 {
   216  		t.Fatal(i)
   217  	}
   218  }
   219  
   220  func TestPrefixedRegistryUnregister(t *testing.T) {
   221  	r := NewPrefixedRegistry("prefix.")
   222  
   223  	_ = r.Register("foo", NewCounter())
   224  
   225  	i := 0
   226  	r.Each(func(name string, m interface{}) {
   227  		i++
   228  		if name != "prefix.foo" {
   229  			t.Fatal(name)
   230  		}
   231  	})
   232  	if i != 1 {
   233  		t.Fatal(i)
   234  	}
   235  
   236  	r.Unregister("foo")
   237  
   238  	i = 0
   239  	r.Each(func(name string, m interface{}) {
   240  		i++
   241  	})
   242  
   243  	if i != 0 {
   244  		t.Fatal(i)
   245  	}
   246  }
   247  
   248  func TestPrefixedRegistryGet(t *testing.T) {
   249  	pr := NewPrefixedRegistry("prefix.")
   250  	name := "foo"
   251  	pr.Register(name, NewCounter())
   252  
   253  	fooCounter := pr.Get(name)
   254  	if fooCounter == nil {
   255  		t.Fatal(name)
   256  	}
   257  }
   258  
   259  func TestPrefixedChildRegistryGet(t *testing.T) {
   260  	r := NewRegistry()
   261  	pr := NewPrefixedChildRegistry(r, "prefix.")
   262  	name := "foo"
   263  	pr.Register(name, NewCounter())
   264  	fooCounter := pr.Get(name)
   265  	if fooCounter == nil {
   266  		t.Fatal(name)
   267  	}
   268  }
   269  
   270  func TestChildPrefixedRegistryRegister(t *testing.T) {
   271  	r := NewPrefixedChildRegistry(DefaultRegistry, "prefix.")
   272  	err := r.Register("foo", NewCounter())
   273  	c := NewCounter()
   274  	Register("bar", c)
   275  	if err != nil {
   276  		t.Fatal(err.Error())
   277  	}
   278  
   279  	i := 0
   280  	r.Each(func(name string, m interface{}) {
   281  		i++
   282  		if name != "prefix.foo" {
   283  			t.Fatal(name)
   284  		}
   285  	})
   286  	if i != 1 {
   287  		t.Fatal(i)
   288  	}
   289  }
   290  
   291  func TestChildPrefixedRegistryOfChildRegister(t *testing.T) {
   292  	r := NewPrefixedChildRegistry(NewRegistry(), "prefix.")
   293  	r2 := NewPrefixedChildRegistry(r, "prefix2.")
   294  	err := r.Register("foo2", NewCounter())
   295  	if err != nil {
   296  		t.Fatal(err.Error())
   297  	}
   298  	err = r2.Register("baz", NewCounter())
   299  	if err != nil {
   300  		t.Fatal(err.Error())
   301  	}
   302  	c := NewCounter()
   303  	Register("bars", c)
   304  
   305  	i := 0
   306  	r2.Each(func(name string, m interface{}) {
   307  		i++
   308  		if name != "prefix.prefix2.baz" {
   309  			t.Fatal(name)
   310  		}
   311  	})
   312  	if i != 1 {
   313  		t.Fatal(i)
   314  	}
   315  }
   316  
   317  func TestWalkRegistries(t *testing.T) {
   318  	r := NewPrefixedChildRegistry(NewRegistry(), "prefix.")
   319  	r2 := NewPrefixedChildRegistry(r, "prefix2.")
   320  	err := r.Register("foo2", NewCounter())
   321  	if err != nil {
   322  		t.Fatal(err.Error())
   323  	}
   324  	err = r2.Register("baz", NewCounter())
   325  	if err != nil {
   326  		t.Fatal(err.Error())
   327  	}
   328  	c := NewCounter()
   329  	Register("bars", c)
   330  
   331  	_, prefix := findPrefix(r2, "")
   332  	if prefix != "prefix.prefix2." {
   333  		t.Fatal(prefix)
   334  	}
   335  }