Skip to content

Commit af2c9bc

Browse files
committed
test(bench): add RunTemplate benchmark and rebuild Serialize input per iteration
Serialize rewrites its input in place, so the benchmarks now build fresh input untimed each iteration to avoid timing a no-op walk on older bases. The new RunTemplate benchmark reuses one input so it measures lazy template reads, and the benchmark workflow runs it against base and head. Claude-Session-Id: 9cbaec31-655b-4670-a331-adddc1c8c5d2
1 parent c9423b0 commit af2c9bc

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎.github/workflows/benchmark.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ jobs:
100100
for sample in {1..10}
101101
do
102102
echo "Running base sample ${sample}/10"
103-
.tmp/bench/base.test -test.run=^$ -test.bench='Benchmark(Serialize|RunExpressionContext|CELEnvExtend|CELProgramEvaluation|RunExpressionNativeInput)' -test.benchmem=true -test.benchtime=1s -test.count=1 -test.timeout=20m >> bench-base.txt
103+
.tmp/bench/base.test -test.run=^$ -test.bench='Benchmark(Serialize|RunTemplate|RunExpressionContext|CELEnvExtend|CELProgramEvaluation|RunExpressionNativeInput)' -test.benchmem=true -test.benchtime=1s -test.count=1 -test.timeout=20m >> bench-base.txt
104104
echo "Running head sample ${sample}/10"
105-
.tmp/bench/head.test -test.run=^$ -test.bench='Benchmark(Serialize|RunExpressionContext|CELEnvExtend|CELProgramEvaluation|RunExpressionNativeInput)' -test.benchmem=true -test.benchtime=1s -test.count=1 -test.timeout=20m >> bench-head.txt
105+
.tmp/bench/head.test -test.run=^$ -test.bench='Benchmark(Serialize|RunTemplate|RunExpressionContext|CELEnvExtend|CELProgramEvaluation|RunExpressionNativeInput)' -test.benchmem=true -test.benchtime=1s -test.count=1 -test.timeout=20m >> bench-head.txt
106106
done
107107
- name: Compare
108108
run: |

‎serialize_bench_test.go‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,53 @@ type benchPerson struct {
2424
Addresses []benchAddress `json:"addresses,omitempty"`
2525
}
2626

27+
// benchmarkFreshInput runs fn on a newly built input each iteration, with the build untimed. The
28+
// benchmark workflow runs this file against older bases whose Serialize rewrote its input in place,
29+
// so reusing one input there would time a no-op walk over already serialized maps.
30+
func benchmarkFreshInput(b *testing.B, build func() map[string]any, fn func(map[string]any) error) {
31+
b.ReportAllocs()
32+
for b.Loop() {
33+
b.StopTimer()
34+
input := build()
35+
b.StartTimer()
36+
if err := fn(input); err != nil {
37+
b.Fatal(err)
38+
}
39+
}
40+
}
41+
42+
func serializeBenchmark(input map[string]any) error {
43+
_, err := Serialize(input)
44+
return err
45+
}
46+
2747
func BenchmarkSerialize(b *testing.B) {
2848
for _, size := range []int{10, 100, 1000, 10000} {
2949
b.Run(fmt.Sprintf("items=%d/native_values=true", size), func(b *testing.B) {
30-
input := newSerializeBenchmarkInput(size)
31-
b.ReportAllocs()
32-
for b.Loop() {
33-
if _, err := Serialize(input); err != nil {
34-
b.Fatal(err)
35-
}
36-
}
50+
benchmarkFreshInput(b, func() map[string]any { return newSerializeBenchmarkInput(size) }, serializeBenchmark)
3751
})
3852
}
3953
}
4054

4155
func BenchmarkSerialize_NoNativeTypes(b *testing.B) {
4256
for _, size := range []int{100, 1000, 10000} {
4357
b.Run(fmt.Sprintf("items=%d/native_values=false", size), func(b *testing.B) {
44-
input := newPlainBenchmarkInput(size)
58+
benchmarkFreshInput(b, func() map[string]any { return newPlainBenchmarkInput(size) }, serializeBenchmark)
59+
})
60+
}
61+
}
62+
63+
// BenchmarkRunTemplate_LargeEnv reads two fields of a large environment through a go template. The
64+
// input is reused because rebuilding it would dwarf a lazy read; an in-place serializing base only
65+
// gains from that.
66+
func BenchmarkRunTemplate_LargeEnv(b *testing.B) {
67+
const text = "{{ .nested.labels.app }} {{ (index .items 7).name }}"
68+
for _, size := range []int{10, 1000, 10000} {
69+
b.Run(fmt.Sprintf("items=%d", size), func(b *testing.B) {
70+
input := newSerializeBenchmarkInput(size)
4571
b.ReportAllocs()
4672
for b.Loop() {
47-
if _, err := Serialize(input); err != nil {
73+
if _, err := RunTemplate(input, Template{Template: text}); err != nil {
4874
b.Fatal(err)
4975
}
5076
}

0 commit comments

Comments
 (0)