102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
|
package metrics
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestSummarySerial(t *testing.T) {
|
||
|
name := `TestSummarySerial`
|
||
|
s := NewSummary(name)
|
||
|
|
||
|
// Verify that the summary isn't visible in the output of WritePrometheus,
|
||
|
// since it doesn't contain any values yet.
|
||
|
var bb bytes.Buffer
|
||
|
WritePrometheus(&bb, false)
|
||
|
result := bb.String()
|
||
|
if strings.Contains(result, name) {
|
||
|
t.Fatalf("summary %s shouldn't be visible in the WritePrometheus output; got\n%s", name, result)
|
||
|
}
|
||
|
|
||
|
// Write data to summary
|
||
|
for i := 0; i < 2000; i++ {
|
||
|
s.Update(float64(i))
|
||
|
t := time.Now()
|
||
|
s.UpdateDuration(t.Add(-time.Millisecond * time.Duration(i)))
|
||
|
}
|
||
|
|
||
|
// Make sure the summary doesn't print anything on marshalTo call
|
||
|
// and updates s.quantileValues.
|
||
|
testMarshalTo(t, s, "prefix", "")
|
||
|
|
||
|
// Verify s.quantileValues
|
||
|
if s.quantileValues[len(s.quantileValues)-1] != 1999 {
|
||
|
t.Fatalf("unexpected quantileValues[last]; got %v; want %v", s.quantileValues[len(s.quantileValues)-1], 1999)
|
||
|
}
|
||
|
|
||
|
// Make sure the summary becomes visible in the output of WritePrometheus,
|
||
|
// since now it contains values.
|
||
|
bb.Reset()
|
||
|
WritePrometheus(&bb, false)
|
||
|
result = bb.String()
|
||
|
if !strings.Contains(result, name) {
|
||
|
t.Fatalf("missing summary %s in the WritePrometheus output; got\n%s", name, result)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSummaryConcurrent(t *testing.T) {
|
||
|
name := "SummaryConcurrent"
|
||
|
s := NewSummary(name)
|
||
|
err := testConcurrent(func() error {
|
||
|
for i := 0; i < 10; i++ {
|
||
|
s.Update(float64(i))
|
||
|
}
|
||
|
testMarshalTo(t, s, "prefix", "")
|
||
|
return nil
|
||
|
})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSummaryWithTags(t *testing.T) {
|
||
|
name := `TestSummary{tag="foo"}`
|
||
|
s := NewSummary(name)
|
||
|
s.Update(123)
|
||
|
|
||
|
var bb bytes.Buffer
|
||
|
WritePrometheus(&bb, false)
|
||
|
result := bb.String()
|
||
|
namePrefixWithTag := `TestSummary{tag="foo",quantile="`
|
||
|
if !strings.Contains(result, namePrefixWithTag) {
|
||
|
t.Fatalf("missing summary prefix %s in the WritePrometheus output; got\n%s", namePrefixWithTag, result)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSummaryInvalidQuantiles(t *testing.T) {
|
||
|
name := "SummaryInvalidQuantiles"
|
||
|
expectPanic(t, name, func() {
|
||
|
NewSummaryExt(name, time.Minute, []float64{123, -234})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestSummarySmallWindow(t *testing.T) {
|
||
|
name := "SummarySmallWindow"
|
||
|
window := time.Millisecond * 20
|
||
|
quantiles := []float64{0.1, 0.2, 0.3}
|
||
|
s := NewSummaryExt(name, window, quantiles)
|
||
|
for i := 0; i < 2000; i++ {
|
||
|
s.Update(123)
|
||
|
}
|
||
|
// Wait for window update and verify that the summary has been cleared.
|
||
|
time.Sleep(5 * window)
|
||
|
var bb bytes.Buffer
|
||
|
WritePrometheus(&bb, false)
|
||
|
result := bb.String()
|
||
|
if strings.Contains(result, name) {
|
||
|
t.Fatalf("summary %s cannot be present in the WritePrometheus output; got\n%s", name, result)
|
||
|
}
|
||
|
}
|