Skip to content

Commit 06ee601

Browse files
committed
Add trivial benchmark tests
1 parent 2172815 commit 06ee601

File tree

2 files changed

+112
-20
lines changed

2 files changed

+112
-20
lines changed

jwt_benchmark_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package echojwt
2+
3+
import (
4+
"github.com/golang-jwt/jwt/v4"
5+
"github.com/labstack/echo/v4"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func BenchmarkJWTSuccessPath(b *testing.B) {
12+
e := echo.New()
13+
14+
e.GET("/", func(c echo.Context) error {
15+
token := c.Get("user").(*jwt.Token)
16+
return c.JSON(http.StatusTeapot, token.Claims)
17+
})
18+
19+
b.ReportAllocs()
20+
mw, err := Config{SigningKey: []byte("secret")}.ToMiddleware()
21+
if err != nil {
22+
b.Fatal(err)
23+
}
24+
e.Use(mw)
25+
26+
b.ResetTimer()
27+
for i := 0; i < b.N; i++ {
28+
req := httptest.NewRequest(http.MethodGet, "/", nil)
29+
req.Header.Set(echo.HeaderAuthorization, "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ")
30+
res := httptest.NewRecorder()
31+
32+
e.ServeHTTP(res, req)
33+
34+
if res.Code != http.StatusUnauthorized {
35+
b.Failed()
36+
}
37+
}
38+
}
39+
40+
func BenchmarkJWTErrorPath(b *testing.B) {
41+
e := echo.New()
42+
43+
e.GET("/", func(c echo.Context) error {
44+
token := c.Get("user").(*jwt.Token)
45+
return c.JSON(http.StatusTeapot, token.Claims)
46+
})
47+
48+
b.ReportAllocs()
49+
mw, err := Config{SigningKey: []byte("secret")}.ToMiddleware()
50+
if err != nil {
51+
b.Fatal(err)
52+
}
53+
e.Use(mw)
54+
55+
b.ResetTimer()
56+
for i := 0; i < b.N; i++ {
57+
req := httptest.NewRequest(http.MethodGet, "/", nil)
58+
req.Header.Set(echo.HeaderAuthorization, "Bearer x.x.x")
59+
res := httptest.NewRecorder()
60+
61+
e.ServeHTTP(res, req)
62+
63+
if res.Code != http.StatusUnauthorized {
64+
b.Failed()
65+
}
66+
}
67+
}

jwt_test.go

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http/httptest"
1111
"net/url"
1212
"strings"
13-
"sync"
1413
"testing"
1514

1615
"github.com/golang-jwt/jwt/v4"
@@ -753,28 +752,54 @@ func TestWithConfig_panic(t *testing.T) {
753752
)
754753
}
755754

756-
func TestToMiddlewareRace(t *testing.T) {
757-
e := echo.New()
758-
mw, err := Config{
759-
ParseTokenFunc: func(c echo.Context, auth string) (interface{}, error) {
760-
return auth, nil
755+
func TestDataRacesOnParallelExecution(t *testing.T) {
756+
var testCases = []struct {
757+
name string
758+
whenHeader string
759+
expectCode int
760+
}{ // run multiple cases in parallel to catch data races
761+
{
762+
name: "ok",
763+
whenHeader: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ",
764+
expectCode: http.StatusTeapot,
761765
},
762-
SuccessHandler: func(c echo.Context) {
763-
c.Set("success", "yes")
766+
{
767+
name: "nok",
768+
whenHeader: "Bearer x.x.x",
769+
expectCode: http.StatusUnauthorized,
764770
},
765-
}.ToMiddleware()
766-
assert.NoError(t, err)
771+
{
772+
name: "nok, simulatenous error",
773+
whenHeader: "Bearer x.x.x",
774+
expectCode: http.StatusUnauthorized,
775+
},
776+
}
767777

768-
dummyHandler := func(_ echo.Context) error { return nil }
769-
var wg sync.WaitGroup
770-
for i := 0; i < 10; i++ {
771-
wg.Add(1)
772-
go func() {
773-
defer wg.Done()
774-
for j := 0; j < 10; j++ {
775-
mw(dummyHandler)(e.NewContext(httptest.NewRequest("", "/", nil), httptest.NewRecorder()))
778+
e := echo.New()
779+
e.GET("/", func(c echo.Context) error {
780+
token := c.Get("user").(*jwt.Token)
781+
return c.JSON(http.StatusTeapot, token.Claims)
782+
})
783+
784+
mw, err := Config{SigningKey: []byte("secret")}.ToMiddleware()
785+
if err != nil {
786+
t.Fatal(err)
787+
}
788+
e.Use(mw)
789+
790+
for _, tc := range testCases {
791+
t.Run(tc.name, func(t *testing.T) {
792+
t.Parallel()
793+
794+
req := httptest.NewRequest(http.MethodGet, "/", nil)
795+
req.Header.Set(echo.HeaderAuthorization, tc.whenHeader)
796+
res := httptest.NewRecorder()
797+
798+
e.ServeHTTP(res, req)
799+
800+
if res.Code != tc.expectCode {
801+
t.Failed()
776802
}
777-
}()
803+
})
778804
}
779-
wg.Wait()
780805
}

0 commit comments

Comments
 (0)