Skip to content

Commit

Permalink
Merge pull request #68 from Tantalor93/betterplots
Browse files Browse the repository at this point in the history
  • Loading branch information
Tantalor93 committed Aug 13, 2022
2 parents 8a90dc5 + 75c453c commit d3a6324
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ vendor/
bin/
report/
dnspyre

graphs*/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ Flags:
--silent Disable stdout.
--color ANSI Color output.
--plot=/path/to/folder Plot benchmark results and export them to directory.
--plotf=png Format of graphs. Supported formats png, svg, pdf.
--doh-method=post HTTP method to use for DoH requests
--doh-protocol=1.1 HTTP protocol to use for DoH requests
--plotf=png Format of graphs. Supported formats: png, jpg.
--doh-method=post HTTP method to use for DoH requests. Supported values: get, post.
--doh-protocol=1.1 HTTP protocol to use for DoH requests. Supported values: 1.1, 2.
-d, --duration=1m Specifies for how long the benchmark should be executing, the benchmark will run for the specified time while sending DNS requests in infinite loop based on data source. After running for specified duration, the benchmark
is cancelled. This option is exclusive with --number option. The duration is specified in GO duration format e.g. 10s, 15m, 1h.
--version Show application version.
Expand Down
74 changes: 55 additions & 19 deletions cmd/dnspyre/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"sort"

"github.com/miekg/dns"
"go-hep.org/x/hep/hplot"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)

Expand All @@ -24,8 +26,11 @@ func plotHistogramLatency(file string, times []Datapoint) {
if err != nil {
panic(err)
}
p.X.Label.Text = "latencies (ms)"
p.Y.Label.Text = "number of requests"
p.X.Label.Text = "Latencies (ms)"
p.X.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}
p.Y.Label.Text = "Number of requests"
p.Y.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}
hist.FillColor = color.RGBA{R: 175, G: 238, B: 238}
p.Add(hist)

if err := p.Save(6*vg.Inch, 6*vg.Inch, file); err != nil {
Expand All @@ -40,13 +45,15 @@ func plotBoxPlotLatency(file, server string, times []Datapoint) {
}
p := plot.New()
p.Title.Text = "Latencies distribution"
p.Y.Label.Text = "latencies (ms)"
p.Y.Label.Text = "Latencies (ms)"
p.Y.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}
p.NominalX(server)

boxplot, err := plotter.NewBoxPlot(vg.Length(120), 0, values)
if err != nil {
panic(err)
}
boxplot.FillColor = color.RGBA{R: 127, G: 188, B: 165, A: 1}
p.Add(boxplot)

