-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
435 lines (361 loc) · 11.1 KB
/
benchmark_test.go
File metadata and controls
435 lines (361 loc) · 11.1 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package rescode
import (
"errors"
"fmt"
"testing"
"google.golang.org/grpc/codes"
)
// LegacyErrorRegistry simulates a runtime map-based error system for comparison
type LegacyErrorRegistry struct {
errors map[string]LegacyErrorDef
}
type LegacyErrorDef struct {
Code uint64
Message string
HttpCode int
RpcCode codes.Code
}
// NewLegacyRegistry creates a legacy error registry with runtime lookups
func NewLegacyRegistry() *LegacyErrorRegistry {
return &LegacyErrorRegistry{
errors: map[string]LegacyErrorDef{
"PolicyNotFound": {
Code: 20001,
Message: "Policy not found",
HttpCode: 404,
RpcCode: codes.NotFound,
},
"InvalidKind": {
Code: 20002,
Message: "Invalid policy kind",
HttpCode: 400,
RpcCode: codes.InvalidArgument,
},
"InternalError": {
Code: 20003,
Message: "Internal server error",
HttpCode: 500,
RpcCode: codes.Internal,
},
},
}
}
func (r *LegacyErrorRegistry) CreateError(key string, err ...error) (*RC, error) {
def, exists := r.errors[key]
if !exists {
return nil, fmt.Errorf("error definition not found: %s", key)
}
rc := &RC{
Code: def.Code,
Message: def.Message,
HttpCode: def.HttpCode,
RpcCode: def.RpcCode,
}
if len(err) > 0 {
rc.err = err[0]
}
return rc, nil
}
// StaticCodeLegacy approach: uint64 constants with message map
const (
StaticCodePolicyNotFound uint64 = 20001
StaticCodeInvalidKind uint64 = 20002
StaticCodeInternalError uint64 = 20003
)
var staticCodeMessages = map[uint64]string{
StaticCodePolicyNotFound: "Policy not found",
StaticCodeInvalidKind: "Invalid policy kind",
StaticCodeInternalError: "Internal server error",
}
var staticCodeHttpCodes = map[uint64]int{
StaticCodePolicyNotFound: 404,
StaticCodeInvalidKind: 400,
StaticCodeInternalError: 500,
}
var staticCodeRpcCodes = map[uint64]codes.Code{
StaticCodePolicyNotFound: codes.NotFound,
StaticCodeInvalidKind: codes.InvalidArgument,
StaticCodeInternalError: codes.Internal,
}
func CreateStaticCodeError(code uint64, err ...error) (*RC, error) {
message, exists := staticCodeMessages[code]
if !exists {
return nil, fmt.Errorf("error code not found: %d", code)
}
rc := &RC{
Code: code,
Message: message,
HttpCode: staticCodeHttpCodes[code],
RpcCode: staticCodeRpcCodes[code],
}
if len(err) > 0 {
rc.err = err[0]
}
return rc, nil
}
// VarDeclarationLegacy approach: var declarations with maps
var (
VarCodePolicyNotFound uint64 = 20001
VarCodeInvalidKind uint64 = 20002
VarCodeInternalError uint64 = 20003
)
var varCodeMessages = map[uint64]string{
VarCodePolicyNotFound: "Policy not found",
VarCodeInvalidKind: "Invalid policy kind",
VarCodeInternalError: "Internal server error",
}
var varCodeHttpCodes = map[uint64]int{
VarCodePolicyNotFound: 404,
VarCodeInvalidKind: 400,
VarCodeInternalError: 500,
}
var varCodeRpcCodes = map[uint64]codes.Code{
VarCodePolicyNotFound: codes.NotFound,
VarCodeInvalidKind: codes.InvalidArgument,
VarCodeInternalError: codes.Internal,
}
func CreateVarCodeError(code uint64, err ...error) (*RC, error) {
message, exists := varCodeMessages[code]
if !exists {
return nil, fmt.Errorf("error code not found: %d", code)
}
rc := &RC{
Code: code,
Message: message,
HttpCode: varCodeHttpCodes[code],
RpcCode: varCodeRpcCodes[code],
}
if len(err) > 0 {
rc.err = err[0]
}
return rc, nil
}
// Benchmarks comparing generated code vs legacy runtime approach
func BenchmarkGenerated_PolicyNotFound(b *testing.B) {
// This uses the rescode.New approach with compile-time constants
creator := New(20001, 404, codes.NotFound, "Policy not found")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = creator()
}
}
func BenchmarkLegacy_PolicyNotFound(b *testing.B) {
registry := NewLegacyRegistry()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = registry.CreateError("PolicyNotFound")
}
}
func BenchmarkGenerated_PolicyNotFound_WithError(b *testing.B) {
creator := New(20001, 404, codes.NotFound, "Policy not found")
err := errors.New("wrapped error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = creator(err)
}
}
func BenchmarkLegacy_PolicyNotFound_WithError(b *testing.B) {
registry := NewLegacyRegistry()
err := errors.New("wrapped error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = registry.CreateError("PolicyNotFound", err)
}
}
func BenchmarkGenerated_MultipleErrors(b *testing.B) {
policyNotFound := New(20001, 404, codes.NotFound, "Policy not found")
invalidKind := New(20002, 400, codes.InvalidArgument, "Invalid policy kind")
internalError := New(20003, 500, codes.Internal, "Internal server error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
switch i % 3 {
case 0:
_ = policyNotFound()
case 1:
_ = invalidKind()
case 2:
_ = internalError()
}
}
}
func BenchmarkLegacy_MultipleErrors(b *testing.B) {
registry := NewLegacyRegistry()
keys := []string{"PolicyNotFound", "InvalidKind", "InternalError"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := keys[i%3]
_, _ = registry.CreateError(key)
}
}
func BenchmarkGenerated_ErrorMessage(b *testing.B) {
creator := New(20001, 404, codes.NotFound, "Policy not found")
err := creator()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
func BenchmarkLegacy_ErrorMessage(b *testing.B) {
registry := NewLegacyRegistry()
err, _ := registry.CreateError("PolicyNotFound", errors.New("wrapped error"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
func BenchmarkGenerated_JSON(b *testing.B) {
creator := New(20001, 404, codes.NotFound, "Policy not found", map[string]string{"resource": "policy_123"})
err := creator(errors.New("wrapped error"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.JSON()
}
}
func BenchmarkLegacy_JSON(b *testing.B) {
registry := NewLegacyRegistry()
err, _ := registry.CreateError("PolicyNotFound", errors.New("wrapped error"))
err.SetData(map[string]string{"resource": "policy_123"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.JSON()
}
}
// Benchmarks for Static Code approach
func BenchmarkStaticCode_PolicyNotFound(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = CreateStaticCodeError(StaticCodePolicyNotFound)
}
}
func BenchmarkStaticCode_PolicyNotFound_WithError(b *testing.B) {
err := errors.New("wrapped error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = CreateStaticCodeError(StaticCodePolicyNotFound, err)
}
}
func BenchmarkStaticCode_MultipleErrors(b *testing.B) {
codes := []uint64{StaticCodePolicyNotFound, StaticCodeInvalidKind, StaticCodeInternalError}
b.ResetTimer()
for i := 0; i < b.N; i++ {
code := codes[i%3]
_, _ = CreateStaticCodeError(code)
}
}
func BenchmarkStaticCode_ErrorMessage(b *testing.B) {
err, _ := CreateStaticCodeError(StaticCodePolicyNotFound, errors.New("wrapped error"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
func BenchmarkStaticCode_JSON(b *testing.B) {
err, _ := CreateStaticCodeError(StaticCodePolicyNotFound, errors.New("wrapped error"))
err.SetData(map[string]string{"resource": "policy_123"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.JSON()
}
}
// Benchmarks for Var Declaration approach
func BenchmarkVarCode_PolicyNotFound(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = CreateVarCodeError(VarCodePolicyNotFound)
}
}
func BenchmarkVarCode_PolicyNotFound_WithError(b *testing.B) {
err := errors.New("wrapped error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = CreateVarCodeError(VarCodePolicyNotFound, err)
}
}
func BenchmarkVarCode_MultipleErrors(b *testing.B) {
codes := []uint64{VarCodePolicyNotFound, VarCodeInvalidKind, VarCodeInternalError}
b.ResetTimer()
for i := 0; i < b.N; i++ {
code := codes[i%3]
_, _ = CreateVarCodeError(code)
}
}
func BenchmarkVarCode_ErrorMessage(b *testing.B) {
err, _ := CreateVarCodeError(VarCodePolicyNotFound, errors.New("wrapped error"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
func BenchmarkVarCode_JSON(b *testing.B) {
err, _ := CreateVarCodeError(VarCodePolicyNotFound, errors.New("wrapped error"))
err.SetData(map[string]string{"resource": "policy_123"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = err.JSON()
}
}
// Test to verify legacy vs generated produce same results
func TestLegacyVsGenerated_Compatibility(t *testing.T) {
registry := NewLegacyRegistry()
creator := New(20001, 404, codes.NotFound, "Policy not found")
// Test basic creation
legacyErr, _ := registry.CreateError("PolicyNotFound")
generatedErr := creator()
if legacyErr.Code != generatedErr.Code {
t.Errorf("Code mismatch: legacy %d, generated %d", legacyErr.Code, generatedErr.Code)
}
if legacyErr.Message != generatedErr.Message {
t.Errorf("Message mismatch: legacy %q, generated %q", legacyErr.Message, generatedErr.Message)
}
if legacyErr.HttpCode != generatedErr.HttpCode {
t.Errorf("HttpCode mismatch: legacy %d, generated %d", legacyErr.HttpCode, generatedErr.HttpCode)
}
if legacyErr.RpcCode != generatedErr.RpcCode {
t.Errorf("RpcCode mismatch: legacy %d, generated %d", legacyErr.RpcCode, generatedErr.RpcCode)
}
// Test with wrapped error
originalErr := errors.New("wrapped error")
legacyWithErr, _ := registry.CreateError("PolicyNotFound", originalErr)
generatedWithErr := creator(originalErr)
if legacyWithErr.Error() != generatedWithErr.Error() {
t.Errorf("Error() mismatch: legacy %q, generated %q", legacyWithErr.Error(), generatedWithErr.Error())
}
}
// Test static code approach compatibility
func TestStaticCodeVsGenerated_Compatibility(t *testing.T) {
creator := New(20001, 404, codes.NotFound, "Policy not found")
// Test basic creation
staticErr, _ := CreateStaticCodeError(StaticCodePolicyNotFound)
generatedErr := creator()
if staticErr.Code != generatedErr.Code {
t.Errorf("Code mismatch: static %d, generated %d", staticErr.Code, generatedErr.Code)
}
if staticErr.Message != generatedErr.Message {
t.Errorf("Message mismatch: static %q, generated %q", staticErr.Message, generatedErr.Message)
}
if staticErr.HttpCode != generatedErr.HttpCode {
t.Errorf("HttpCode mismatch: static %d, generated %d", staticErr.HttpCode, generatedErr.HttpCode)
}
if staticErr.RpcCode != generatedErr.RpcCode {
t.Errorf("RpcCode mismatch: static %d, generated %d", staticErr.RpcCode, generatedErr.RpcCode)
}
}
// Test var code approach compatibility
func TestVarCodeVsGenerated_Compatibility(t *testing.T) {
creator := New(20001, 404, codes.NotFound, "Policy not found")
// Test basic creation
varErr, _ := CreateVarCodeError(VarCodePolicyNotFound)
generatedErr := creator()
if varErr.Code != generatedErr.Code {
t.Errorf("Code mismatch: var %d, generated %d", varErr.Code, generatedErr.Code)
}
if varErr.Message != generatedErr.Message {
t.Errorf("Message mismatch: var %q, generated %q", varErr.Message, generatedErr.Message)
}
if varErr.HttpCode != generatedErr.HttpCode {
t.Errorf("HttpCode mismatch: var %d, generated %d", varErr.HttpCode, generatedErr.HttpCode)
}
if varErr.RpcCode != generatedErr.RpcCode {
t.Errorf("RpcCode mismatch: var %d, generated %d", varErr.RpcCode, generatedErr.RpcCode)
}
}