Skip to content

Commit

Permalink
Add Timespan Func with Initial Value (#27)
Browse files Browse the repository at this point in the history
* update func

* unit test fix

* fix edge case

* lint fix

* unit test logic

* update to value
  • Loading branch information
nathanliu1 authored and rodaine committed Mar 12, 2018
1 parent 26800ee commit 943f43e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ type Timer interface {
// They measure time from the time they are allocated by a Timer with
// AllocateSpan()
// until they call
// Complete().
// Complete()
// or
// CompleteWithDuration(time.Duration)
// When either function is called the timespan is flushed.
// When Complete is called the timespan is flushed.
//
// A Timespan can be flushed at function
// return by calling Complete with golang's defer statement.
type Timespan interface {
// End the Timespan and flush it.
Complete()

// End the Timespan and flush it. Adds additional time.Duration to the measured time
CompleteWithDuration(time.Duration)
}

// A StatGenerator can be used to programatically generate stats.
Expand Down Expand Up @@ -288,6 +294,10 @@ func (ts *timespan) Complete() {
ts.timer.time(time.Now().Sub(ts.start))
}

func (ts *timespan) CompleteWithDuration(value time.Duration) {
ts.timer.time(value)
}

type statStore struct {
countersMtx sync.RWMutex
counters map[string]*counter
Expand Down
16 changes: 16 additions & 0 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stats
import (
"math/rand"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -35,6 +36,21 @@ func TestStats(t *testing.T) {
wg.Wait()
}

// Ensure timers and timespans are working
func TestTimer(t *testing.T) {
testDuration := time.Duration(9800000)
sink := &testStatSink{}
store := NewStore(sink, true)
store.NewTimer("test").AllocateSpan().CompleteWithDuration(testDuration)
store.Flush()

expected := "test:9800.000000|ms"
timer := sink.record
if !strings.Contains(timer, expected) {
t.Error("wanted timer value of test:9800.000000|ms, got", timer)
}
}

var bmID = ""
var bmVal = uint64(0)

Expand Down

0 comments on commit 943f43e

Please sign in to comment.