func newLatencyTracker() *latencyTracker {
	return &latencyTracker{
		estimator: quantile.New(
			quantile.Unknown(0.01),
			quantile.Known(0.50, 0.01),
			quantile.Known(0.95, 0.001),
			quantile.Known(0.99, 0.0005),
			quantile.Known(1.0, 0.0005),
		),
		started: time.Now(),
	}
}
Esempio n. 2
0
// Add implements the Add method of the Report interface by adding the given
// Result to Metrics.
func (m *Metrics) Add(r *Result) {
	if m.StatusCodes == nil {
		m.StatusCodes = map[string]int{}
	}

	if m.errors == nil {
		m.Errors = []string{}
		m.errors = map[string]struct{}{}
	}

	if m.latencies == nil {
		m.latencies = quantile.New(
			quantile.Known(0.50, 0.01),
			quantile.Known(0.95, 0.001),
			quantile.Known(0.99, 0.0005),
		)
	}

	m.Requests++
	m.StatusCodes[strconv.Itoa(int(r.Code))]++
	m.Latencies.Total += r.Latency
	m.BytesOut.Total += r.BytesOut
	m.BytesIn.Total += r.BytesIn

	m.latencies.Add(float64(r.Latency))

	if m.Earliest.IsZero() || m.Earliest.After(r.Timestamp) {
		m.Earliest = r.Timestamp
	}

	if r.Timestamp.After(m.Latest) {
		m.Latest = r.Timestamp
	}

	if end := r.End(); end.After(m.End) {
		m.End = end
	}

	if r.Latency > m.Latencies.Max {
		m.Latencies.Max = r.Latency
	}

	if r.Code >= 200 && r.Code < 400 {
		m.success++
	}

	if r.Error != "" {
		if _, ok := m.errors[r.Error]; !ok {
			m.errors[r.Error] = struct{}{}
			m.Errors = append(m.Errors, r.Error)
		}
	}
}
Esempio n. 3
0
func (m *Metrics) init() {
	if m.StatusCodes == nil {
		m.StatusCodes = map[string]int{}
	}

	if m.errors == nil {
		m.errors = map[string]struct{}{}
	}

	if m.latencies == nil {
		m.latencies = quantile.New(
			quantile.Known(0.50, 0.01),
			quantile.Known(0.95, 0.001),
			quantile.Known(0.99, 0.0005),
		)
	}
}