Skip to content

Commit 3d6bf34

Browse files
Merge pull request #270 from foomo/feature/benchmarks
perf: add benchmarks
2 parents 2ac2f6c + 9e6e5e8 commit 3d6bf34

16 files changed

Lines changed: 569 additions & 43 deletions

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ go.work:
3838

3939
.PHONY: check
4040
## Run lint & tests
41-
check: tidy generate lint.fix test.race audit
41+
check: tidy generate lint.fix test audit
4242

4343
.PHONY: lint
4444
## Run linter

‎closer_bench_test.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package keel_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/foomo/keel"
8+
)
9+
10+
type benchCloser struct{}
11+
12+
func (benchCloser) Close() {}
13+
14+
type benchErrorCloser struct{}
15+
16+
func (benchErrorCloser) Close() error { return nil }
17+
18+
type benchCloserWithContext struct{}
19+
20+
func (benchCloserWithContext) Close(context.Context) {}
21+
22+
type benchNonCloser struct{}
23+
24+
func BenchmarkIsCloser(b *testing.B) {
25+
b.Run("first-case", func(b *testing.B) {
26+
v := benchCloser{}
27+
28+
for b.Loop() {
29+
keel.IsCloser(v)
30+
}
31+
})
32+
33+
b.Run("error-closer", func(b *testing.B) {
34+
v := benchErrorCloser{}
35+
36+
for b.Loop() {
37+
keel.IsCloser(v)
38+
}
39+
})
40+
41+
b.Run("context-closer", func(b *testing.B) {
42+
v := benchCloserWithContext{}
43+
44+
for b.Loop() {
45+
keel.IsCloser(v)
46+
}
47+
})
48+
49+
b.Run("non-match", func(b *testing.B) {
50+
v := benchNonCloser{}
51+
52+
for b.Loop() {
53+
keel.IsCloser(v)
54+
}
55+
})
56+
}

‎env/env.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func MustExists(key string) {
3333

3434
// Get env var or fallback
3535
func Get(key, fallback string) string {
36-
defaults.Store(key, fallback)
36+
if _, ok := defaults.Load(key); !ok {
37+
defaults.Store(key, fallback)
38+
}
3739

3840
if _, ok := types.Load(key); !ok {
3941
types.Store(key, "string")

‎env/env_bench_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package env_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/foomo/keel/env"
7+
)
8+
9+
func BenchmarkGet(b *testing.B) {
10+
b.Setenv("BENCH_ENV_STRING", "value")
11+
12+
for b.Loop() {
13+
env.Get("BENCH_ENV_STRING", "fallback")
14+
}
15+
}
16+
17+
func BenchmarkGetInt(b *testing.B) {
18+
b.Setenv("BENCH_ENV_INT", "42")
19+
20+
for b.Loop() {
21+
env.GetInt("BENCH_ENV_INT", 0)
22+
}
23+
}
24+
25+
func BenchmarkGetStringSlice(b *testing.B) {
26+
b.Setenv("BENCH_ENV_SLICE", "a,b,c,d,e")
27+
28+
for b.Loop() {
29+
env.GetStringSlice("BENCH_ENV_SLICE", nil)
30+
}
31+
}
32+
33+
func BenchmarkGetParallel(b *testing.B) {
34+
b.Setenv("BENCH_ENV_PARALLEL", "value")
35+
36+
b.RunParallel(func(pb *testing.PB) {
37+
for pb.Next() {
38+
env.Get("BENCH_ENV_PARALLEL", "fallback")
39+
}
40+
})
41+
}

‎log/fields_bench_test.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package log_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/foomo/keel/log"
7+
)
8+
9+
func BenchmarkFJSON(b *testing.B) {
10+
b.Run("simple", func(b *testing.B) {
11+
v := map[string]string{"key": "value"}
12+
13+
for b.Loop() {
14+
log.FJSON(v)
15+
}
16+
})
17+
18+
b.Run("nested", func(b *testing.B) {
19+
v := map[string]any{
20+
"key": "value",
21+
"number": 42,
22+
"nested": map[string]any{
23+
"inner": "data",
24+
"list": []int{1, 2, 3},
25+
},
26+
}
27+
28+
for b.Loop() {
29+
log.FJSON(v)
30+
}
31+
})
32+
}
33+
34+
func BenchmarkFValue(b *testing.B) {
35+
b.Run("string", func(b *testing.B) {
36+
for b.Loop() {
37+
log.FValue("hello")
38+
}
39+
})
40+
41+
b.Run("int", func(b *testing.B) {
42+
for b.Loop() {
43+
log.FValue(42)
44+
}
45+
})
46+
47+
b.Run("struct", func(b *testing.B) {
48+
v := struct {
49+
Name string
50+
Age int
51+
}{"Alice", 30}
52+
53+
for b.Loop() {
54+
log.FValue(v)
55+
}
56+
})
57+
}

‎log/labeler_bench_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package log_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/foomo/keel/log"
7+
"go.uber.org/zap"
8+
)
9+
10+
func BenchmarkLabelerAdd(b *testing.B) {
11+
field := zap.String("key", "value")
12+
13+
for b.Loop() {
14+
l := &log.Labeler{}
15+
l.Add(field)
16+
}
17+
}
18+
19+
func BenchmarkLabelerGet(b *testing.B) {
20+
l := &log.Labeler{}
21+
for range 10 {
22+
l.Add(zap.String("key", "value"))
23+
}
24+
25+
for b.Loop() {
26+
l.Get()
27+
}
28+
}
29+
30+
func BenchmarkLabelerAddParallel(b *testing.B) {
31+
l := &log.Labeler{}
32+
field := zap.String("key", "value")
33+
34+
b.RunParallel(func(pb *testing.PB) {
35+
for pb.Next() {
36+
l.Add(field)
37+
}
38+
})
39+
}

