Kaynağa Gözat

Add UpdateSince for use with defer.

Richard Crowley 13 yıl önce
ebeveyn
işleme
e0ec3c8081
2 değiştirilmiş dosya ile 10 ekleme ve 4 silme
  1. 9 3
      timer.go
  2. 1 1
      timer_test.go

+ 9 - 3
timer.go

@@ -102,11 +102,17 @@ func (t *StandardTimer) StdDev() float64 {
 func (t *StandardTimer) Time(f func()) {
 	ts := time.Now()
 	f()
-	t.Update(uint64(time.Now().Sub(ts)))
+	t.Update(time.Since(ts))
 }
 
 // Record the duration of an event.
-func (t *StandardTimer) Update(duration uint64) {
-	t.h.Update(int64(duration))
+func (t *StandardTimer) Update(d time.Duration) {
+	t.h.Update(int64(d))
+	t.m.Mark(1)
+}
+
+// Record the duration of an event that started at a time and ends now.
+func (t *StandardTimer) UpdateSince(ts time.Time) {
+	t.h.Update(int64(time.Since(ts)))
 	t.m.Mark(1)
 }

+ 1 - 1
timer_test.go

@@ -49,7 +49,7 @@ func TestTimerZero(t *testing.T) {
 
 func TestTimerExtremes(t *testing.T) {
 	tm := NewTimer()
-	tm.Update(uint64(math.MaxInt64))
+	tm.Update(math.MaxInt64)
 	tm.Update(0)
 	if stdDev := tm.StdDev(); 6.521908912666392e18 != stdDev {
 		t.Errorf("tm.StdDev(): 6.521908912666392e18 != %v\n", stdDev)