Richard Crowley 13 роки тому
батько
коміт
15d638721f
5 змінених файлів з 52 додано та 29 видалено
  1. 6 6
      ewma.go
  2. 3 1
      ewma_test.go
  3. 28 17
      histogram.go
  4. 3 1
      histogram_test.go
  5. 12 4
      sample_test.go

+ 6 - 6
ewma.go

@@ -20,10 +20,10 @@ type EWMA interface {
 // events and processes them on each tick.  It uses the sync/atomic package
 // to manage uncounted events.
 type StandardEWMA struct {
-	alpha float64
+	alpha     float64
 	uncounted int64
-	in chan bool
-	out chan float64
+	in        chan bool
+	out       chan float64
 }
 
 // Create a new EWMA with the given alpha.  Create the clock channel and
@@ -36,17 +36,17 @@ func NewEWMA(alpha float64) *StandardEWMA {
 
 // Create a new EWMA with alpha set for a one-minute moving average.
 func NewEWMA1() *StandardEWMA {
-	return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 1))
+	return NewEWMA(1 - math.Exp(-5.0/60.0/1))
 }
 
 // Create a new EWMA with alpha set for a five-minute moving average.
 func NewEWMA5() *StandardEWMA {
-	return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 5))
+	return NewEWMA(1 - math.Exp(-5.0/60.0/5))
 }
 
 // Create a new EWMA with alpha set for a fifteen-minute moving average.
 func NewEWMA15() *StandardEWMA {
-	return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 15))
+	return NewEWMA(1 - math.Exp(-5.0/60.0/15))
 }
 
 // Return the moving average rate of events per second.

+ 3 - 1
ewma_test.go

@@ -210,5 +210,7 @@ func TestEWMA15(t *testing.T) {
 }
 
 func elapseMinute(a EWMA) {
-	for i := 0; i < 12; i++ { a.Tick() }
+	for i := 0; i < 12; i++ {
+		a.Tick()
+	}
 }

+ 28 - 17
histogram.go

