2016-10-07 01:58:24 +03:00
|
|
|
// Copyright 2014 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// the file is borrowed from github.com/rakyll/boom/boomer/print.go
|
|
|
|
|
|
|
|
package report
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
barChar = "∎"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Result describes the timings for an operation.
|
|
|
|
type Result struct {
|
2017-05-18 02:05:08 +03:00
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
Err error
|
|
|
|
Weight float64
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (res *Result) Duration() time.Duration { return res.End.Sub(res.Start) }
|
|
|
|
|
|
|
|
type report struct {
|
|
|
|
results chan Result
|
|
|
|
precision string
|
|
|
|
|
2017-05-18 02:05:08 +03:00
|
|
|
stats Stats
|
|
|
|
sps *secondPoints
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 21:01:32 +03:00
|
|
|
// Stats exposes results raw data.
|
|
|
|
type Stats struct {
|
|
|
|
AvgTotal float64
|
|
|
|
Fastest float64
|
|
|
|
Slowest float64
|
|
|
|
Average float64
|
|
|
|
Stddev float64
|
|
|
|
RPS float64
|
|
|
|
Total time.Duration
|
|
|
|
ErrorDist map[string]int
|
|
|
|
Lats []float64
|
|
|
|
TimeSeries TimeSeries
|
|
|
|
}
|
|
|
|
|
2017-05-18 02:05:08 +03:00
|
|
|
func (s *Stats) copy() Stats {
|
|
|
|
ss := *s
|
|
|
|
ss.ErrorDist = copyMap(ss.ErrorDist)
|
|
|
|
ss.Lats = copyFloats(ss.Lats)
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:58:24 +03:00
|
|
|
// Report processes a result stream until it is closed, then produces a
|
|
|
|
// string with information about the consumed result data.
|
|
|
|
type Report interface {
|
|
|
|
Results() chan<- Result
|
2017-01-13 21:01:32 +03:00
|
|
|
|
|
|
|
// Run returns results in print-friendly format.
|
2016-10-07 01:58:24 +03:00
|
|
|
Run() <-chan string
|
2017-01-13 21:01:32 +03:00
|
|
|
|
|
|
|
// Stats returns results in raw data.
|
|
|
|
Stats() <-chan Stats
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-18 02:05:08 +03:00
|
|
|
func NewReport(precision string) Report { return newReport(precision) }
|
|
|
|
|
|
|
|
func newReport(precision string) *report {
|
|
|
|
r := &report{
|
2016-10-07 01:58:24 +03:00
|
|
|
results: make(chan Result, 16),
|
|
|
|
precision: precision,
|
|
|
|
}
|
2017-05-18 02:05:08 +03:00
|
|
|
r.stats.ErrorDist = make(map[string]int)
|
|
|
|
return r
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReportSample(precision string) Report {
|
|
|
|
r := NewReport(precision).(*report)
|
|
|
|
r.sps = newSecondPoints()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) Results() chan<- Result { return r.results }
|
|
|
|
|
|
|
|
func (r *report) Run() <-chan string {
|
|
|
|
donec := make(chan string, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(donec)
|
|
|
|
r.processResults()
|
|
|
|
donec <- r.String()
|
|
|
|
}()
|
|
|
|
return donec
|
|
|
|
}
|
|
|
|
|
2017-01-13 21:01:32 +03:00
|
|
|
func (r *report) Stats() <-chan Stats {
|
|
|
|
donec := make(chan Stats, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(donec)
|
|
|
|
r.processResults()
|
2017-05-18 02:05:08 +03:00
|
|
|
s := r.stats.copy()
|
2017-01-17 23:51:47 +03:00
|
|
|
if r.sps != nil {
|
2017-05-18 02:05:08 +03:00
|
|
|
s.TimeSeries = r.sps.getTimeSeries()
|
2017-01-13 21:01:32 +03:00
|
|
|
}
|
2017-05-18 02:05:08 +03:00
|
|
|
donec <- s
|
2017-01-13 21:01:32 +03:00
|
|
|
}()
|
|
|
|
return donec
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyMap(m map[string]int) (c map[string]int) {
|
|
|
|
c = make(map[string]int, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
c[k] = v
|
|
|
|
}
|
2017-11-11 05:28:57 +03:00
|
|
|
return c
|
2017-01-13 21:01:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyFloats(s []float64) (c []float64) {
|
|
|
|
c = make([]float64, len(s))
|
|
|
|
copy(c, s)
|
2017-11-11 05:28:57 +03:00
|
|
|
return c
|
2017-01-13 21:01:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-07 01:58:24 +03:00
|
|
|
func (r *report) String() (s string) {
|
2017-05-18 02:05:08 +03:00
|
|
|
if len(r.stats.Lats) > 0 {
|
2016-10-07 01:58:24 +03:00
|
|
|
s += fmt.Sprintf("\nSummary:\n")
|
2017-05-18 02:05:08 +03:00
|
|
|
s += fmt.Sprintf(" Total:\t%s.\n", r.sec2str(r.stats.Total.Seconds()))
|
|
|
|
s += fmt.Sprintf(" Slowest:\t%s.\n", r.sec2str(r.stats.Slowest))
|
|
|
|
s += fmt.Sprintf(" Fastest:\t%s.\n", r.sec2str(r.stats.Fastest))
|
|
|
|
s += fmt.Sprintf(" Average:\t%s.\n", r.sec2str(r.stats.Average))
|
|
|
|
s += fmt.Sprintf(" Stddev:\t%s.\n", r.sec2str(r.stats.Stddev))
|
|
|
|
s += fmt.Sprintf(" Requests/sec:\t"+r.precision+"\n", r.stats.RPS)
|
2016-10-07 01:58:24 +03:00
|
|
|
s += r.histogram()
|
2017-01-11 05:15:53 +03:00
|
|
|
s += r.sprintLatencies()
|
2016-10-07 01:58:24 +03:00
|
|
|
if r.sps != nil {
|
|
|
|
s += fmt.Sprintf("%v\n", r.sps.getTimeSeries())
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 02:05:08 +03:00
|
|
|
if len(r.stats.ErrorDist) > 0 {
|
2016-10-07 01:58:24 +03:00
|
|
|
s += r.errors()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) sec2str(sec float64) string { return fmt.Sprintf(r.precision+" secs", sec) }
|
|
|
|
|
|
|
|
type reportRate struct{ *report }
|
|
|
|
|
|
|
|
func NewReportRate(precision string) Report {
|
|
|
|
return &reportRate{NewReport(precision).(*report)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *reportRate) String() string {
|
2017-05-18 02:05:08 +03:00
|
|
|
return fmt.Sprintf(" Requests/sec:\t"+r.precision+"\n", r.stats.RPS)
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) processResult(res *Result) {
|
|
|
|
if res.Err != nil {
|
2017-05-18 02:05:08 +03:00
|
|
|
r.stats.ErrorDist[res.Err.Error()]++
|
2016-10-07 01:58:24 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dur := res.Duration()
|
2017-05-18 02:05:08 +03:00
|
|
|
r.stats.Lats = append(r.stats.Lats, dur.Seconds())
|
|
|
|
r.stats.AvgTotal += dur.Seconds()
|
2016-10-07 01:58:24 +03:00
|
|
|
if r.sps != nil {
|
|
|
|
r.sps.Add(res.Start, dur)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) processResults() {
|
|
|
|
st := time.Now()
|
|
|
|
for res := range r.results {
|
|
|
|
r.processResult(&res)
|
|
|
|
}
|
2017-05-18 02:05:08 +03:00
|
|
|
r.stats.Total = time.Since(st)
|
2016-10-07 01:58:24 +03:00
|
|
|
|
2017-05-18 02:05:08 +03:00
|
|
|
r.stats.RPS = float64(len(r.stats.Lats)) / r.stats.Total.Seconds()
|
|
|
|
r.stats.Average = r.stats.AvgTotal / float64(len(r.stats.Lats))
|
|
|
|
for i := range r.stats.Lats {
|
|
|
|
dev := r.stats.Lats[i] - r.stats.Average
|
|
|
|
r.stats.Stddev += dev * dev
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
2017-05-18 02:05:08 +03:00
|
|
|
r.stats.Stddev = math.Sqrt(r.stats.Stddev / float64(len(r.stats.Lats)))
|
|
|
|
sort.Float64s(r.stats.Lats)
|
|
|
|
if len(r.stats.Lats) > 0 {
|
|
|
|
r.stats.Fastest = r.stats.Lats[0]
|
|
|
|
r.stats.Slowest = r.stats.Lats[len(r.stats.Lats)-1]
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 05:15:53 +03:00
|
|
|
var pctls = []float64{10, 25, 50, 75, 90, 95, 99, 99.9}
|
|
|
|
|
2017-01-13 21:01:32 +03:00
|
|
|
// Percentiles returns percentile distribution of float64 slice.
|
|
|
|
func Percentiles(nums []float64) (pcs []float64, data []float64) {
|
|
|
|
return pctls, percentiles(nums)
|
|
|
|
}
|
|
|
|
|
2017-01-11 05:15:53 +03:00
|
|
|
func percentiles(nums []float64) (data []float64) {
|
|
|
|
data = make([]float64, len(pctls))
|
2016-10-07 01:58:24 +03:00
|
|
|
j := 0
|
2017-01-11 05:15:53 +03:00
|
|
|
n := len(nums)
|
|
|
|
for i := 0; i < n && j < len(pctls); i++ {
|
|
|
|
current := float64(i) * 100.0 / float64(n)
|
2016-10-07 01:58:24 +03:00
|
|
|
if current >= pctls[j] {
|
2017-01-11 05:15:53 +03:00
|
|
|
data[j] = nums[i]
|
2016-10-07 01:58:24 +03:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 05:28:57 +03:00
|
|
|
return data
|
2017-01-11 05:15:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) sprintLatencies() string {
|
2017-05-18 02:05:08 +03:00
|
|
|
data := percentiles(r.stats.Lats)
|
2016-10-07 01:58:24 +03:00
|
|
|
s := fmt.Sprintf("\nLatency distribution:\n")
|
|
|
|
for i := 0; i < len(pctls); i++ {
|
|
|
|
if data[i] > 0 {
|
|
|
|
s += fmt.Sprintf(" %v%% in %s.\n", pctls[i], r.sec2str(data[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) histogram() string {
|
|
|
|
bc := 10
|
|
|
|
buckets := make([]float64, bc+1)
|
|
|
|
counts := make([]int, bc+1)
|
2017-05-18 02:05:08 +03:00
|
|
|
bs := (r.stats.Slowest - r.stats.Fastest) / float64(bc)
|
2016-10-07 01:58:24 +03:00
|
|
|
for i := 0; i < bc; i++ {
|
2017-05-18 02:05:08 +03:00
|
|
|
buckets[i] = r.stats.Fastest + bs*float64(i)
|
2016-10-07 01:58:24 +03:00
|
|
|
}
|
2017-05-18 02:05:08 +03:00
|
|
|
buckets[bc] = r.stats.Slowest
|
2016-10-07 01:58:24 +03:00
|
|
|
var bi int
|
|
|
|
var max int
|
2017-05-18 02:05:08 +03:00
|
|
|
for i := 0; i < len(r.stats.Lats); {
|
|
|
|
if r.stats.Lats[i] <= buckets[bi] {
|
2016-10-07 01:58:24 +03:00
|
|
|
i++
|
|
|
|
counts[bi]++
|
|
|
|
if max < counts[bi] {
|
|
|
|
max = counts[bi]
|
|
|
|
}
|
|
|
|
} else if bi < len(buckets)-1 {
|
|
|
|
bi++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s := fmt.Sprintf("\nResponse time histogram:\n")
|
|
|
|
for i := 0; i < len(buckets); i++ {
|
|
|
|
// Normalize bar lengths.
|
|
|
|
var barLen int
|
|
|
|
if max > 0 {
|
|
|
|
barLen = counts[i] * 40 / max
|
|
|
|
}
|
|
|
|
s += fmt.Sprintf(" "+r.precision+" [%v]\t|%v\n", buckets[i], counts[i], strings.Repeat(barChar, barLen))
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *report) errors() string {
|
|
|
|
s := fmt.Sprintf("\nError distribution:\n")
|
2017-05-18 02:05:08 +03:00
|
|
|
for err, num := range r.stats.ErrorDist {
|
2016-10-07 01:58:24 +03:00
|
|
|
s += fmt.Sprintf(" [%d]\t%s\n", num, err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|