diff --git a/hack/benchmark/cpu_usage/chart.go b/hack/benchmark/cpu_usage/chart.go index 4c67fdb288..b703dccd8a 100644 --- a/hack/benchmark/cpu_usage/chart.go +++ b/hack/benchmark/cpu_usage/chart.go @@ -20,6 +20,7 @@ import ( "encoding/csv" "fmt" "log" + "math" "os" "runtime" "strconv" @@ -31,6 +32,16 @@ import ( "gonum.org/v1/plot/vg" ) +type integerTicks struct{} + +func (integerTicks) Ticks(min, max float64) []plot.Tick { + var t []plot.Tick + for i := math.Trunc(min); i <= max; i += 50 { + t = append(t, plot.Tick{Value: i, Label: fmt.Sprint(i)}) + } + return t +} + func main() { if err := execute(); err != nil { fmt.Println(err) @@ -106,8 +117,45 @@ func execute() error { } else if runtime.GOOS == "linux" { p.NominalX("OS idle", "minikube kvm2", "minikube virtualbox", "minikube docker", "minikube docker auto-pause", "Docker idle", "k3d", "kind") } + p.X.Label.Text = "Tools" - // output bar graph + // Set data label to each bar + var cpuLabels []string + for i := range results { + cLabel := strconv.FormatFloat(results[i], 'f', -1, 64) + cpuLabels = append(cpuLabels, cLabel) + } + + var xysCPU []plotter.XY + for i := range results { + rxPos := float64(i)-0.13 + ryPos := results[i]+0.1 + cXY := plotter.XY{X: rxPos, Y: ryPos} + xysCPU = append(xysCPU, cXY) + } + // CPU Busy% data label + cl, err := plotter.NewLabels(plotter.XYLabels{ + XYs: xysCPU, + Labels: cpuLabels, + }, + ) + if err != nil { + log.Fatalf("could not creates labels plotter: %+v", err) + } + //for i := range rl.TextStyle { + // rl.TextStyle[i].Color = color.RGBA{R: 255, A: 255} + //} + var t []plot.Tick + for i := math.Trunc(0); i <= 300; i += 50 { + t = append(t, plot.Tick{Value: i, Label: fmt.Sprint(i)}) + } + // define max cpu busy% to 30% + p.Y.Max = 30 + p.Y.Tick.Marker = integerTicks{} + // Add CPU Busy% label to plot + p.Add(cl) + + // Output bar graph if runtime.GOOS == "darwin" { if err := p.Save(13*vg.Inch, 8*vg.Inch, "./site/static/images/benchmarks/cpuUsage/mac.png"); err != nil { return errors.Wrap(err, "Failed to create bar graph png") @@ -120,4 +168,4 @@ func execute() error { log.Printf("Generated graph png to 'site/static/images/benchmarks/cpuUsage/linux.png'") } return nil -} +} \ No newline at end of file diff --git a/hack/benchmark/cpu_usage/test.txt b/hack/benchmark/cpu_usage/test.txt new file mode 100644 index 0000000000..cdcfe72fee --- /dev/null +++ b/hack/benchmark/cpu_usage/test.txt @@ -0,0 +1,113 @@ +package main + +import ( +"fmt" +"image/color" +"log" +"math" +"strconv" + +"gonum.org/v1/plot" +"gonum.org/v1/plot/plotter" +"gonum.org/v1/plot/vg" +) + +type integerTicks struct{} + +func (integerTicks) Ticks(min, max float64) []plot.Tick { + var t []plot.Tick + for i := math.Trunc(min); i <= max; i += 50 { + t = append(t, plot.Tick{Value: i, Label: fmt.Sprint(i)}) + } +return t +} + +func main() { + red := plotter.Values{213, 206, 256, 256, 16, 122, 291} + black := plotter.Values{16, 86, 109, 290, 42, 257, 9} + + days := []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"} + + p := plot.New() + + redC := color.RGBA{R: 255, A: 255} + blackC := color.RGBA{R: 196, G: 196, A: 255} + + w := vg.Points(8) + + redBars, err := plotter.NewBarChart(red, w) + if err != nil { + panic(err) + } + redBars.Color = redC + redBars.Offset = -w + + blackBars, err := plotter.NewBarChart(black, w) + if err != nil { + panic(err) + } + blackBars.Color = blackC + blackBars.Offset = w + + p.Add(blackBars, redBars) + p.NominalX(days...) + + var redLabels []string + var blackLabels []string + + for i := range red { + rLabel := strconv.FormatFloat(red[i], 'f', -1, 64) + bLabel := strconv.FormatFloat(black[i], 'f', -1, 64) + + redLabels = append(redLabels, rLabel) + blackLabels = append(blackLabels, bLabel) + } + + var xysR, xysB []plotter.XY + + for i := range red { + rxPos, bxPos := float64(i)-0.5, float64(i)+0.1 + ryPos, byPos := red[i]+5, black[i]+5 + + rXY := plotter.XY{X: rxPos, Y: ryPos} + bXY := plotter.XY{X: bxPos, Y: byPos} + + xysR = append(xysR, rXY) + xysB = append(xysB, bXY) + } + + rl, err := plotter.NewLabels(plotter.XYLabels{ + XYs: xysR, + Labels: redLabels, + }, + ) + if err != nil { + log.Fatalf("could not creates labels plotter: %+v", err) + } + for i := range rl.TextStyle { + rl.TextStyle[i].Color = color.RGBA{R: 255, A: 255} + //fmt.Println(r, i) + } + + bl, err := plotter.NewLabels(plotter.XYLabels{ + XYs: xysB, + Labels: blackLabels, + }, + ) + if err != nil { + log.Fatalf("could not creates labels plotter: %+v", err) + } + + var t []plot.Tick + for i := math.Trunc(0); i <= 300; i += 50 { + t = append(t, plot.Tick{Value: i, Label: fmt.Sprint(i)}) + } + p.Y.Max = 310 + p.Y.Tick.Marker = integerTicks{} + + p.Add(rl, bl) + + if err := p.Save(4*vg.Inch, 4*vg.Inch, "points.png"); err != nil { + panic(err) + } +} \ No newline at end of file