-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathexpander_bench_test.go
More file actions
85 lines (74 loc) · 2.55 KB
/
Copy pathexpander_bench_test.go
File metadata and controls
85 lines (74 loc) · 2.55 KB
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
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package spec
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
// expandBenchmarks are the documents the expansion benchmarks run on, ordered by how much cycle
// detection they exercise.
//
// fixture-957.json is the one PR #121 had to disable for slowness, so it is the case any change
// to how cycles are remembered has to answer for. bitbucket.json is the largest cyclic document
// here, 300 $ref; clickmeter.json is the largest acyclic one, and says what expansion costs when
// cycles are not the subject.
func expandBenchmarks() []struct{ name, path string } {
return []struct{ name, path string }{
{"petstore-acyclic", filepath.Join("testdata", "expansion", "petstore2.0.json")},
{"circular-spec", filepath.Join("testdata", "expansion", "circularSpec.json")},
{"shared-node-cycles", filepath.Join("testdata", "expansion", "shared-node-cycles.json")},
{"issue-957", filepath.Join("testdata", "bugs", "957", "fixture-957.json")},
{"bitbucket", filepath.Join("testdata", "more_circulars", "bitbucket.json")},
{"clickmeter", filepath.Join("testdata", "expansion", "clickmeter.json")},
}
}
// BenchmarkExpandSpec measures expansion alone: the document is unmarshalled again for every
// iteration, since expansion rewrites it, and that unmarshalling is not timed.
func BenchmarkExpandSpec(b *testing.B) {
for _, bench := range expandBenchmarks() {
data, err := os.ReadFile(bench.path)
if err != nil {
b.Fatal(err)
}
b.Run(bench.name, func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
b.StopTimer()
doc := new(Swagger)
if err := json.Unmarshal(data, doc); err != nil {
b.Fatal(err)
}
b.StartTimer()
if err := ExpandSpec(doc, &ExpandOptions{RelativeBase: bench.path}); err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkExpandSpecSkipSchemas measures the path flatten takes for its minimal and full modes,
// where schemata are not inlined and only $ref are rebased.
func BenchmarkExpandSpecSkipSchemas(b *testing.B) {
for _, bench := range expandBenchmarks() {
data, err := os.ReadFile(bench.path)
if err != nil {
b.Fatal(err)
}
b.Run(bench.name, func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
b.StopTimer()
doc := new(Swagger)
if err := json.Unmarshal(data, doc); err != nil {
b.Fatal(err)
}
b.StartTimer()
if err := ExpandSpec(doc, &ExpandOptions{RelativeBase: bench.path, SkipSchemas: true}); err != nil {
b.Fatal(err)
}
}
})
}
}