Skip to content

Commit

Permalink
Merge pull request #69 from Tantalor93/latencyplot
Browse files Browse the repository at this point in the history
  • Loading branch information
Tantalor93 committed Aug 14, 2022
2 parents d3a6324 + 50bb954 commit 86b535f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
102 changes: 102 additions & 0 deletions cmd/dnspyre/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/miekg/dns"
"github.com/montanaflynn/stats"
"go-hep.org/x/hep/hplot"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
Expand Down Expand Up @@ -153,3 +154,104 @@ func plotLineThroughput(file string, times []Datapoint) {
fmt.Fprintln(os.Stderr, "Failed to save plot.", err)
}
}

type latencyMeasurements struct {
p99 float64
p95 float64
p90 float64
p50 float64
}

func plotLineLatencies(file string, times []Datapoint) {
m := make(map[int64]latencyMeasurements)

timings := make([]float64, 0)
if len(times) != 0 {
first := times[0].Start.Unix()
last := int64(0)

for _, v := range times {
unix := v.Start.Unix() - first
if _, ok := m[unix]; !ok {
m[unix] = latencyMeasurements{}
}
if unix != last {
p99, err := stats.Percentile(timings, 99)
if err != nil {
panic(err)
}
p95, err := stats.Percentile(timings, 95)
if err != nil {
panic(err)
}
p90, err := stats.Percentile(timings, 90)
if err != nil {
panic(err)
}
p50, err := stats.Percentile(timings, 50)
if err != nil {
panic(err)
}
measure := m[unix]
measure.p99 = p99
measure.p95 = p95
measure.p90 = p90
measure.p50 = p50
m[unix] = measure
last = unix
}
timings = append(timings, v.Duration)
}
}

var p99values plotter.XYs
var p95values plotter.XYs
var p90values plotter.XYs
var p50values plotter.XYs

for k, v := range m {
p99values = append(p99values, plotter.XY{X: float64(k), Y: v.p99})
p95values = append(p95values, plotter.XY{X: float64(k), Y: v.p95})
p90values = append(p90values, plotter.XY{X: float64(k), Y: v.p90})
p50values = append(p50values, plotter.XY{X: float64(k), Y: v.p50})
}

less := func(xys plotter.XYs) func(i, j int) bool {
return func(i, j int) bool {
return xys[i].X < xys[j].X
}
}

sort.SliceStable(p99values, less(p99values))
sort.SliceStable(p95values, less(p95values))
sort.SliceStable(p90values, less(p90values))
sort.SliceStable(p50values, less(p50values))

p := plot.New()
p.Title.Text = "Response latencies"
p.X.Label.Text = "Time of test (s)"
p.X.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}
p.Y.Label.Text = "Latency (ms)"
p.Y.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}

plotLine(p, p99values, plotutil.Color(0), "p99")
plotLine(p, p95values, plotutil.Color(1), "p95")
plotLine(p, p90values, plotutil.Color(2), "p90")
plotLine(p, p50values, plotutil.Color(3), "p50")

p.Legend.Top = true

if err := p.Save(6*vg.Inch, 6*vg.Inch, file); err != nil {
fmt.Fprintln(os.Stderr, "Failed to save plot.", err)
}
}

func plotLine(p *plot.Plot, values plotter.XYs, color color.Color, name string) {
l, err := plotter.NewLine(values)
l.Color = color
if err != nil {
panic(err)
}
p.Add(l)
p.Legend.Add(name, l)
}
1 change: 1 addition & 0 deletions cmd/dnspyre/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (b *Benchmark) PrintReport(stats []*ResultStats, t time.Duration) {
plotBoxPlotLatency(b.fileName(dir, "latency-boxplot"), b.Server, times)
plotResponses(b.fileName(dir, "responses-barchart"), codeTotals)
plotLineThroughput(b.fileName(dir, "throughput-lineplot"), times)
plotLineLatencies(b.fileName(dir, "latency-lineplot"), times)
}

if csv != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ generates graphs like these:
![latency boxplot](graphs/latency-boxplot.png)
![respones bar](graphs/responses-barchart.png)
![throughput line](graphs/throughput-lineplot.png)
![latency line](graphs/latency-lineplot.png)
Binary file added docs/graphs/latency-lineplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ require (
github.com/fatih/color v1.13.0
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/miekg/dns v1.1.50
github.com/montanaflynn/stats v0.6.6
github.com/olekukonko/tablewriter v0.0.0-20170719101040-be5337e7b39e
github.com/stretchr/testify v1.8.0
github.com/tantalor93/doh-go v0.1.0
go-hep.org/x/hep v0.31.1
go.uber.org/ratelimit v0.2.0
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
gonum.org/v1/plot v0.11.0
)

require go-hep.org/x/hep v0.31.1

require (
git.sr.ht/~sbinet/gg v0.3.1 // indirect
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/montanaflynn/stats v0.6.6 h1:Duep6KMIDpY4Yo11iFsvyqJDyfzLF9+sndUKT+v64GQ=
github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/olekukonko/tablewriter v0.0.0-20170719101040-be5337e7b39e h1:TXm3cxcAmTO1Gp9W7QK0DeczE8vgXJDUyPPjEXViZ3Y=
Expand Down

0 comments on commit 86b535f

Please sign in to comment.