Debian Codegraph @

benchmarks/report/report.go

raw
1 // Package report holds the results of the benchmark report of go-json, which compares the JSON libraries of Go
2 // doing the same work, and renders them as an HTML page and an SVG summary.
3 //
4 // A run is measured on one machine ( see report_test.go of the benchmarks ): every library of a comparison is
5 // measured there, by turns, so that the libraries are compared on the same CPU. The runs of the machines are
6 // rendered together.
7 package report
8
9 import (
10 "encoding/json"
11 "fmt"
12 "os"
13 "time"
14 )
15
16 // Run is what a run of the report measured on one machine.
17 type Run struct {
18 GeneratedAt time.Time `json:"generatedAt"`
19 GoVersion string `json:"goVersion"`
20 GOOS string `json:"goos"`
21 GOARCH string `json:"goarch"`
22 CPU string `json:"cpu"`
23 // CPUFeatures are the extensions of the instruction set of the CPU which libraries choose their code by, as
24 // AVX2 and AVX-512: a library may be several times faster on a CPU with one than on one without.
25 CPUFeatures []string `json:"cpuFeatures"`
26 // Commit, Repository and RunURL tell where the run comes from: the commit of go-json measured, the repository
27 // it is in and the workflow run which measured it.
28 Commit string `json:"commit"`
29 Repository string `json:"repository"`
30 RunURL string `json:"runURL"`
31 BenchTime string `json:"benchTime"`
32 Rounds int `json:"rounds"`
33
34 Libraries []Library `json:"libraries"`
35 Categories []Category `json:"categories"`
36 Conditions []Condition `json:"conditions"`
37 Payloads []Payload `json:"payloads"`
38 Configs []Config `json:"configs"`
39 Probes []ProbeResult `json:"probes"`
40 Results []Result `json:"results"`
41 // Differences are the payloads on which the result of a configuration is not the one of its category: it is
42 // measured, and shown with the difference.
43 Differences []Exclusion `json:"differences"`
44 // Exclusions are the payloads on which an operation of a configuration fails, which is not measured.
45 Exclusions []Exclusion `json:"exclusions"`
46 }
47
48 // Library is a library measured, with the version the run was built with.
49 type Library struct {
50 Name string `json:"name"`
51 Module string `json:"module"`
52 Version string `json:"version"`
53 }
54
55 // Category is a set of configurations which behave the same: only the configurations of a category are compared
56 // with each other.
57 type Category struct {
58 ID string `json:"id"`
59 Title string `json:"title"`
60 Description string `json:"description"`
61 // Baseline is the configuration which the others of the category are compared with.
62 Baseline string `json:"baseline"`
63 }
64
65 // Condition is a condition of the process which the benchmarks are measured in.
66 type Condition struct {
67 ID string `json:"id"`
68 Title string `json:"title"`
69 Description string `json:"description"`
70 }
71
72 // Payload is an input of the benchmarks: it is decoded into its Go type, and the value is encoded.
73 type Payload struct {
74 ID string `json:"id"`
75 Title string `json:"title"`
76 Description string `json:"description"`
77 Bytes int `json:"bytes"`
78 }
79
80 // Config is a library called in one way: the functions and the options it is called with.
81 type Config struct {
82 ID string `json:"id"`
83 Library string `json:"library"`
84 Category string `json:"category"`
85 // Title is the name of the configuration on the page, and Setting how the library is called.
86 Title string `json:"title"`
87 Setting string `json:"setting"`
88 }
89
90 // ProbeResult is whether a configuration behaves as its category requires on a probe: a small input which tells a
91 // behavior apart. A configuration which fails a required probe is still measured, and shown apart from the others
92 // of its category, with what differs.
93 type ProbeResult struct {
94 Config string `json:"config"`
95 Probe string `json:"probe"`
96 Description string `json:"description"`
97 Required bool `json:"required"`
98 Passed bool `json:"passed"`
99 Detail string `json:"detail,omitempty"`
100 }
101
102 // Result is the measurement of a configuration on a payload.
103 type Result struct {
104 Condition string `json:"condition"`
105 Op string `json:"op"`
106 Payload string `json:"payload"`
107 Config string `json:"config"`
108 NsPerOp float64 `json:"nsPerOp"`
109 BytesPerOp int64 `json:"bytesPerOp"`
110 AllocsPerOp int64 `json:"allocsPerOp"`
111 }
112
113 // Exclusion is a configuration on a payload, and why it is noted: the operation fails ( Run.Exclusions ), or its
114 // result is not the one of its category ( Run.Differences ).
115 type Exclusion struct {
116 Op string `json:"op"`
117 Payload string `json:"payload"`
118 Config string `json:"config"`
119 Reason string `json:"reason"`
120 }
121
122 // Operations are the operations measured.
123 const (
124 OpEncode = "encode"
125 OpDecode = "decode"
126 )
127
128 // ReadRun reads a run written by WriteRun.
129 func ReadRun(path string) (*Run, error) {
130 data, err := os.ReadFile(path)
131 if err != nil {
132 return nil, err
133 }
134 var r Run
135 if err := json.Unmarshal(data, &r); err != nil {
136 return nil, fmt.Errorf("%s: %w", path, err)
137 }
138 return &r, nil
139 }
140
141 // WriteRun writes a run as JSON.
142 func WriteRun(path string, r *Run) error {
143 data, err := json.MarshalIndent(r, "", " ")
144 if err != nil {
145 return err
146 }
147 return os.WriteFile(path, append(data, '\n'), 0o644)
148 }
149
150 // result returns the result of a configuration on a payload, or nil.
151 func (r *Run) result(cond, op, payload, config string) *Result {
152 for i := range r.Results {
153 x := &r.Results[i]
154 if x.Condition == cond && x.Op == op && x.Payload == payload && x.Config == config {
155 return x
156 }
157 }
158 return nil
159 }
160
161 // exclusion returns why a configuration is not measured on a payload, or "".
162 func (r *Run) exclusion(op, payload, config string) string {
163 return find(r.Exclusions, op, payload, config)
164 }
165
166 // difference returns how the result of a configuration on a payload differs from the one of its category, or "".
167 func (r *Run) difference(op, payload, config string) string {
168 return find(r.Differences, op, payload, config)
169 }
170
171 func find(es []Exclusion, op, payload, config string) string {
172 for _, e := range es {
173 if e.Op == op && e.Payload == payload && e.Config == config {
174 return e.Reason
175 }
176 }
177 return ""
178 }
179
180 // configsOf returns the configurations of a category, in their order.
181 func (r *Run) configsOf(category string) []Config {
182 var cs []Config
183 for _, c := range r.Configs {
184 if c.Category == category {
185 cs = append(cs, c)
186 }
187 }
188 return cs
189 }
190
191 // library returns the library of the name.
192 func (r *Run) library(name string) Library {
193 for _, l := range r.Libraries {
194 if l.Name == name {
195 return l
196 }
197 }
198 return Library{Name: name}
199 }