-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbench_test.go
More file actions
108 lines (93 loc) · 3.92 KB
/
Copy pathbench_test.go
File metadata and controls
108 lines (93 loc) · 3.92 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
106
107
108
package main
import (
"testing"
"github.com/itchyny/gojq"
)
// smallJSON / largeJSON are reused across benchmarks to isolate the
// parse+compile cost from JSON unmarshal cost.
const smallJSON = `{"repo":{"name":"mgdm/htmlq"},"type":"PushEvent"}`
const largeJSON = `[{"id":"24583862139","type":"PushEvent","actor":{"id":71893,"login":"mgdm","display_login":"mgdm","gravatar_id":"","url":"https://api.github.com/users/mgdm","avatar_url":"https://avatars.githubusercontent.com/u/71893?"},"repo":{"id":185476675,"name":"mgdm/htmlq","url":"https://api.github.com/repos/mgdm/htmlq"},"payload":{"push_id":11320965987,"size":1,"distinct_size":1,"ref":"refs/heads/master","head":"739cd363543cd5c36a2d7bcbbb3ab7e811205611","before":"1f5fa50722436df15d57e8627e32b68a6dc8c927","commits":[{"sha":"739cd363543cd5c36a2d7bcbbb3ab7e811205611","author":{"email":"michael@mgdm.net","name":"Michael Maclean"},"message":"Add flake.nix","distinct":true}]},"public":true,"created_at":"2022-10-13T17:52:21Z"},{"id":"24583836616","type":"PushEvent","actor":{"id":71893,"login":"mgdm","display_login":"mgdm","gravatar_id":"","url":"https://api.github.com/users/mgdm","avatar_url":"https://avatars.githubusercontent.com/u/71893?"},"repo":{"id":185476675,"name":"mgdm/htmlq","url":"https://api.github.com/repos/mgdm/htmlq"},"payload":{"push_id":11320953650,"size":1,"distinct_size":1,"ref":"refs/heads/master","head":"1f5fa50722436df15d57e8627e32b68a6dc8c927","before":"103bb2157fba78218e2679ce16365a769de12ccf","commits":[{"sha":"1f5fa50722436df15d57e8627e32b68a6dc8c927","author":{"email":"michael.maclean@bbc.co.uk","name":"Michael Maclean"},"message":"Add flake.nix","distinct":true}]},"public":true,"created_at":"2022-10-13T17:51:04Z"}]`
// --- compile step in isolation ---
// BenchmarkCompileHot measures a cache hit: parse+compile already done.
func BenchmarkCompileHot(b *testing.B) {
const q = ".repo.name"
compileQuery(q) // warm cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
compileQuery(q)
}
}
// BenchmarkCompileCold measures what happened before the cache: parse+compile
// on every call.
func BenchmarkCompileCold(b *testing.B) {
const q = ".repo.name"
b.ResetTimer()
for i := 0; i < b.N; i++ {
parsed, _ := gojq.Parse(q)
gojq.Compile(parsed)
}
}
// --- end-to-end jq() via SQL ---
func BenchmarkJqScalarHot(b *testing.B) {
db := openDB(b)
var got string
db.QueryRow(`SELECT jq(?, '.repo.name')`, smallJSON).Scan(&got) // warm cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
db.QueryRow(`SELECT jq(?, '.repo.name')`, smallJSON).Scan(&got)
}
}
func BenchmarkJqScalarCold(b *testing.B) {
db := openDB(b)
var got string
b.ResetTimer()
for i := 0; i < b.N; i++ {
queryCache.Delete(".repo.name")
db.QueryRow(`SELECT jq(?, '.repo.name')`, smallJSON).Scan(&got)
}
}
func BenchmarkJqLargeJSONHot(b *testing.B) {
db := openDB(b)
var got string
db.QueryRow(`SELECT jq(?, '.[].repo.name')`, largeJSON).Scan(&got) // warm cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
db.QueryRow(`SELECT jq(?, '.[].repo.name')`, largeJSON).Scan(&got)
}
}
func BenchmarkJqLargeJSONCold(b *testing.B) {
db := openDB(b)
var got string
b.ResetTimer()
for i := 0; i < b.N; i++ {
queryCache.Delete(".[].repo.name")
db.QueryRow(`SELECT jq(?, '.[].repo.name')`, largeJSON).Scan(&got)
}
}
// --- end-to-end jq_each() via SQL ---
func BenchmarkJqEachHot(b *testing.B) {
db := openDB(b)
db.QueryRow(`SELECT value FROM jq_each(?, '.[].repo.name') LIMIT 1`, largeJSON).Scan(nil) // warm cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
rows, _ := db.Query(`SELECT value FROM jq_each(?, '.[].repo.name')`, largeJSON)
for rows.Next() {
var v string
rows.Scan(&v)
}
rows.Close()
}
}
func BenchmarkJqEachCold(b *testing.B) {
db := openDB(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
queryCache.Delete(".[].repo.name")
rows, _ := db.Query(`SELECT value FROM jq_each(?, '.[].repo.name')`, largeJSON)
for rows.Next() {
var v string
rows.Scan(&v)
}
rows.Close()
}
}