-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_coldstart_test.go
More file actions
105 lines (85 loc) · 2.67 KB
/
benchmark_coldstart_test.go
File metadata and controls
105 lines (85 loc) · 2.67 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package purejson
import "testing"
var benchmarkParserResult Element
func BenchmarkColdStart_twitter_json(b *testing.B) {
runColdStartBenchmark(b, benchmarkFixtureTwitter)
}
func BenchmarkColdStart_citm_catalog_json(b *testing.B) {
runColdStartBenchmark(b, benchmarkFixtureCITM)
}
func BenchmarkColdStart_canada_json(b *testing.B) {
runColdStartBenchmark(b, benchmarkFixtureCanada)
}
func BenchmarkWarm_twitter_json(b *testing.B) {
runWarmBenchmark(b, benchmarkFixtureTwitter)
}
func BenchmarkWarm_citm_catalog_json(b *testing.B) {
runWarmBenchmark(b, benchmarkFixtureCITM)
}
func BenchmarkWarm_canada_json(b *testing.B) {
runWarmBenchmark(b, benchmarkFixtureCanada)
}
// cold-start here means first Parse after NewParser inside an already loaded process.
// It intentionally excludes bootstrap or download time.
// Both cold and warm families report native-bytes/op, native-allocs/op, and
// native-live-bytes through benchmarkRunWithNativeAllocMetrics.
func runColdStartBenchmark(b *testing.B, fixtureName string) {
data := loadBenchmarkFixture(b, fixtureName)
b.ReportAllocs()
b.SetBytes(int64(len(data)))
benchmarkRunWithNativeAllocMetrics(b, true, func() {
for i := 0; i < b.N; i++ {
parser, err := NewParser()
if err != nil {
b.Fatalf("NewParser(%s): %v", fixtureName, err)
}
doc, err := parser.Parse(data)
if err != nil {
_ = parser.Close()
b.Fatalf("Parse(%s): %v", fixtureName, err)
}
benchmarkParserResult = doc.Root()
if err := doc.Close(); err != nil {
_ = parser.Close()
b.Fatalf("doc.Close(%s): %v", fixtureName, err)
}
if err := parser.Close(); err != nil {
b.Fatalf("parser.Close(%s): %v", fixtureName, err)
}
}
})
}
// Warm benchmarks do one warm-up parse before ResetTimer and then reuse the parser.
func runWarmBenchmark(b *testing.B, fixtureName string) {
data := loadBenchmarkFixture(b, fixtureName)
parser, err := NewParser()
if err != nil {
b.Fatalf("NewParser(%s): %v", fixtureName, err)
}
warmDoc, err := parser.Parse(data)
if err != nil {
_ = parser.Close()
b.Fatalf("warm Parse(%s): %v", fixtureName, err)
}
if err := warmDoc.Close(); err != nil {
_ = parser.Close()
b.Fatalf("warm doc.Close(%s): %v", fixtureName, err)
}
b.ReportAllocs()
b.SetBytes(int64(len(data)))
benchmarkRunWithNativeAllocMetrics(b, true, func() {
for i := 0; i < b.N; i++ {
doc, err := parser.Parse(data)
if err != nil {
b.Fatalf("Parse(%s): %v", fixtureName, err)
}
benchmarkParserResult = doc.Root()
if err := doc.Close(); err != nil {
b.Fatalf("doc.Close(%s): %v", fixtureName, err)
}
}
})
if err := parser.Close(); err != nil {
b.Fatalf("parser.Close(%s): %v", fixtureName, err)
}
}