Debian Codegraph @

benchmarks/report/summary.go

raw
1 package report
2
3 import (
4 "fmt"
5 "html/template"
6 "math"
7 "sort"
8 "strings"
9 )
10
11 // Site is what is rendered from the runs of the machines: the page, the SVG summary, and the files of the runs,
12 // which the page links to.
13 type Site struct {
14 Runs []*Run
15 // Files are the names of the files of the runs, in the order of Runs, which the page links to.
16 Files []string
17 // Attestations is the URL of the attestations of the files, if they are attested.
18 Attestations string
19 }
20
21 // sortRuns orders the runs by architecture, amd64 first.
22 func (s *Site) sortRuns() {
23 idx := make([]int, len(s.Runs))
24 for i := range idx {
25 idx[i] = i
26 }
27 sort.SliceStable(idx, func(a, b int) bool { return s.Runs[idx[a]].GOARCH < s.Runs[idx[b]].GOARCH })
28 runs, files := make([]*Run, len(idx)), make([]string, len(idx))
29 for i, j := range idx {
30 runs[i] = s.Runs[j]
31 if j < len(s.Files) {
32 files[i] = s.Files[j]
33 }
34 }
35 s.Runs, s.Files = runs, files
36 }
37
38 // probesOf returns the descriptions of the probes which a category requires.
39 func probesOf(r *Run, category string) []string {
40 seen := map[string]bool{}
41 var ds []string
42 for _, p := range r.Probes {
43 if !p.Required || seen[p.Probe] {
44 continue
45 }
46 for _, c := range r.Configs {
47 if c.ID == p.Config && c.Category == category {
48 seen[p.Probe] = true
49 ds = append(ds, p.Description)
50 break
51 }
52 }
53 }
54 return ds
55 }
56
57 func failedProbes(r *Run, config string) []ProbeResult {
58 var fs []ProbeResult
59 for _, p := range r.Probes {
60 if p.Config == config && p.Required && !p.Passed {
61 fs = append(fs, p)
62 }
63 }
64 return fs
65 }
66
67 // Summary renders the SVG summary of the runs: for each machine, the speed of every library relative to
68 // encoding/json in the category of the behavior of encoding/json, with a live heap, by the geometric mean over
69 // the payloads.
70 func (s *Site) Summary() string {
71 s.sortRuns()
72 const (
73 width = 920
74 pad = 16
75 rowH = 22
76 labelW = 190
77 barMax = 190
78 panelGap = 24
79 )
80 type bar struct {
81 label string
82 library string
83 ratio float64
84 differs bool
85 }
86 type panel struct {
87 title string
88 bars []bar
89 }
90 var panels []panel
91 var date, goVersion string
92 differing := map[string]bool{}
93 for _, r := range s.Runs {
94 date, goVersion = r.GeneratedAt.Format("2006-01-02"), r.GoVersion
95 for _, op := range []string{OpEncode, OpDecode} {
96 p := panel{title: fmt.Sprintf("%s · %s", opTitle(op), r.GOARCH)}
97 // the configurations which behave as encoding/json first, then the others, marked
98 for _, differs := range []bool{false, true} {
99 for _, c := range r.configsOf("std") {
100 if strings.Contains(c.ID, "/of") || (len(failedProbes(r, c.ID)) > 0) != differs {
101 continue
102 }
103 if ratio := meanRatio(r, "live-heap", op, "std", c.ID); ratio > 0 {
104 label := trimLabel(c.Title)
105 if differs {
106 label += " †"
107 differing[c.Title] = true
108 }
109 p.bars = append(p.bars, bar{label: label, library: c.Library, ratio: ratio, differs: differs})
110 }
111 }
112 }
113 panels = append(panels, p)
114 }
115 }
116 maxRatio := 1.0
117 maxRows := 0
118 for _, p := range panels {
119 for _, b := range p.bars {
120 maxRatio = math.Max(maxRatio, b.ratio)
121 }
122 maxRows = max(maxRows, len(p.bars))
123 }
124 panelH := 28 + maxRows*rowH
125 cols := 2
126 rows := (len(panels) + cols - 1) / cols
127 height := 56 + rows*(panelH+panelGap) + pad
128 if len(differing) > 0 {
129 height += 16
130 }
131 var b strings.Builder
132 fmt.Fprintf(&b, `<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif">`, width, height, width, height)
133 // the colors are fixed, on a background of its own, so that it reads the same on a light and a dark page
134 b.WriteString(`<style>.t{font-size:13px;fill:#1f2328}.m{font-size:11px;fill:#59636e}.h{font-size:14px;font-weight:600;fill:#1f2328}.one{stroke:#8c959f;stroke-dasharray:3 3}</style>`)
135 // the bars of a library are of its color, which the page gives it too; the bars of a configuration which behaves
136 // differently are hatched, as on the page
137 b.WriteString(`<defs>`)
138 for i, c := range libraryColors {
139 fmt.Fprintf(&b, `<pattern id="h%d" width="6" height="6" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"><rect width="6" height="6" fill="%s" fill-opacity="0.45"/><rect width="3" height="6" fill="%s"/></pattern>`, i, c.color, c.color)
140 }
141 b.WriteString(`</defs>`)
142 fmt.Fprintf(&b, `<rect x="0.5" y="0.5" width="%d" height="%d" rx="6" fill="#ffffff" stroke="#d1d9e0"/>`, width-1, height-1)
143 fmt.Fprintf(&b, `<text class="h" x="%d" y="%d">Speed relative to encoding/json, with the same behavior as encoding/json ( higher is faster )</text>`, pad, pad+14)
144 fmt.Fprintf(&b, `<text class="m" x="%d" y="%d">%s · %s · with a live heap of 64 MB · geometric mean over the payloads</text>`, pad, pad+32, date, template.HTMLEscapeString(goVersion))
145 for i, p := range panels {
146 x := pad + (i%cols)*((width-2*pad)/cols)
147 y := pad + 44 + (i/cols)*(panelH+panelGap)
148 fmt.Fprintf(&b, `<text class="h" x="%d" y="%d">%s</text>`, x, y+16, template.HTMLEscapeString(p.title))
149 for j, bb := range p.bars {
150 yy := y + 28 + j*rowH
151 w := float64(barMax) * bb.ratio / maxRatio
152 i := libraryColorIndex(bb.library)
153 fill := libraryColors[i].color
154 if bb.differs {
155 fill = fmt.Sprintf("url(#h%d)", i)
156 }
157 fmt.Fprintf(&b, `<text class="t" x="%d" y="%d">%s</text>`, x, yy+14, template.HTMLEscapeString(bb.label))
158 fmt.Fprintf(&b, `<rect x="%d" y="%d" width="%.1f" height="14" rx="2" fill="%s"/>`, x+labelW, yy+3, w, fill)
159 fmt.Fprintf(&b, `<text class="t" x="%.1f" y="%d">%.2fx</text>`, float64(x+labelW)+w+6, yy+14, bb.ratio)
160 }
161 if len(p.bars) > 0 {
162 // the line of the speed of encoding/json
163 one := float64(x+labelW) + float64(barMax)/maxRatio
164 fmt.Fprintf(&b, `<line class="one" x1="%.1f" y1="%d" x2="%.1f" y2="%d"/>`, one, y+26, one, y+28+len(p.bars)*rowH)
165 }
166 }
167 if len(differing) > 0 {
168 var names []string
169 for n := range differing {
170 names = append(names, n)
171 }
172 sort.Strings(names)
173 fmt.Fprintf(&b, `<text class="m" x="%d" y="%d">† %s: behaves differently from encoding/json, so not the same work ( see the page for what differs )</text>`,
174 pad, height-pad, template.HTMLEscapeString(strings.Join(names, ", ")))
175 }
176 b.WriteString(`</svg>`)
177 return b.String()
178 }
179
180 // libraryColors are the colors of the libraries, the ones of the page ( page.html ) on a light background.
181 var libraryColors = []struct{ library, color string }{
182 {"encoding/json", "#8c959f"},
183 {"encoding/json/v2", "#57606a"},
184 {"goccy/go-json", "#0969da"},
185 {"bytedance/sonic", "#e16f24"},
186 {"json-iterator/go", "#1a7f37"},
187 {"segmentio/encoding", "#8250df"},
188 {"", "#59636e"},
189 }
190
191 // libraryColorIndex returns the index of the color of the library in libraryColors: the last one for a library
192 // which has none.
193 func libraryColorIndex(library string) int {
194 for i, c := range libraryColors {
195 if c.library == library {
196 return i
197 }
198 }
199 return len(libraryColors) - 1
200 }
201
202 func opTitle(op string) string {
203 if op == OpEncode {
204 return "Encode"
205 }
206 return "Decode"
207 }
208
209 func trimLabel(s string) string {
210 s = strings.TrimSuffix(s, " ( DefaultOptionsV1 )")
211 if s == "encoding/json/v2" {
212 return "encoding/json/v2 ( v1 )"
213 }
214 return s
215 }
216
217 // meanRatio returns the geometric mean over the payloads of the speed of a configuration relative to the baseline
218 // of the category, or 0 if it is measured on none of them.
219 func meanRatio(r *Run, cond, op, category, config string) float64 {
220 var base string
221 for _, c := range r.Categories {
222 if c.ID == category {
223 base = c.Baseline
224 }
225 }
226 var logSum float64
227 var n int
228 for _, p := range r.Payloads {
229 b := r.result(cond, op, p.ID, base)
230 x := r.result(cond, op, p.ID, config)
231 if b == nil || x == nil {
232 continue
233 }
234 logSum += math.Log(b.NsPerOp / x.NsPerOp)
235 n++
236 }
237 if n == 0 {
238 return 0
239 }
240 return math.Exp(logSum / float64(n))
241 }