func determineProperties(sizes []int) *expvar.Map { var sum int props := new(expvar.Map).Init() for _, n := range sizes { sum += n } // Determine properties sort.Ints(sizes) if len(sizes) > 0 { summary := map[string]int{ "min": sizes[0], "max": sizes[len(sizes)-1], "length": len(sizes), "sum": sum, "95e": sizes[int(float64(len(sizes))*0.95)], } mean := float64(sum) / float64(summary["length"]) // Pack them into an expvar Map for k, v := range summary { n := new(expvar.Int) n.Set(int64(v)) props.Set(k, n) } avge := new(expvar.Float) avge.Set(mean) props.Set("avg", avge) } return props }
func manipulateCapacity(res doorman.Resource, current float64, id string) { clientRequested := new(expvar.Float) for range time.Tick(*interval) { r := rand.Float64() log.V(2).Infof("r=%v decreaseChance=%v increaseChance=%v", r, *decreaseChance, *increaseChance) switch { case r < *decreaseChance: current -= *step log.Infof("client %v will request less: %v.", id, current) case r < *decreaseChance+*increaseChance: log.Infof("client %v will request more: %v.", id, current) current += *step default: log.V(2).Infof("client %v not changing requested capacity", id) continue } if current > *maxCapacity { current = *maxCapacity } if current < *minCapacity { current = *minCapacity } log.Infof("client %v will request %v", id, current) if err := res.Ask(current); err != nil { log.Errorf("res.Ask(%v): %v", current, err) askErrors.Add(1) continue } clientRequested.Set(current) requested.Set(id, clientRequested) } }
func GetFloatValue(f *expvar.Float) float64 { a, e := strconv.ParseFloat(f.String(), 64) if e != nil { return 0 } return a }
// TrackMetrics decorates a MessageHandler to collect metrics about successes, failures and runtime reports in ms*10. func TrackMetrics(successes, failures *expvar.Int, timing *expvar.Float) MessageHandlerDecorator { ema := movingaverage.New(5 * time.Second) return func(fn sqsconsumer.MessageHandlerFunc) sqsconsumer.MessageHandlerFunc { return func(ctx context.Context, msg string) error { start := time.Now() defer func() { v := ema.Update(time.Since(start).Seconds() * 1000) timing.Set(v) }() err := fn(ctx, msg) if err != nil { failures.Add(1) } else { successes.Add(1) } return err } } }