Repository navigation
Expand file tree
/
Copy pathserialize_bench_test.go
More file actions
108 lines (100 loc) · 2.9 KB
/
Copy pathserialize_bench_test.go
File metadata and controls
108 lines (100 loc) · 2.9 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 gomplate
import (
"fmt"
"testing"
"time"
"github.com/google/uuid"
)
type benchAddress struct {
City string `json:"city_name"`
Country string `json:"country,omitempty"`
}
type benchPerson struct {
Name string `json:"name"`
Age int `json:"age,omitempty"`
ID uuid.UUID `json:"id"`
Duration time.Duration `json:"duration"`
Address *benchAddress `json:",omitempty"`
MetaData map[string]any `json:",omitempty"`
Codes []string `json:",omitempty"`
Addresses []benchAddress `json:"addresses,omitempty"`
}
func BenchmarkSerialize(b *testing.B) {
for _, size := range []int{10, 100, 1000, 10000} {
b.Run(fmt.Sprintf("items=%d/native_values=true", size), func(b *testing.B) {
input := newSerializeBenchmarkInput(size)
b.ReportAllocs()
for b.Loop() {
if _, err := Serialize(input); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkSerialize_NoNativeTypes(b *testing.B) {
for _, size := range []int{100, 1000, 10000} {
b.Run(fmt.Sprintf("items=%d/native_values=false", size), func(b *testing.B) {
input := newPlainBenchmarkInput(size)
b.ReportAllocs()
for b.Loop() {
if _, err := Serialize(input); err != nil {
b.Fatal(err)
}
}
})
}
}
func newSerializeBenchmarkInput(size int) map[string]any {
items := make([]any, size)
identifier := uuid.MustParse("0192f0a4-1234-7000-8000-aaaaaaaaaaaa")
for i := range items {
items[i] = benchPerson{
Name: fmt.Sprintf("person-%d", i),
Age: i % 100,
ID: identifier,
Duration: time.Duration(i) * time.Millisecond,
Address: &benchAddress{City: "Kathmandu", Country: "Nepal"},
MetaData: map[string]any{
"index": i, "enabled": i%2 == 0, "uuid": identifier,
"duration": time.Duration(i) * time.Second,
},
Codes: []string{"GO", "JS", "CEL"},
Addresses: []benchAddress{
{City: "Kathmandu", Country: "Nepal"},
{City: "Lalitpur", Country: "Nepal"},
{City: "Bhaktapur", Country: "Nepal"},
},
}
}
return map[string]any{
"id": identifier,
"started": time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC),
"duration": 5 * time.Minute,
"items": items,
"nested": map[string]any{
"bytes": []byte("hello world"),
"labels": map[string]string{"app": "gomplate", "bench": "serialize"},
"durations": []time.Duration{time.Second, time.Minute, time.Hour},
},
}
}
func newPlainBenchmarkInput(size int) map[string]any {
items := make([]any, size)
for i := range items {
items[i] = map[string]any{
"name": fmt.Sprintf("person-%d", i),
"age": i % 100,
"enabled": i%2 == 0,
"codes": []string{"GO", "JS", "CEL"},
"address": map[string]any{"city": "Kathmandu", "country": "Nepal"},
}
}
return map[string]any{
"started": "2026-01-01T00:00:00Z",
"items": items,
"nested": map[string]any{
"labels": map[string]string{"app": "gomplate", "bench": "serialize"},
},
}
}