if err := p.Save(6*vg.Inch, 6*vg.Inch, file); err != nil {
Expand All @@ -55,23 +62,49 @@ func plotBoxPlotLatency(file, server string, times []Datapoint) {
}

func plotResponses(file string, rcodes map[int]int64) {
var values plotter.Values
var names []string
for k, v := range rcodes {
values = append(values, float64(v))
names = append(names, dns.RcodeToString[k])
sortedKeys := make([]int, 0)
for k := range rcodes {
sortedKeys = append(sortedKeys, k)
}
p := plot.New()
p.Title.Text = "Responses distribution"
sort.Ints(sortedKeys)

colors := []color.Color{
color.RGBA{R: 122, G: 195, B: 106},
color.RGBA{R: 241, G: 90, B: 96},
color.RGBA{R: 90, G: 155, B: 212},
color.RGBA{R: 250, G: 167, B: 91},
color.RGBA{R: 158, G: 103, B: 171},
color.RGBA{R: 206, G: 112, B: 88},
color.RGBA{R: 215, G: 127, B: 180},
}
colors = append(colors, plotutil.DarkColors...)

barchart, err := plotter.NewBarChart(values, vg.Length(60))
if err != nil {
panic(err)
p := plot.New()
p.Title.Text = "Response code distribution"
p.NominalX("Response codes")

width := vg.Points(40)

c := 0
off := -vg.Length(len(rcodes)/2) * width
fmt.Println(sortedKeys)
for _, v := range sortedKeys {
fmt.Println(v)
bar, err := plotter.NewBarChart(plotter.Values{float64(rcodes[v])}, width)
if err != nil {
panic(err)
}
p.Legend.Add(dns.RcodeToString[v], bar)
bar.Color = colors[c%len(colors)]
bar.Offset = off
p.Add(bar)
c++
off += width
}
p.Add(barchart)
p.NominalX(names...)
p.Y.Label.Text = "number of requests"
barchart.Color = color.Gray{Y: 128}

p.Y.Label.Text = "Number of requests"
p.Y.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}
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)
Expand Down Expand Up @@ -104,10 +137,13 @@ func plotLineThroughput(file string, times []Datapoint) {

p := plot.New()
p.Title.Text = "Throughput per second"
p.X.Label.Text = "time of test (s)"
p.Y.Label.Text = "number of requests (per sec)"
p.X.Label.Text = "Time of test (s)"
p.X.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}
p.Y.Label.Text = "Number of requests (per sec)"
p.Y.Tick.Marker = hplot.Ticks{N: 10, Format: "%.0f"}

l, err := plotter.NewLine(values)
l.Color = color.RGBA{R: 255, A: 255}
if err != nil {
panic(err)
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/dnspyre/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ func (b *Benchmark) PrintReport(stats []*ResultStats, t time.Duration) {
if len(b.PlotDir) != 0 {
now := time.Now()
unix := now.Unix()
plotHistogramLatency(b.getFileName("latency-hist", unix), times)
plotBoxPlotLatency(b.getFileName("latency-box", unix), b.Server, times)
plotResponses(b.getFileName("responses-bar", unix), codeTotals)
plotLineThroughput(b.getFileName("throughput-line", unix), times)
dir := fmt.Sprintf("%s/graphs-%d", b.PlotDir, unix)
if err := os.Mkdir(dir, os.ModePerm); err != nil {
panic(err)
}
plotHistogramLatency(b.fileName(dir, "latency-histogram"), times)
plotBoxPlotLatency(b.fileName(dir, "latency-boxplot"), b.Server, times)
plotResponses(b.fileName(dir, "responses-barchart"), codeTotals)
plotLineThroughput(b.fileName(dir, "throughput-lineplot"), times)
}

if csv != nil {
Expand Down Expand Up @@ -183,8 +187,8 @@ func (b *Benchmark) PrintReport(stats []*ResultStats, t time.Duration) {
}
}

func (b *Benchmark) getFileName(filePrefix string, unix int64) string {
return b.PlotDir + "/" + filePrefix + "-" + strconv.FormatInt(unix, 10) + "." + b.PlotFormat
func (b *Benchmark) fileName(dir, name string) string {
return dir + "/" + name + "." + b.PlotFormat
}

func writeBars(f *os.File, bars []hdrhistogram.Bar) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dnspyre/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
pColor = pApp.Flag("color", "ANSI Color output.").Default("true").Bool()

pPlotDir = pApp.Flag("plot", "Plot benchmark results and export them to directory.").Default("").PlaceHolder("/path/to/folder").String()
pPlotFormat = pApp.Flag("plotf", "Format of graphs. Supported formats png, svg, pdf.").Default("png").Enum("png", "svg", "pdf")
pPlotFormat = pApp.Flag("plotf", "Format of graphs. Supported formats: png, jpg.").Default("png").Enum("png", "jpg")

pDoHmethod = pApp.Flag("doh-method", "HTTP method to use for DoH requests. Supported values: get, post.").Default("post").Enum("get", "post")
pDoHProtocol = pApp.Flag("doh-protocol", "HTTP protocol to use for DoH requests. Supported values: 1.1, 2.").Default("1.1").Enum("1.1", "2")
Expand Down
10 changes: 5 additions & 5 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ dnspyre -c 2 --server 'https://1.1.1.1/dns-query' --doh-protocol 2 --recurse goo
```

### Plotting graphs
plots benchmark results as histograms, boxplots and line graphs to the current directory
plots benchmark results as histograms, boxplots and line graphs to the new subdirectory `graphs-<current unix timestamp>` in the current directory
```
dnspyre -d 30s -c 2 --server 8.8.8.8 --plot . --recurse https://raw.githubusercontent.com/Tantalor93/dnspyre/master/data/1000-domains
```
generates graphs like these:
![latency histogram](graphs/latency-hist-1660333390.png)
![latency boxplot](graphs/latency-box-1660333390.png)
![respones bar](graphs/responses-bar-1660333390.png)
![throughput line](graphs/throughput-line-1660333390.png)
![latency histogram](graphs/latency-histogram.png)
![latency boxplot](graphs/latency-boxplot.png)
![respones bar](graphs/responses-barchart.png)
![throughput line](graphs/throughput-lineplot.png)
Binary file removed docs/graphs/latency-box-1660333390.png
Binary file not shown.
Binary file added docs/graphs/latency-boxplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/graphs/latency-hist-1660333390.png
Binary file not shown.
Binary file added docs/graphs/latency-histogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/graphs/responses-bar-1660333390.png
Binary file not shown.
Binary file added docs/graphs/responses-barchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/graphs/throughput-line-1660333390.png
Binary file not shown.
Binary file added docs/graphs/throughput-lineplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 14 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,43 @@ require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/fatih/color v1.13.0
github.com/mattn/go-runewidth v0.0.2 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/miekg/dns v1.1.50
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.uber.org/ratelimit v0.2.0
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678
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
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/campoy/embedmd v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-fonts/liberation v0.2.0 // indirect
github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81 // indirect
github.com/go-pdf/fpdf v0.6.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/gonuts/binary v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
golang.org/x/mod v0.4.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/exp v0.0.0-20210220032938-85be41e4509f // indirect
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit d3a6324

Please sign in to comment.