forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserialize_bench_test.go
More file actions
130 lines (114 loc) · 3.09 KB
/
Copy pathserialize_bench_test.go
File metadata and controls
130 lines (114 loc) · 3.09 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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) {
sizes := []int{10, 100, 1000, 10000}
for _, size := range sizes {
b.Run(fmt.Sprintf("Size-%d", size), func(b *testing.B) {
input := newSerializeBenchmarkInput(size)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := Serialize(input); err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkSerialize_NoNativeTypes measures the path where Walk finds no
// uuid/duration/AsMapper values — isolates the Alter cost from the SetOne
// fixup loop optimized in the last commit.
func BenchmarkSerialize_NoNativeTypes(b *testing.B) {
sizes := []int{100, 1000, 10000}
for _, size := range sizes {
b.Run(fmt.Sprintf("Size-%d", size), func(b *testing.B) {
input := newPlainBenchmarkInput(size)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := Serialize(input); err != nil {
b.Fatal(err)
}
}
})
}
}
func newSerializeBenchmarkInput(size int) map[string]any {
items := make([]any, size)
for i := range items {
items[i] = benchPerson{
Name: fmt.Sprintf("person-%d", i),
Age: i % 100,
ID: uuid.New(),
Duration: time.Duration(i) * time.Millisecond,
Address: &benchAddress{
City: "Kathmandu",
Country: "Nepal",
},
MetaData: map[string]any{
"index": i,
"enabled": i%2 == 0,
"uuid": uuid.New(),
"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": uuid.New(),
"started": time.Now(),
"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"},
},
}
}