github.com/loov/hrtime@v1.0.3/_example/plotting/main.go (about) 1 // plotting demonstrates how to combine hrtime with plot package. 2 package main 3 4 import ( 5 "fmt" 6 "io/ioutil" 7 "time" 8 9 "github.com/loov/hrtime" 10 "github.com/loov/plot" 11 ) 12 13 func main() { 14 DensityPlot() 15 PercentilesPlot() 16 TimingPlot() 17 StackedPlot() 18 } 19 20 // N is the number of experiments 21 const N = 5000 22 23 // TimingPlot demonstrates how to plot timing values based on the order. 24 func TimingPlot() { 25 fmt.Println("Timing Plot (timing.svg)") 26 27 bench := hrtime.NewBenchmark(N) 28 for bench.Next() { 29 time.Sleep(5000 * time.Nanosecond) 30 } 31 32 seconds := plot.DurationToSeconds(bench.Laps()) 33 34 p := plot.New() 35 p.Margin = plot.R(5, 0, 0, 5) 36 p.AddGroup( 37 plot.NewGrid(), 38 plot.NewGizmo(), 39 plot.NewLine("", plot.Points(nil, seconds)), 40 plot.NewTickLabels(), 41 ) 42 43 svg := plot.NewSVG(800, 300) 44 p.Draw(svg) 45 ioutil.WriteFile("timing.svg", svg.Bytes(), 0755) 46 } 47 48 // DensityPlot demonstrates how to create a density plot from the values. 49 func DensityPlot() { 50 fmt.Println("Density Plot (density.svg)") 51 52 bench := hrtime.NewBenchmark(N) 53 for bench.Next() { 54 time.Sleep(5000 * time.Nanosecond) 55 } 56 57 seconds := plot.DurationToSeconds(bench.Laps()) 58 59 p := plot.New() 60 p.Margin = plot.R(5, 0, 0, 5) 61 p.AddGroup( 62 plot.NewGrid(), 63 plot.NewGizmo(), 64 plot.NewDensity("", seconds), 65 plot.NewTickLabels(), 66 ) 67 68 svg := plot.NewSVG(800, 300) 69 p.Draw(svg) 70 ioutil.WriteFile("density.svg", svg.Bytes(), 0755) 71 } 72 73 // PercentilesPlot demonstrates how to create a percentiles plot from the values. 74 func PercentilesPlot() { 75 fmt.Println("Percentiles Plot (percentiles.svg)") 76 77 bench := hrtime.NewBenchmark(N) 78 for bench.Next() { 79 time.Sleep(5000 * time.Nanosecond) 80 } 81 82 seconds := plot.DurationToSeconds(bench.Laps()) 83 84 p := plot.New() 85 p.Margin = plot.R(5, 0, 0, 5) 86 p.X = plot.NewPercentilesAxis() 87 p.AddGroup( 88 plot.NewGrid(), 89 plot.NewGizmo(), 90 plot.NewPercentiles("", seconds), 91 plot.NewTickLabels(), 92 ) 93 94 svg := plot.NewSVG(800, 300) 95 p.Draw(svg) 96 ioutil.WriteFile("percentiles.svg", svg.Bytes(), 0755) 97 } 98 99 // StackedPlot demonstrates how to combine plots 100 func StackedPlot() { 101 fmt.Println("Stacked Plot (stacked.svg)") 102 103 bench := hrtime.NewBenchmark(N) 104 for bench.Next() { 105 time.Sleep(5000 * time.Nanosecond) 106 } 107 108 p := plot.New() 109 stack := plot.NewVStack() 110 stack.Margin = plot.R(5, 5, 5, 5) 111 p.Add(stack) 112 113 seconds := plot.DurationToSeconds(bench.Laps()) 114 115 stack.Add(plot.NewAxisGroup( 116 plot.NewGrid(), 117 plot.NewGizmo(), 118 plot.NewLine("", plot.Points(nil, seconds)), 119 plot.NewTickLabels(), 120 )) 121 122 stack.Add(plot.NewAxisGroup( 123 plot.NewGrid(), 124 plot.NewGizmo(), 125 plot.NewDensity("", seconds), 126 plot.NewTickLabels(), 127 )) 128 129 percentiles := plot.NewAxisGroup( 130 plot.NewGrid(), 131 plot.NewGizmo(), 132 plot.NewPercentiles("", seconds), 133 plot.NewTickLabels(), 134 ) 135 percentiles.X = plot.NewPercentilesAxis() 136 stack.Add(percentiles) 137 138 svg := plot.NewSVG(800, 600) 139 p.Draw(svg) 140 ioutil.WriteFile("stacked.svg", svg.Bytes(), 0755) 141 }