| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | package metrics | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"math" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkTimer(b *testing.B) { | 
					
						
							|  |  |  | 	tm := NewTimer() | 
					
						
							|  |  |  | 	b.ResetTimer() | 
					
						
							|  |  |  | 	for i := 0; i < b.N; i++ { | 
					
						
							|  |  |  | 		tm.Update(1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetOrRegisterTimer(t *testing.T) { | 
					
						
							|  |  |  | 	r := NewRegistry() | 
					
						
							|  |  |  | 	NewRegisteredTimer("foo", r).Update(47) | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if tm := GetOrRegisterTimer("foo", r); tm.Count() != 1 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Fatal(tm) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTimerExtremes(t *testing.T) { | 
					
						
							|  |  |  | 	tm := NewTimer() | 
					
						
							|  |  |  | 	tm.Update(math.MaxInt64) | 
					
						
							|  |  |  | 	tm.Update(0) | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if stdDev := tm.StdDev(); stdDev != 4.611686018427388e+18 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTimerStop(t *testing.T) { | 
					
						
							|  |  |  | 	l := len(arbiter.meters) | 
					
						
							|  |  |  | 	tm := NewTimer() | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if l+1 != len(arbiter.meters) { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	tm.Stop() | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if l != len(arbiter.meters) { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTimerFunc(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-31 15:01:16 +02:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		tm         = NewTimer() | 
					
						
							|  |  |  | 		testStart  = time.Now() | 
					
						
							|  |  |  | 		actualTime time.Duration | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	tm.Time(func() { | 
					
						
							|  |  |  | 		time.Sleep(50 * time.Millisecond) | 
					
						
							|  |  |  | 		actualTime = time.Since(testStart) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		drift    = time.Millisecond * 2 | 
					
						
							|  |  |  | 		measured = time.Duration(tm.Max()) | 
					
						
							|  |  |  | 		ceil     = actualTime + drift | 
					
						
							|  |  |  | 		floor    = actualTime - drift | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if measured > ceil || measured < floor { | 
					
						
							|  |  |  | 		t.Errorf("tm.Max(): %v > %v || %v > %v\n", measured, ceil, measured, floor) | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTimerZero(t *testing.T) { | 
					
						
							|  |  |  | 	tm := NewTimer() | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if count := tm.Count(); count != 0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Count(): 0 != %v\n", count) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if min := tm.Min(); min != 0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Min(): 0 != %v\n", min) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if max := tm.Max(); max != 0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Max(): 0 != %v\n", max) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if mean := tm.Mean(); mean != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Mean(): 0.0 != %v\n", mean) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if stdDev := tm.StdDev(); stdDev != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ps := tm.Percentiles([]float64{0.5, 0.75, 0.99}) | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if ps[0] != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("median: 0.0 != %v\n", ps[0]) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if ps[1] != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("75th percentile: 0.0 != %v\n", ps[1]) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if ps[2] != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("99th percentile: 0.0 != %v\n", ps[2]) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if rate1 := tm.Rate1(); rate1 != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if rate5 := tm.Rate5(); rate5 != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if rate15 := tm.Rate15(); rate15 != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-22 16:04:35 +01:00
										 |  |  | 	if rateMean := tm.RateMean(); rateMean != 0.0 { | 
					
						
							| 
									
										
										
										
											2018-02-23 10:56:08 +01:00
										 |  |  | 		t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleGetOrRegisterTimer() { | 
					
						
							|  |  |  | 	m := "account.create.latency" | 
					
						
							|  |  |  | 	t := GetOrRegisterTimer(m, nil) | 
					
						
							|  |  |  | 	t.Update(47) | 
					
						
							|  |  |  | 	fmt.Println(t.Max()) // Output: 47 | 
					
						
							|  |  |  | } |