-
Notifications
You must be signed in to change notification settings - Fork 8
/
timeline.go
102 lines (86 loc) · 2.4 KB
/
timeline.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
package tachymeter
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
)
// Timeline holds a []*timelineEvents,
// which nest *Metrics for analyzing
// multiple collections of measured events.
type Timeline struct {
timeline []*timelineEvent
}
// timelineEvent holds a *Metrics and
// time that it was added to the Timeline.
type timelineEvent struct {
Metrics *Metrics
Created time.Time
}
// AddEvent adds a *Metrics to the *Timeline.
func (t *Timeline) AddEvent(m *Metrics) {
t.timeline = append(t.timeline, &timelineEvent{
Metrics: m,
Created: time.Now(),
})
}
// WriteHTML takes an absolute path p and writes an
// html file to 'p/tachymeter-<timestamp>.html' of all
// histograms held by the *Timeline, in series.
func (t *Timeline) WriteHTML(p string) error {
path, err := filepath.Abs(p)
if err != nil {
return err
}
var b bytes.Buffer
b.WriteString(head)
// Append graph + info entry for each timeline
// event.
for n := range t.timeline {
// Graph div.
b.WriteString(fmt.Sprintf(`%s<div class="graph">%s`, tab, nl))
b.WriteString(fmt.Sprintf(`%s%s<canvas id="canvas-%d"></canvas>%s`, tab, tab, n, nl))
b.WriteString(fmt.Sprintf(`%s</div>%s`, tab, nl))
// Info div.
b.WriteString(fmt.Sprintf(`%s<div class="info">%s`, tab, nl))
b.WriteString(fmt.Sprintf(`%s<p><h2>Iteration %d</h2>%s`, tab, n+1, nl))
b.WriteString(t.timeline[n].Metrics.String())
b.WriteString(fmt.Sprintf("%s%s</p></div>%s", nl, tab, nl))
}
// Write graphs.
for id, m := range t.timeline {
s := genGraphHTML(m, id)
b.WriteString(s)
}
b.WriteString(tail)
// Write file.
d := []byte(b.String())
fname := fmt.Sprintf("%s/tachymeter-%d.html", path, time.Now().Unix())
err = ioutil.WriteFile(fname, d, 0644)
if err != nil {
return err
}
return nil
}
// genGraphHTML takes a *timelineEvent and id (used for each graph
// html element ID) and creates a chart.js graph output.
func genGraphHTML(te *timelineEvent, id int) string {
keys := []string{}
values := []uint64{}
for _, b := range *te.Metrics.Histogram {
for k, v := range b {
keys = append(keys, k)
values = append(values, v)
}
}
keysj, _ := json.Marshal(keys)
valuesj, _ := json.Marshal(values)
out := strings.Replace(graph, "XCANVASID", strconv.Itoa(id), 1)
out = strings.Replace(out, "XKEYS", string(keysj), 1)
out = strings.Replace(out, "XVALUES", string(valuesj), 1)
return out
}