‎net/http/middleware/cors.go‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ func CORS(opts ...CORSOption) keelhttp.Middleware {
8989

9090
// CORSWithOptions middleware
9191
func CORSWithOptions(opts CORSOptions) keelhttp.Middleware {
92-
allowOriginPatterns := make([]string, len(opts.AllowOrigins))
92+
allowOriginPatterns := make([]*regexp.Regexp, len(opts.AllowOrigins))
9393
for i, origin := range opts.AllowOrigins {
9494
pattern := regexp.QuoteMeta(origin)
9595
pattern = strings.ReplaceAll(pattern, "\\*", ".*")
9696
pattern = strings.ReplaceAll(pattern, "\\?", ".")
9797
pattern = "^" + pattern + "$"
98-
allowOriginPatterns[i] = pattern
98+
allowOriginPatterns[i] = regexp.MustCompile(pattern)
9999
}
100100

101101
allowMethods := strings.Join(opts.AllowMethods, ",")
@@ -159,7 +159,7 @@ func CORSWithOptions(opts CORSOptions) keelhttp.Middleware {
159159
break
160160
}
161161

162-
if match, _ := regexp.MatchString(re, origin); match {
162+
if re.MatchString(origin) {
163163
allowOrigin = origin
164164
break
165165
}
@@ -245,30 +245,28 @@ func matchSubdomain(domain, pattern string) bool {
245245
return false
246246
}
247247

248-
domComp := strings.Split(domAuth, ".")
249-
patComp := strings.Split(patAuth, ".")
248+
// Compare components right-to-left without allocating slices.
249+
// Walk backward through both strings, extracting one dot-delimited
250+
// component at a time and comparing them.
251+
domPos := len(domAuth)
252+
patPos := len(patAuth)
250253

251-
for i := len(domComp)/2 - 1; i >= 0; i-- {
252-
opp := len(domComp) - 1 - i
253-
domComp[i], domComp[opp] = domComp[opp], domComp[i]
254-
}
255-
256-
for i := len(patComp)/2 - 1; i >= 0; i-- {
257-
opp := len(patComp) - 1 - i
258-
patComp[i], patComp[opp] = patComp[opp], patComp[i]
259-
}
254+
for domPos > 0 && patPos > 0 {
255+
// Extract rightmost remaining component from domain.
256+
domDot := strings.LastIndexByte(domAuth[:domPos], '.')
257+
domSeg := domAuth[domDot+1 : domPos]
258+
domPos = domDot // -1 when no more dots, which terminates next iteration
260259

261-
for i, v := range domComp {
262-
if len(patComp) <= i {
263-
return false
264-
}
260+
// Extract rightmost remaining component from pattern.
261+
patDot := strings.LastIndexByte(patAuth[:patPos], '.')
262+
patSeg := patAuth[patDot+1 : patPos]
263+
patPos = patDot
265264

266-
p := patComp[i]
267-
if p == "*" {
265+
if patSeg == "*" {
268266
return true
269267
}
270268

271-
if p != v {
269+
if patSeg != domSeg {
272270
return false
273271
}
274272
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package middleware_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
keelhttp "github.com/foomo/keel/net/http"
9+
"github.com/foomo/keel/net/http/middleware"
10+
"go.uber.org/zap"
11+
)
12+
13+
func BenchmarkMatchSubdomain(b *testing.B) {
14+
cases := []struct {
15+
name string
16+
domain string
17+
pattern string
18+
}{
19+
{"exact", "https://www.foomo.org", "https://www.foomo.org"},
20+
{"wildcard-shallow", "https://www.foomo.org", "https://*.foomo.org"},
21+
{"wildcard-deep", "https://a.b.c.d.foomo.org", "https://*.foomo.org"},
22+
{"no-match", "https://www.example.com", "https://*.foomo.org"},
23+
{"scheme-mismatch", "http://www.foomo.org", "https://*.foomo.org"},
24+
}
25+
26+
for _, tc := range cases {
27+
b.Run(tc.name, func(b *testing.B) {
28+
for b.Loop() {
29+
middleware.BenchMatchSubdomain(tc.domain, tc.pattern)
30+
}
31+
})
32+
}
33+
}
34+
35+
func BenchmarkCORSOriginMatching(b *testing.B) {
36+
origins := []string{
37+
"https://app.foomo.org",
38+
"https://api.foomo.org",
39+
"https://*.example.com",
40+
}
41+
42+
handler := middleware.CORSWithOptions(middleware.CORSOptions{
43+
AllowOrigins: origins,
44+
AllowMethods: []string{http.MethodGet, http.MethodPost},
45+
})
46+
47+
l := zap.NewNop()
48+
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
49+
h := handler(l, "bench", next)
50+
51+
cases := []struct {
52+
name string
53+
origin string
54+
method string
55+
}{
56+
{"exact-match", "https://app.foomo.org", http.MethodGet},
57+
{"pattern-match", "https://sub.example.com", http.MethodGet},
58+
{"no-match", "https://evil.com", http.MethodGet},
59+
{"preflight-match", "https://app.foomo.org", http.MethodOptions},
60+
}
61+
62+
for _, tc := range cases {
63+
b.Run(tc.name, func(b *testing.B) {
64+
r := httptest.NewRequestWithContext(b.Context(), tc.method, "https://localhost/", nil)
65+
r.Header.Set(keelhttp.HeaderOrigin, tc.origin)
66+
67+
w := httptest.NewRecorder()
68+
69+
for b.Loop() {
70+
h.ServeHTTP(w, r)
71+
w.Body.Reset()
72+
}
73+
})
74+
}
75+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package middleware //nolint:testpackage
2+
3+
var BenchMatchSubdomain = matchSubdomain

0 commit comments

Comments
 (0)