Repository navigation
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
176 lines (153 loc) · 3.74 KB
/
Copy pathbenchmark_test.go
File metadata and controls
176 lines (153 loc) · 3.74 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
// benchmark_test.go: Benchmarks for the go-errors AGILira library
//
// Copyright (c) 2025 AGILira - A. Giordano
// Series: an AGLIra library
// SPDX-License-Identifier: MPL-2.0
package errors
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/agilira/go-timecache"
)
const BenchmarkErrorCode ErrorCode = "BENCHMARK_ERROR"
// Benchmark error creation functions
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = New(BenchmarkErrorCode, "Benchmark error message")
}
}
func BenchmarkNewWithField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = NewWithField(BenchmarkErrorCode, "Validation error", "username", "invalid")
}
}
func BenchmarkNewWithContext(b *testing.B) {
ctx := map[string]interface{}{
"user_id": "123",
"action": "login",
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = NewWithContext(BenchmarkErrorCode, "Context error", ctx)
}
}
func BenchmarkWrap(b *testing.B) {
originalErr := fmt.Errorf("original error")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Wrap(originalErr, BenchmarkErrorCode, "Wrapped error")
}
}
// Benchmark stacktrace operations
func BenchmarkCaptureStacktrace(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = CaptureStacktrace(1)
}
}
func BenchmarkStacktraceString(b *testing.B) {
stack := CaptureStacktrace(1)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = stack.String()
}
}
// Benchmark JSON marshaling
func BenchmarkMarshalJSON(b *testing.B) {
err := New(BenchmarkErrorCode, "JSON benchmark error").
WithUserMessage("User friendly message").
WithContext("key", "value").
AsRetryable().
WithCriticalSeverity()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(err)
}
}
func BenchmarkMarshalJSONWithStack(b *testing.B) {
originalErr := fmt.Errorf("original error")
err := Wrap(originalErr, BenchmarkErrorCode, "Error with stack").
WithUserMessage("User friendly message").
WithContext("operation", "benchmark")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(err)
}
}
// Benchmark method chaining
func BenchmarkMethodChaining(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = New(BenchmarkErrorCode, "Chaining benchmark").
WithUserMessage("User message").
WithContext("iteration", i).
WithContext("benchmark", true).
AsRetryable().
WithWarningSeverity()
}
}
// Benchmark error interface methods
func BenchmarkErrorString(b *testing.B) {
err := New(BenchmarkErrorCode, "Error string benchmark")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}
func BenchmarkHasCode(b *testing.B) {
err := New(BenchmarkErrorCode, "HasCode benchmark")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = HasCode(err, BenchmarkErrorCode)
}
}
func BenchmarkRootCause(b *testing.B) {
originalErr := fmt.Errorf("original error")
wrappedErr := Wrap(originalErr, BenchmarkErrorCode, "Wrapped error")
doubleWrapped := Wrap(wrappedErr, "DOUBLE_WRAP", "Double wrapped")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = RootCause(doubleWrapped)
}
}
// Benchmark validation functions
func BenchmarkValidateErrorCode(b *testing.B) {
testCodes := []ErrorCode{
"VALID_CODE",
"",
" \t\n ",
"ANOTHER_VALID_CODE",
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, code := range testCodes {
_ = validateErrorCode(code)
}
}
}
// Benchmark time access methods comparison
func BenchmarkTimeNow(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = time.Now()
}
}
func BenchmarkTimeCachedTime(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = timecache.CachedTime()
}
}