MCPcopy Create free account
hub / github.com/bitly/statsdaemon / processTimers

Function processTimers

statsdaemon.go:306–366  ·  view source on GitHub ↗
(buffer *bytes.Buffer, now int64, pctls Percentiles)

Source from the content-addressed store, hash-verified

304}
305
306func processTimers(buffer *bytes.Buffer, now int64, pctls Percentiles) int64 {
307 var num int64
308 for bucket, timer := range timers {
309 bucketWithoutPostfix := bucket[:len(bucket)-len(*postfix)]
310 num++
311
312 sort.Sort(timer)
313 min := timer[0]
314 max := timer[len(timer)-1]
315 maxAtThreshold := max
316 count := len(timer)
317
318 sum := float64(0)
319 for _, value := range timer {
320 sum += value
321 }
322 mean := sum / float64(len(timer))
323
324 for _, pct := range pctls {
325 if len(timer) > 1 {
326 var abs float64
327 if pct.float >= 0 {
328 abs = pct.float
329 } else {
330 abs = 100 + pct.float
331 }
332 // poor man's math.Round(x):
333 // math.Floor(x + 0.5)
334 indexOfPerc := int(math.Floor(((abs / 100.0) * float64(count)) + 0.5))
335 if pct.float >= 0 {
336 indexOfPerc -= 1 // index offset=0
337 }
338 maxAtThreshold = timer[indexOfPerc]
339 }
340
341 var tmpl string
342 var pctstr string
343 if pct.float >= 0 {
344 tmpl = "%s.upper_%s%s %s %d\n"
345 pctstr = pct.str
346 } else {
347 tmpl = "%s.lower_%s%s %s %d\n"
348 pctstr = pct.str[1:]
349 }
350 threshold_s := strconv.FormatFloat(maxAtThreshold, 'f', -1, 64)
351 fmt.Fprintf(buffer, tmpl, bucketWithoutPostfix, pctstr, *postfix, threshold_s, now)
352 }
353
354 mean_s := strconv.FormatFloat(mean, 'f', -1, 64)
355 max_s := strconv.FormatFloat(max, 'f', -1, 64)
356 min_s := strconv.FormatFloat(min, 'f', -1, 64)
357
358 fmt.Fprintf(buffer, "%s.mean%s %s %d\n", bucketWithoutPostfix, *postfix, mean_s, now)
359 fmt.Fprintf(buffer, "%s.upper%s %s %d\n", bucketWithoutPostfix, *postfix, max_s, now)
360 fmt.Fprintf(buffer, "%s.lower%s %s %d\n", bucketWithoutPostfix, *postfix, min_s, now)
361 fmt.Fprintf(buffer, "%s.count%s %d %d\n", bucketWithoutPostfix, *postfix, count, now)
362
363 delete(timers, bucket)

Callers 8

submitFunction · 0.85
TestProcessTimersFunction · 0.85
BenchmarkOneBigTimerFunction · 0.85
BenchmarkLotsOfTimersFunction · 0.85

Calls

no outgoing calls