-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveToFile.go
280 lines (253 loc) · 6.63 KB
/
SaveToFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"errors"
"fmt"
"io"
"log"
"math"
"os"
"path/filepath"
"strconv"
"github.com/VividCortex/ewma"
"github.com/eclesh/welford"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
)
func SaveToFile(dbt *DatabaseTable) {
stats := welford.New()
// deletes the first high values in descending order
i := 0
var lastValue float64
for ; i < len(dbt.Data); i++ {
if lastValue == 0.0 || lastValue > dbt.Data[i].Value {
lastValue = dbt.Data[i].Value
} else {
break
}
}
var filename = dbt.Data[0].Timestamp.Format("2006-01-02T15-04-05")
var projectName string
if cliMode {
// filename = dbt.Data[0].Timestamp.Format("2006-01-02T15-04-05")
if f.Name != "" {
filename = f.Name + "_" + filename
projectName = f.Name
} else {
projectName = filename
}
} else {
guiFileName := gui.FileName.Text()
projectName = guiFileName
// filename = dbt.Data[0].Timestamp.Format("2006-01-02T15-04-05")
if guiFileName != "" && guiFileName != filename {
filename = gui.FileName.Text() + "_" + filename
}
}
// CSV
csvFile, err := os.Create(filepath.Join(f.LogFolder, filename+".csv"))
if err != nil {
log.Println(err)
}
_, err = csvFile.Write(append([]byte("time,latency"), []byte{13, 10}...))
if err != nil {
log.Println(err)
}
// https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
SimpleEWMA := ewma.NewMovingAverage()
var highestLatencyRound5 int
round5Map := make(map[int]int)
var XValuesLineChartTime = []string{}
var YValuesLineChartTime = []opts.LineData{}
var benchmarkTime float64
var rawData []float64
for _, v := range dbt.Data {
// CSV
temp := []byte(v.Timestamp.Format("2006-01-02T15:04:05.999999999") + ",")
temp = append(temp, []byte(float64ToString(v.Value))...) // in ms
temp = append(temp, []byte{13, 10}...) // \r\n
_, err := csvFile.Write(temp)
if err != nil {
log.Println(err)
}
// summary
rawData = append(rawData, v.Value)
benchmarkTime += v.Value
SimpleEWMA.Add(v.Value)
stats.Add(v.Value)
// chart, bar
r5 := int(round5(v.Value))
val, ok := round5Map[r5]
if ok {
round5Map[r5] = val + 1
} else {
round5Map[r5] = 1
if highestLatencyRound5 < r5 {
highestLatencyRound5 = r5
}
}
// chart, line
XValuesLineChartTime = append(XValuesLineChartTime, v.Timestamp.Sub(dbt.Data[0].Timestamp).String())
YValuesLineChartTime = append(YValuesLineChartTime, opts.LineData{
Value: v.Value,
})
}
csvFile.Close()
var newFile bool
if _, err := os.Stat(filepath.Join(f.LogFolder, "summary.csv")); errors.Is(err, os.ErrNotExist) {
newFile = true
}
header := "name,start,duration,count,max,min,average,ewma average,stdev\n"
summary := fmt.Sprintf(`%s,%s,%s,%d,%.3f,%.3f,%.3f,%.3f,%.3f`,
projectName,
dbt.Data[0].Timestamp.Format("2006-01-02T15:04:05.999999999"),
dbt.Data[len(dbt.Data)-1].Timestamp.Sub(dbt.Data[0].Timestamp).String(),
len(dbt.Data),
stats.Max(),
stats.Min(),
stats.Mean(),
SimpleEWMA.Value(),
stats.Stddev(),
)
summaryFile, err := os.OpenFile(filepath.Join(f.LogFolder, "summary.csv"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Println(err)
}
if newFile {
if _, err := summaryFile.WriteString(header); err != nil {
log.Println(err)
}
}
if _, err := summaryFile.WriteString(summary + "\n"); err != nil {
log.Println(err)
}
summaryFile.Close()
// Chart
chartXValues := []string{}
for i := 5; i <= highestLatencyRound5; i += 5 {
if i == 5 {
chartXValues = append(chartXValues, "5ms")
} else {
chartXValues = append(chartXValues, strconv.Itoa(i))
}
}
BarItems := make([]opts.BarData, highestLatencyRound5/5+1)
for i := 1; i < len(BarItems); i++ {
val, ok := round5Map[i*5]
if !ok {
BarItems[i-1] = opts.BarData{
Value: 0,
}
} else {
BarItems[i-1] = opts.BarData{
Value: val,
}
}
}
page := components.NewPage().
SetLayout(components.PageFlexLayout).
AddCharts(
createBarChartRound5(
filename,
fmt.Sprintf("start: %s, count: %d, max: %.3f, min: %.3f, average: %.3f, ewma average: %.3f", dbt.Data[0].Timestamp.Format("2006-01-02T15:04:05"), stats.Count(), stats.Max(), stats.Min(), stats.Mean(), SimpleEWMA.Value()),
chartXValues,
BarItems,
),
createLineChartTime(
"time series",
fmt.Sprintf("duration: %s, max: %.3f, min: %.3f, average: %.3f, ewma average: %.3f", dbt.Data[len(dbt.Data)-1].Timestamp.Sub(dbt.Data[0].Timestamp).String(), stats.Max(), stats.Min(), stats.Mean(), SimpleEWMA.Value()),
XValuesLineChartTime,
YValuesLineChartTime,
),
)
chartFile, err := os.Create(filepath.Join(f.LogFolder, filename+".html"))
if err != nil {
log.Println(err)
}
page.Render(io.MultiWriter(chartFile))
chartFile.Close()
}
func createBarChartRound5(title, subtitle string, xValues []string, yValues []opts.BarData) *charts.Bar {
bar := charts.NewBar()
bar.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
PageTitle: "GoSysLat",
Theme: types.ThemeWesteros,
}),
charts.WithTitleOpts(opts.Title{
Title: title,
Subtitle: subtitle,
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "inside",
Start: 0,
End: 100,
XAxisIndex: []int{0},
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "slider",
Start: 0,
End: 100,
XAxisIndex: []int{0},
}),
)
bar.SetXAxis(xValues).
AddSeries("Data", yValues)
return bar
}
func createLineChartTime(title, subtitle string, xValues []string, yValues []opts.LineData) *charts.Line {
line := charts.NewLine()
line.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemeWesteros,
}),
charts.WithTitleOpts(opts.Title{
Title: title,
Subtitle: subtitle,
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "inside",
Start: 0,
End: 100,
XAxisIndex: []int{0},
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "slider",
Start: 0,
End: 100,
XAxisIndex: []int{0},
}),
)
line.SetXAxis(xValues).
AddSeries("Data", yValues).
SetSeriesOptions(
charts.WithLabelOpts(
opts.Label{
Show: true,
},
),
charts.WithAreaStyleOpts(
opts.AreaStyle{
Opacity: 0.2,
},
),
charts.WithLineChartOpts(
opts.LineChart{
Smooth: true,
},
),
)
return line
}
func round5(x float64) float64 {
// x = 1 => 5
// x = 6 => 10
return math.Ceil(x/5) * 5
}
func round(val float64) float64 {
return math.Round(val*100) / 100
}
func roundInt(val float64) int {
return int(math.Round(val))
}