fat-contexts-article-companion/plot/main.go

82 lines
1.4 KiB
Go
Raw Normal View History

2024-03-17 11:33:29 +01:00
package main
import (
"encoding/csv"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"io"
"log"
"os"
"strconv"
)
func main() {
2025-01-13 14:51:40 +01:00
thin, fat, experiment := getData()
2024-03-17 11:33:29 +01:00
plot := charts.NewLine()
2025-01-13 14:51:40 +01:00
xAxis := make([]int, len(thin))
2024-03-17 11:33:29 +01:00
for i := range xAxis {
xAxis[i] = i
}
plot.SetGlobalOptions(
charts.WithXAxisOpts(opts.XAxis{Name: "loop count"}),
charts.WithYAxisOpts(opts.YAxis{Name: "Nanosecond"}),
)
plot.
SetXAxis(xAxis).
2025-01-13 14:51:40 +01:00
AddSeries("Thin", thin).
AddSeries("Fat", fat).
AddSeries("Experiment", experiment)
2024-03-17 11:33:29 +01:00
f, err := os.Create("plot.html")
if err != nil {
panic(err)
}
err = plot.Render(f)
if err != nil {
panic(err)
}
}
2025-01-13 14:51:40 +01:00
func getData() ([]opts.LineData, []opts.LineData, []opts.LineData) {
var thin []opts.LineData
2024-03-17 11:33:29 +01:00
var fat []opts.LineData
2025-01-13 14:51:40 +01:00
var experiment []opts.LineData
2024-03-17 11:33:29 +01:00
f, err := os.Open("data")
if err != nil {
panic(err)
}
r := csv.NewReader(f)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
series := record[0]
val, err := strconv.ParseUint(record[1], 10, 64)
if err != nil {
log.Fatal(err)
}
point := opts.LineData{Value: val, Name: "ns"}
switch series {
2025-01-13 14:51:40 +01:00
case "thin":
thin = append(thin, point)
2024-03-17 11:33:29 +01:00
case "fat":
fat = append(fat, point)
2025-01-13 14:51:40 +01:00
case "experiment":
experiment = append(experiment, point)
2024-03-17 11:33:29 +01:00
}
}
2025-01-13 14:51:40 +01:00
return thin, fat, experiment
2024-03-17 11:33:29 +01:00
}