Skip to content

Commit

Permalink
v3: fix benchmark results (#1982)
Browse files Browse the repository at this point in the history
* v3: fix benchmark results

* v3: fix benchmark results

* Fix Benchmark_Router_Github_API

* reduce duplicated tests run actions

* reduce duplicated tests run actions

* fix some benchmarks

Co-authored-by: wernerr <rene@gofiber.io>
  • Loading branch information
efectn and ReneWerner87 committed Aug 7, 2022
1 parent a458bd3 commit adcb1f2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 26 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
name: Test
jobs:
Build:
strategy:
matrix:
go-version: [1.18.x]
go-version: [1.19.x]
platform: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
4 changes: 2 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ func (c *DefaultCtx) Next() (err error) {
err = c.route.Handlers[c.indexHandler](c)
} else {
// Continue handler stack
_, err = c.app.next(c)
_, err = c.app.next(c, c.app.newCtxFunc != nil)
}
return err
}
Expand All @@ -770,7 +770,7 @@ func (c *DefaultCtx) Next() (err error) {
// changing the request path. Note that handlers might be executed again.
func (c *DefaultCtx) RestartRouting() error {
c.indexRoute = -1
_, err := c.app.next(c)
_, err := c.app.next(c, c.app.newCtxFunc != nil)
return err
}

Expand Down
30 changes: 15 additions & 15 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_Ctx_Accepts(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Accepts -benchmem -count=4
func Benchmark_Ctx_Accepts(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9")
var res string
Expand Down Expand Up @@ -141,7 +141,7 @@ func Test_Ctx_AcceptsCharsets(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_AcceptsCharsets -benchmem -count=4
func Benchmark_Ctx_AcceptsCharsets(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
var res string
Expand All @@ -167,7 +167,7 @@ func Test_Ctx_AcceptsEncodings(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_AcceptsEncodings -benchmem -count=4
func Benchmark_Ctx_AcceptsEncodings(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().Header.Set(HeaderAcceptEncoding, "deflate, gzip;q=1.0, *;q=0.5")
var res string
Expand All @@ -192,7 +192,7 @@ func Test_Ctx_AcceptsLanguages(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_AcceptsLanguages -benchmem -count=4
func Benchmark_Ctx_AcceptsLanguages(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().Header.Set(HeaderAcceptLanguage, "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5")
var res string
Expand Down Expand Up @@ -253,7 +253,7 @@ func Test_Ctx_Append(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Append -benchmem -count=4
func Benchmark_Ctx_Append(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -286,7 +286,7 @@ func Test_Ctx_Attachment(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Attachment -benchmem -count=4
func Benchmark_Ctx_Attachment(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -312,7 +312,7 @@ func Test_Ctx_BaseURL(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_BaseURL -benchmem
func Benchmark_Ctx_BaseURL(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().SetHost("google.com:1337")
c.Request().URI().SetPath("/haha/oke/lol")
Expand Down Expand Up @@ -722,7 +722,7 @@ func Test_Ctx_Cookie(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Cookie -benchmem -count=4
func Benchmark_Ctx_Cookie(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -2100,7 +2100,7 @@ func Test_Ctx_JSONP(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_JSONP -benchmem -count=4
func Benchmark_Ctx_JSONP(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

type SomeStruct struct {
Name string
Expand Down Expand Up @@ -2140,7 +2140,7 @@ func Test_Ctx_Links(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Links -benchmem -count=4
func Benchmark_Ctx_Links(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -2539,7 +2539,7 @@ func Benchmark_Ctx_RedirectToRoute(b *testing.B) {
return c.JSON(c.Params("name"))
}).Name("user")

c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -2560,7 +2560,7 @@ func Benchmark_Ctx_RedirectToRouteWithQueries(b *testing.B) {
return c.JSON(c.Params("name"))
}).Name("user")

c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -3018,7 +3018,7 @@ func Benchmark_Ctx_Type(b *testing.B) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Type_Charset -benchmem -count=4
func Benchmark_Ctx_Type_Charset(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -3043,7 +3043,7 @@ func Test_Ctx_Vary(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Vary -benchmem -count=4
func Benchmark_Ctx_Vary(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -3090,7 +3090,7 @@ func Test_Ctx_Writef(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_Ctx_Writef -benchmem -count=4
func Benchmark_Ctx_Writef(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})
c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

world := "World!"
b.ReportAllocs()
Expand Down
44 changes: 43 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,49 @@ func (app *App) quoteString(raw string) string {
}

// Scan stack if other methods match the request
func methodExist(c CustomCtx) (exist bool) {
func methodExist(c *DefaultCtx) (exist bool) {
for i := 0; i < len(intMethod); i++ {
// Skip original method
if c.getMethodINT() == i {
continue
}
// Reset stack index
c.setIndexRoute(-1)

tree, ok := c.App().treeStack[i][c.getTreePath()]
if !ok {
tree = c.App().treeStack[i][""]
}
// Get stack length
lenr := len(tree) - 1
// Loop over the route stack starting from previous index
for c.getIndexRoute() < lenr {
// Increment route index
c.setIndexRoute(c.getIndexRoute() + 1)
// Get *Route
route := tree[c.getIndexRoute()]
// Skip use routes
if route.use {
continue
}
// Check if it matches the request path
match := route.match(c.getDetectionPath(), c.Path(), c.getValues())
// No match, next route
if match {
// We matched
exist = true
// Add method to Allow header
c.Append(HeaderAllow, intMethod[i])
// Break stack loop
break
}
}
}
return
}

// Scan stack if other methods match the request
func methodExistCustom(c CustomCtx) (exist bool) {
for i := 0; i < len(intMethod); i++ {
// Skip original method
if c.getMethodINT() == i {
Expand Down
13 changes: 10 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *Route) match(detectionPath, path string, params *[maxParams]string) (ma
return false
}

func (app *App) next(c CustomCtx) (match bool, err error) {
func (app *App) next(c CustomCtx, customCtx bool) (match bool, err error) {
// Get stack length
tree, ok := app.treeStack[c.getMethodINT()][c.getTreePath()]
if !ok {
Expand Down Expand Up @@ -136,9 +136,16 @@ func (app *App) next(c CustomCtx) (match bool, err error) {
// If c.Next() does not match, return 404
err = NewErrors(StatusNotFound, "Cannot "+c.Method()+" "+c.getPathOriginal())

var isMethodExist bool
if customCtx {
isMethodExist = methodExistCustom(c)
} else {
isMethodExist = methodExist(c.(*DefaultCtx))
}

// If no match, scan stack again if other methods match the request
// Moved from app.handler because middleware may break the route chain
if !c.getMatched() && methodExist(c) {
if !c.getMatched() && isMethodExist {
err = ErrMethodNotAllowed
}
return
Expand All @@ -161,7 +168,7 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
}

// Find match in stack
_, err := app.next(c)
_, err := app.next(c, app.newCtxFunc != nil)
if err != nil {
if catch := c.App().ErrorHandler(c, err); catch != nil {
_ = c.SendStatus(StatusInternalServerError)
Expand Down
11 changes: 8 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func Benchmark_Router_Next(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.indexRoute = -1
res, err = app.next(c)
res, err = app.next(c, false)
}
utils.AssertEqual(b, nil, err)
utils.AssertEqual(b, true, res)
Expand Down Expand Up @@ -770,9 +770,14 @@ func Benchmark_Router_Github_API(b *testing.B) {
c.Request.Header.SetMethod(routesFixture.TestRoutes[i].Method)
for n := 0; n < b.N; n++ {
c.URI().SetPath(routesFixture.TestRoutes[i].Path)
ctx := app.NewCtx(c)
match, err = app.next(ctx.(CustomCtx))

ctx := app.AcquireCtx().(CustomCtx)
ctx.Reset(c)

match, err = app.next(ctx, false)
app.ReleaseCtx(ctx)
}

utils.AssertEqual(b, nil, err)
utils.AssertEqual(b, true, match)
}
Expand Down

0 comments on commit adcb1f2

Please sign in to comment.