| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func BenchmarkMeter(b *testing.B) { |
| 9 | m := NewMeter() |
| 10 | b.ResetTimer() |
| 11 | for i := 0; i < b.N; i++ { |
| 12 | m.Mark(1) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | func TestGetOrRegisterMeter(t *testing.T) { |
| 17 | r := NewRegistry() |
| 18 | NewRegisteredMeter("foo", r).Mark(47) |
| 19 | if m := GetOrRegisterMeter("foo", r); 47 != m.Count() { |
| 20 | t.Fatal(m) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestMeterDecay(t *testing.T) { |
| 25 | ma := meterArbiter{ |
| 26 | ticker: time.NewTicker(1), |
| 27 | } |
| 28 | m := newStandardMeter() |
| 29 | ma.meters = append(ma.meters, m) |
| 30 | go ma.tick() |
| 31 | m.Mark(1) |
| 32 | rateMean := m.RateMean() |
| 33 | time.Sleep(1) |
| 34 | if m.RateMean() >= rateMean { |
| 35 | t.Error("m.RateMean() didn't decrease") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestMeterNonzero(t *testing.T) { |
| 40 | m := NewMeter() |
| 41 | m.Mark(3) |
| 42 | if count := m.Count(); 3 != count { |
| 43 | t.Errorf("m.Count(): 3 != %v\n", count) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestMeterSnapshot(t *testing.T) { |
| 48 | m := NewMeter() |
| 49 | m.Mark(1) |
| 50 | if snapshot := m.Snapshot(); m.RateMean() != snapshot.RateMean() { |
| 51 | t.Fatal(snapshot) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestMeterZero(t *testing.T) { |
| 56 | m := NewMeter() |
| 57 | if count := m.Count(); 0 != count { |
| 58 | t.Errorf("m.Count(): 0 != %v\n", count) |
| 59 | } |
| 60 | } |