Skip to content

Commit

Permalink
fix timer count sampling, honor persist-count-keys
Browse files Browse the repository at this point in the history
by handling the timer count with the plain counters

also, BenchmarkManyDifferentSensors should clear the stats maps ...
  • Loading branch information
ploxiln committed Jul 27, 2018
1 parent 619304e commit d9e86f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion statsdaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func packetHandler(s *Packet) {
case "ms":
// if missing gets nil []float64, works with append()
timers[s.Bucket] = append(timers[s.Bucket], s.ValFlt)
counters[s.Bucket+".count"] += float64(1 / s.Sampling)
case "g":
var gaugeValue float64
if s.ValStr == "" {
Expand Down Expand Up @@ -339,7 +340,6 @@ func processTimers(buffer *bytes.Buffer, now int64, pctls Percentiles) int64 {
fmt.Fprintf(buffer, "%s%s.mean%s %s %d\n", *prefix, bucket, *postfix, mean_s, now)
fmt.Fprintf(buffer, "%s%s.upper%s %s %d\n", *prefix, bucket, *postfix, max_s, now)
fmt.Fprintf(buffer, "%s%s.lower%s %s %d\n", *prefix, bucket, *postfix, min_s, now)
fmt.Fprintf(buffer, "%s%s.count%s %d %d\n", *prefix, bucket, *postfix, count, now)

delete(timers, bucket)
}
Expand Down
10 changes: 9 additions & 1 deletion statsdaemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func TestPacketHandlerGauge(t *testing.T) {

func TestPacketHandlerTimer(t *testing.T) {
timers = make(map[string]Float64Slice)
counters = make(map[string]float64)

p := &Packet{
Bucket: "glork",
Expand All @@ -460,11 +461,13 @@ func TestPacketHandlerTimer(t *testing.T) {
packetHandler(p)
assert.Equal(t, len(timers["glork"]), 1)
assert.Equal(t, timers["glork"][0], float64(320))
assert.Equal(t, counters["glork.count"], 1.0)

p.ValFlt = float64(100)
packetHandler(p)
assert.Equal(t, len(timers["glork"]), 2)
assert.Equal(t, timers["glork"][1], float64(100))
assert.Equal(t, counters["glork.count"], 2.0)
}

func TestPacketHandlerSet(t *testing.T) {
Expand Down Expand Up @@ -558,7 +561,7 @@ func TestProcessTimers(t *testing.T) {
assert.Equal(t, string(lines[0]), "response_time.mean 20 1418052649")
assert.Equal(t, string(lines[1]), "response_time.upper 30 1418052649")
assert.Equal(t, string(lines[2]), "response_time.lower 0 1418052649")
assert.Equal(t, string(lines[3]), "response_time.count 3 1418052649")
// response_time.count handled by processCounters, not processTimers

num = processTimers(&buffer, now, Percentiles{})
assert.Equal(t, num, int64(0))
Expand Down Expand Up @@ -769,7 +772,11 @@ func TestMultipleUDPSends(t *testing.T) {
}

func BenchmarkManyDifferentSensors(t *testing.B) {
counters = make(map[string]float64)
gauges = make(map[string]float64)
timers = make(map[string]Float64Slice)
r := rand.New(rand.NewSource(438))

for i := 0; i < 1000; i++ {
bucket := "response_time" + strconv.Itoa(i)
for i := 0; i < 10000; i++ {
Expand Down Expand Up @@ -925,6 +932,7 @@ func BenchmarkPacketHandlerTimer(b *testing.B) {
d1 := parseLine([]byte("glork.some.keyspace:3.7211|ms"))
d2 := parseLine([]byte("glork.some.keyspace:11223|ms"))
timers = make(map[string]Float64Slice)
counters = make(map[string]float64)

for i := 0; i < b.N; i++ {
packetHandler(d1)
Expand Down

0 comments on commit d9e86f9

Please sign in to comment.