-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
186 lines (167 loc) · 3.74 KB
/
benchmarks_test.go
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
package env
import (
"fmt"
"net/url"
"os"
"testing"
)
// Test structures
type testNestedConfig struct {
Label string `env:"LABEL"`
Value int `env:"VALUE"`
}
type testConfig struct {
Host string `env:"HOST" def:"localhost"`
Port int `env:"PORT" def:"8080"`
IPs []string `env:"ALLOWED_IPS" sep:","`
IsProduction bool `env:"IS_PROD"`
API url.URL `env:"API_URL"`
Nested testNestedConfig `env:"NESTED"`
}
// Benchmark environment variable operations
func BenchmarkSet(b *testing.B) {
for i := 0; i < b.N; i++ {
Set("TEST_KEY", "test_value")
}
}
func BenchmarkGet(b *testing.B) {
Set("BENCH_KEY", "bench_value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
Get("BENCH_KEY")
}
}
func BenchmarkLookup(b *testing.B) {
Set("LOOKUP_KEY", "lookup_value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
Lookup("LOOKUP_KEY")
}
}
// Benchmark string parsing
func BenchmarkSplitN(b *testing.B) {
str := "value1,value2,value3,value4,value5"
b.ResetTimer()
for i := 0; i < b.N; i++ {
splitN(str, ",", -1)
}
}
// Benchmark struct operations
func BenchmarkMarshalSimple(b *testing.B) {
config := testConfig{
Host: "localhost",
Port: 8080,
IPs: []string{"127.0.0.1", "192.168.1.1"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Marshal("TEST_", config)
}
}
func BenchmarkUnmarshalSimple(b *testing.B) {
Set("TEST_HOST", "localhost")
Set("TEST_PORT", "8080")
Set("TEST_ALLOWED_IPS", "127.0.0.1,192.168.1.1")
b.ResetTimer()
for i := 0; i < b.N; i++ {
var config testConfig
Unmarshal("TEST_", &config)
}
}
// Benchmark file operations
func BenchmarkLoadEnvFile(b *testing.B) {
// Create temporary .env file for testing
content := `
HOST=localhost
PORT=8080
ALLOWED_IPS=127.0.0.1,192.168.1.1
IS_PROD=true
API_URL=https://api.example.com
NESTED_LABEL=Test
NESTED_VALUE=42
`
tmpfile := b.TempDir() + "/.env"
if err := os.WriteFile(tmpfile, []byte(content), 0644); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Load(tmpfile)
}
}
// Benchmark parallel operations
func BenchmarkParallelUnmarshal(b *testing.B) {
Set("TEST_HOST", "localhost")
Set("TEST_PORT", "8080")
Set("TEST_ALLOWED_IPS", "127.0.0.1,192.168.1.1")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var config testConfig
Unmarshal("TEST_", &config)
}
})
}
// Benchmark with different parallel tasks settings
func BenchmarkParallelTasks(b *testing.B) {
tests := []int{2, 4, 8, 16}
content := `
HOST=localhost
PORT=8080
ALLOWED_IPS=127.0.0.1,192.168.1.1
IS_PROD=true
API_URL=https://api.example.com
`
tmpfile := b.TempDir() + "/.env"
if err := os.WriteFile(tmpfile, []byte(content), 0644); err != nil {
b.Fatal(err)
}
for _, tasks := range tests {
b.Run(fmt.Sprintf("ParallelTasks-%d", tasks), func(b *testing.B) {
ParallelTasks(tasks)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Load(tmpfile)
}
})
}
}
// Benchmark URL parsing
func BenchmarkURLParsing(b *testing.B) {
Set("API_URL", "https://api.example.com/v1")
b.ResetTimer()
for i := 0; i < b.N; i++ {
var config testConfig
Unmarshal("", &config)
}
}
// Benchmark type conversion
func BenchmarkTypeConversion(b *testing.B) {
tests := map[string]string{
"INT": "12345",
"FLOAT": "123.45",
"BOOL": "true",
"STRING": "test_value",
}
for typ, val := range tests {
b.Run(typ, func(b *testing.B) {
Set("TEST_"+typ, val)
b.ResetTimer()
for i := 0; i < b.N; i++ {
switch typ {
case "INT":
var i int
unmarshalEnv("TEST_", &i)
case "FLOAT":
var f float64
unmarshalEnv("TEST_", &f)
case "BOOL":
var bo bool
unmarshalEnv("TEST_", &bo)
case "STRING":
var s string
unmarshalEnv("TEST_", &s)
}
}
})
}
}