| 1 |
package report |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
func testRun(arch string) *Run { |
| 10 |
return &Run{ |
| 11 |
GeneratedAt: time.Date(2026, 9, 27, 0, 0, 0, 0, time.UTC), |
| 12 |
GoVersion: "go1.27.1", GOOS: "linux", GOARCH: arch, CPU: "test CPU", |
| 13 |
Commit: "0123456789abcdef", Repository: "goccy/go-json", BenchTime: "100ms", Rounds: 3, |
| 14 |
Libraries: []Library{{Name: "encoding/json", Module: "std", Version: "go1.27.1"}, {Name: "goccy/go-json", Module: "github.com/goccy/go-json", Version: "0123456789abcdef"}}, |
| 15 |
Categories: []Category{{ID: "std", Title: "Same behavior as encoding/json", Baseline: "encoding/json"}}, |
| 16 |
Conditions: []Condition{{ID: "live-heap", Title: "With a live heap"}}, |
| 17 |
Payloads: []Payload{{ID: "small", Title: "Small struct", Bytes: 100}, {ID: "large", Title: "Large struct", Bytes: 1000}}, |
| 18 |
Configs: []Config{ |
| 19 |
{ID: "encoding/json", Library: "encoding/json", Category: "std", Title: "encoding/json"}, |
| 20 |
{ID: "go-json", Library: "goccy/go-json", Category: "std", Title: "goccy/go-json"}, |
| 21 |
{ID: "other", Library: "other", Category: "std", Title: "other <lib>"}, |
| 22 |
}, |
| 23 |
Probes: []ProbeResult{ |
| 24 |
{Config: "go-json", Probe: "p", Description: "probe", Required: true, Passed: true}, |
| 25 |
{Config: "other", Probe: "p", Description: "probe", Required: true, Passed: false, Detail: "got <x>"}, |
| 26 |
}, |
| 27 |
Results: []Result{ |
| 28 |
{Condition: "live-heap", Op: OpDecode, Payload: "small", Config: "encoding/json", NsPerOp: 400}, |
| 29 |
{Condition: "live-heap", Op: OpDecode, Payload: "small", Config: "go-json", NsPerOp: 100}, |
| 30 |
{Condition: "live-heap", Op: OpDecode, Payload: "large", Config: "encoding/json", NsPerOp: 4000}, |
| 31 |
{Condition: "live-heap", Op: OpDecode, Payload: "large", Config: "go-json", NsPerOp: 2000}, |
| 32 |
{Condition: "live-heap", Op: OpDecode, Payload: "small", Config: "other", NsPerOp: 50}, |
| 33 |
}, |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestRender(t *testing.T) { |
| 38 |
site := &Site{Runs: []*Run{testRun("arm64"), testRun("amd64")}, Files: []string{"results-arm64.json", "results-amd64.json"}} |
| 39 |
page, err := site.HTML() |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
for _, want := range []string{ |
| 44 |
`"arch":"amd64"`, // the results are in the page as JSON |
| 45 |
`"file":"results-amd64.json"`, |
| 46 |
`other \u003clib\u003e`, // escaped in the script |
| 47 |
`"differs":{"other":[`, // the configuration which fails a probe |
| 48 |
"<h1>Go JSON Benchmarks</h1>", |
| 49 |
} { |
| 50 |
if !strings.Contains(page, want) { |
| 51 |
t.Errorf("the page has no %q", want) |
| 52 |
} |
| 53 |
} |
| 54 |
// amd64 is the first run |
| 55 |
if strings.Index(page, `"arch":"amd64"`) > strings.Index(page, `"arch":"arm64"`) { |
| 56 |
t.Error("the runs are not ordered by architecture") |
| 57 |
} |
| 58 |
svg := site.Summary() |
| 59 |
if !strings.HasPrefix(svg, "<svg") || !strings.Contains(svg, "2.83x") || !strings.Contains(svg, "other <lib> †") || |
| 60 |
!strings.Contains(svg, `fill="#0969da"`) || !strings.Contains(svg, `fill="url(#h`) { |
| 61 |
t.Errorf("unexpected summary: %s", svg) |
| 62 |
} |
| 63 |
} |