mirror of https://github.com/gofiber/fiber.git
v3: fix benchmark results (#1982)
* 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>pull/2000/head^2
parent
a458bd344c
commit
adcb1f2bf1
|
@ -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:
|
||||
|
|
4
ctx.go
4
ctx.go
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
30
ctx_test.go
30
ctx_test.go
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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")
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
|
44
helpers.go
44
helpers.go
|
@ -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 {
|
||||
|
|
13
router.go
13
router.go
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue