-
Notifications
You must be signed in to change notification settings - Fork 777
/
picasso.go
242 lines (204 loc) · 4.6 KB
/
picasso.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/wcharczuk/go-chart"
)
var (
title *string
file *string
outputFile *string
outputFormat *string
)
const (
PNG = "png"
SVG = "svg"
)
type (
Tags struct {
Group string `json:"group"`
Iter string `json:"iter"`
Method string `json:"method"`
Name string `json:"name"`
Proto string `json:"proto"`
Status string `json:"status"`
URL string `json:"url"`
Vu string `json:"vu"`
}
Data struct {
Time time.Time `json:"time"`
Value float64 `json:"value"`
Tags Tags `json:"tags"`
}
MetricPoint struct {
Type string `json:"type"`
Data Data `json:"data"`
Metric string `json:"metric"`
}
)
func init() {
title = flag.String("title", "Fission Benchmark", "Chart title")
file = flag.String("file", "", "Metric json file")
outputFile = flag.String("o", "chart.png", "Output file name")
outputFormat = flag.String("format", PNG, "Format of output file (png or svg)")
flag.Parse()
}
func generateContinuousSeries(file string) chart.Series {
f, err := os.OpenFile(file, os.O_RDONLY, 0644)
if err != nil {
fmt.Printf("Failed to open file metric point: %v", err)
return nil
}
defer f.Close()
reader := bufio.NewReader(f)
var xVals []float64
var yVals []float64
var initTime *time.Time
for {
l, _, err := reader.ReadLine()
if err == io.EOF {
break
}
point := &MetricPoint{}
err = json.Unmarshal(l, point)
if err != nil {
fmt.Printf("Failed to parse metric point: %v -> %v", err, string(l))
return nil
}
if point.Type != "Point" || point.Metric != "http_req_duration" {
continue
}
if initTime == nil {
initTime = &point.Data.Time
}
timeSinceStart := point.Data.Time.Sub(*initTime).Seconds()
xVals = append(xVals, timeSinceStart)
yVals = append(yVals, point.Data.Value)
}
return chart.ContinuousSeries{
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultColor(0).WithAlpha(64),
FillColor: chart.GetDefaultColor(0).WithAlpha(64),
},
XValues: xVals,
YValues: yVals,
}
}
func generateChart(title string, file string, format chart.RendererProvider, series []chart.Series) error {
if series == nil {
return errors.New("series cannot be nil")
}
cs := chart.ConcatSeries(series)
graph := chart.Chart{
Title: title,
TitleStyle: chart.StyleShow(),
Background: chart.Style{
Padding: chart.Box{
Top: 50,
Left: 25,
Right: 25,
Bottom: 10,
},
},
XAxis: chart.XAxis{
Name: "Time (s)",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
Range: &chart.ContinuousRange{
Min: 0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.2f s", v.(float64))
},
},
YAxis: chart.YAxis{
Name: "Response Time (ms)",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
Range: &chart.ContinuousRange{
Min: 0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%d ms", int(v.(float64)))
},
},
Series: cs,
}
buffer := bytes.NewBuffer([]byte{})
err := graph.Render(format, buffer)
if err != nil {
return err
}
err = os.WriteFile(file, buffer.Bytes(), 0644)
if err != nil {
return err
}
return nil
}
func listJsonFiles(path string) ([]string, error) {
fi, err := os.Stat(*file)
if err != nil {
return nil, err
}
if fi.Mode().IsRegular() {
return []string{path}, nil
}
var files []string
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".json") && !info.IsDir() {
files = append(files, path)
}
return nil
})
return files, err
}
func main() {
if file == nil || len(*file) == 0 {
fmt.Println("Please provide metric json file name")
return
}
var format chart.RendererProvider
if outputFormat == nil {
format = chart.PNG
} else {
switch strings.ToLower(*outputFormat) {
case PNG:
format = chart.PNG
case SVG:
format = chart.SVG
default:
fmt.Println("Unknown format, use png as output format")
*outputFormat = PNG
}
}
if len(*outputFile) == 0 {
fmt.Println("Please output chart png file name")
return
}
var series []chart.Series
files, err := listJsonFiles(*file)
if err != nil {
fmt.Printf("Failed to get file information: %v", err)
return
}
for _, f := range files {
series = append(series, generateContinuousSeries(f))
}
err = generateChart(*title, *outputFile, format, series)
if err != nil {
fmt.Printf("Failed to generate chart: %v\n", err)
}
}