-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith_bench_test.go
More file actions
202 lines (178 loc) · 4 KB
/
with_bench_test.go
File metadata and controls
202 lines (178 loc) · 4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: MIT
package with
import (
"testing"
)
// Benchmark types
type benchOptions struct {
field1 string
field2 int
field3 bool
field4 float64
}
type benchOptionsWithDefaults struct {
field1 string
field2 int
field3 bool
field4 float64
}
func (o *benchOptionsWithDefaults) SetDefaults() {
o.field1 = "default"
o.field2 = 42
o.field3 = true
o.field4 = 3.14
}
type benchOptionsWithValidation struct {
field1 string
field2 int
field3 bool
field4 float64
}
func (o *benchOptionsWithValidation) Validate() error {
// Minimal validation to measure overhead
if o.field2 < 0 {
return nil
}
return nil
}
// Benchmark option functions
func benchWithField1(val string) Func[benchOptions] {
return func(o *benchOptions) error {
o.field1 = val
return nil
}
}
func benchWithField2(val int) Func[benchOptions] {
return func(o *benchOptions) error {
o.field2 = val
return nil
}
}
func benchWithField3(val bool) Func[benchOptions] {
return func(o *benchOptions) error {
o.field3 = val
return nil
}
}
func benchWithField4(val float64) Func[benchOptions] {
return func(o *benchOptions) error {
o.field4 = val
return nil
}
}
func benchWithField1Defaults(val string) Func[benchOptionsWithDefaults] {
return func(o *benchOptionsWithDefaults) error {
o.field1 = val
return nil
}
}
func benchWithField2Defaults(val int) Func[benchOptionsWithDefaults] {
return func(o *benchOptionsWithDefaults) error {
o.field2 = val
return nil
}
}
func benchWithField1Validation(val string) Func[benchOptionsWithValidation] {
return func(o *benchOptionsWithValidation) error {
o.field1 = val
return nil
}
}
func benchWithField2Validation(val int) Func[benchOptionsWithValidation] {
return func(o *benchOptionsWithValidation) error {
o.field2 = val
return nil
}
}
// Benchmarks
func BenchmarkAddWith_NoOptions(b *testing.B) {
opts := &benchOptions{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = AddWith(opts, []Func[benchOptions]{})
}
}
func BenchmarkAddWith_SingleOption(b *testing.B) {
opts := &benchOptions{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = AddWith(opts, []Func[benchOptions]{
benchWithField1("test"),
})
}
}
func BenchmarkAddWith_FourOptions(b *testing.B) {
opts := &benchOptions{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = AddWith(opts, []Func[benchOptions]{
benchWithField1("test"),
benchWithField2(123),
benchWithField3(true),
benchWithField4(3.14),
})
}
}
func BenchmarkDefaultThenAddWith_NoOptions(b *testing.B) {
opts := &benchOptionsWithDefaults{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = DefaultThenAddWith(opts, []Func[benchOptionsWithDefaults]{})
}
}
func BenchmarkDefaultThenAddWith_TwoOptions(b *testing.B) {
opts := &benchOptionsWithDefaults{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = DefaultThenAddWith(opts, []Func[benchOptionsWithDefaults]{
benchWithField1Defaults("custom"),
benchWithField2Defaults(999),
})
}
}
func BenchmarkAddWith_WithValidation(b *testing.B) {
opts := &benchOptionsWithValidation{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = AddWith(opts, []Func[benchOptionsWithValidation]{
benchWithField1Validation("test"),
benchWithField2Validation(123),
})
}
}
func BenchmarkNop(b *testing.B) {
opts := &benchOptions{}
nopFunc := Nop[benchOptions]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = nopFunc(opts)
}
}
func BenchmarkMustAddWith_SingleOption(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
opts := &benchOptions{}
_ = MustAddWith(opts, []Func[benchOptions]{
benchWithField1("test"),
})
}
}
func BenchmarkMustDefaultThenAddWith_NoOptions(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
opts := &benchOptionsWithDefaults{}
_ = MustDefaultThenAddWith(opts, []Func[benchOptionsWithDefaults]{})
}
}
// Benchmark direct struct initialization for comparison
func BenchmarkDirectInit(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = &benchOptions{
field1: "test",
field2: 123,
field3: true,
field4: 3.14,
}
}
}