@@ -25,9 +25,9 @@ type Histogram interface {
 // The standard implementation of a Histogram uses a Sample and a goroutine
 // to synchronize its calculations.
 type StandardHistogram struct {
-	s Sample
-	in chan int64
-	out chan histogramV
+	s     Sample
+	in    chan int64
+	out   chan histogramV
 	reset chan bool
 }
 
@@ -35,7 +35,7 @@ type StandardHistogram struct {
 // from the synchronizing goroutine.
 type histogramV struct {
 	count, sum, min, max int64
-	variance [2]float64
+	variance             [2]float64
 }
 
 // Create a new histogram with the given Sample.  Create the communication
@@ -74,7 +74,9 @@ func (h *StandardHistogram) Count() int64 {
 // Return the maximal value seen since the histogram was last cleared.
 func (h *StandardHistogram) Max() int64 {
 	hv := <-h.out
-	if 0 < hv.count { return hv.max }
+	if 0 < hv.count {
+		return hv.max
+	}
 	return 0
 }
 
@@ -90,7 +92,9 @@ func (h *StandardHistogram) Mean() float64 {
 // Return the minimal value seen since the histogram was last cleared.
 func (h *StandardHistogram) Min() int64 {
 	hv := <-h.out
-	if 0 < hv.count { return hv.min }
+	if 0 < hv.count {
+		return hv.min
+	}
 	return 0
 }
 
@@ -109,15 +113,15 @@ func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
 	if size > 0 {
 		sort.Sort(values)
 		for i, p := range ps {
-			pos := p * float64(size + 1)
+			pos := p * float64(size+1)
 			if pos < 1.0 {
 				scores[i] = float64(values[0])
 			} else if pos >= float64(size) {
-				scores[i] = float64(values[size - 1])
+				scores[i] = float64(values[size-1])
 			} else {
-				lower := float64(values[int(pos) - 1])
+				lower := float64(values[int(pos)-1])
 				upper := float64(values[int(pos)])
-				scores[i] = lower + (pos - math.Floor(pos)) * (upper - lower)
+				scores[i] = lower + (pos-math.Floor(pos))*(upper-lower)
 			}
 		}
 	}
@@ -138,8 +142,10 @@ func (h *StandardHistogram) Update(v int64) {
 // Return the variance of all values seen since the histogram was last cleared.
 func (h *StandardHistogram) Variance() float64 {
 	hv := <-h.out
-	if 1 >= hv.count { return 0.0 }
-	return hv.variance[1] / float64(hv.count - 1)
+	if 1 >= hv.count {
+		return 0.0
+	}
+	return hv.variance[1] / float64(hv.count-1)
 }
 
 // Receive inputs and send outputs.  Sample each input and update values in
@@ -151,8 +157,12 @@ func (h *StandardHistogram) arbiter() {
 		case v := <-h.in:
 			h.s.Update(v)
 			hv.count++
-			if v < hv.min { hv.min = v }
-			if v > hv.max { hv.max = v }
+			if v < hv.min {
+				hv.min = v
+			}
+			if v > hv.max {
+				hv.max = v
+			}
 			hv.sum += v
 			fv := float64(v)
 			if -1.0 == hv.variance[0] {
@@ -161,11 +171,11 @@ func (h *StandardHistogram) arbiter() {
 			} else {
 				m := hv.variance[0]
 				s := hv.variance[1]
-				hv.variance[0] = m + (fv - m) / float64(hv.count)
-				hv.variance[1] = s + (fv - m) * (fv - hv.variance[0])
+				hv.variance[0] = m + (fv-m)/float64(hv.count)
+				hv.variance[1] = s + (fv-m)*(fv-hv.variance[0])
 			}
 		case h.out <- hv:
-		case <- h.reset:
+		case <-h.reset:
 			h.s.Clear()
 			hv = newHistogramV()
 		}
@@ -174,6 +184,7 @@ func (h *StandardHistogram) arbiter() {
 
 // Cribbed from the standard library's `sort` package.
 type int64Slice []int64
+
 func (p int64Slice) Len() int           { return len(p) }
 func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
 func (p int64Slice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

+ 3 - 1
histogram_test.go

@@ -33,7 +33,9 @@ func TestEmptyHistogram(t *testing.T) {
 
 func TestHistogram10000(t *testing.T) {
 	h := NewHistogram(NewUniformSample(100000))
-	for i := 1; i <= 10000; i++ { h.Update(int64(i)) }
+	for i := 1; i <= 10000; i++ {
+		h.Update(int64(i))
+	}
 	if count := h.Count(); 10000 != count {
 		t.Errorf("h.Count(): 10000 != %v\n", count)
 	}

+ 12 - 4
sample_test.go

@@ -4,7 +4,9 @@ import "testing"
 
 func TestExpDecaySample10(t *testing.T) {
 	s := NewExpDecaySample(100, 0.99)
-	for i := 0; i < 10; i++ { s.Update(int64(i)) }
+	for i := 0; i < 10; i++ {
+		s.Update(int64(i))
+	}
 	if size := s.Size(); 10 != size {
 		t.Errorf("s.Size(): 10 != %v\n", size)
 	}
@@ -20,7 +22,9 @@ func TestExpDecaySample10(t *testing.T) {
 
 func TestExpDecaySample100(t *testing.T) {
 	s := NewExpDecaySample(1000, 0.01)
-	for i := 0; i < 100; i++ { s.Update(int64(i)) }
+	for i := 0; i < 100; i++ {
+		s.Update(int64(i))
+	}
 	if size := s.Size(); 100 != size {
 		t.Errorf("s.Size(): 100 != %v\n", size)
 	}
@@ -36,7 +40,9 @@ func TestExpDecaySample100(t *testing.T) {
 
 func TestExpDecaySample1000(t *testing.T) {
 	s := NewExpDecaySample(100, 0.99)
-	for i := 0; i < 1000; i++ { s.Update(int64(i)) }
+	for i := 0; i < 1000; i++ {
+		s.Update(int64(i))
+	}
 	if size := s.Size(); 100 != size {
 		t.Errorf("s.Size(): 100 != %v\n", size)
 	}
@@ -52,7 +58,9 @@ func TestExpDecaySample1000(t *testing.T) {
 
 func TestUniformSample(t *testing.T) {
 	s := NewUniformSample(100)
-	for i := 0; i < 1000; i++ { s.Update(int64(i)) }
+	for i := 0; i < 1000; i++ {
+		s.Update(int64(i))
+	}
 	if size := s.Size(); 100 != size {
 		t.Errorf("s.Size(): 100 != %v\n", size)
 	}