Update test & benchmark (#374)

Co-Authored-By: RW <renewerner87@googlemail.com>

* Add nosec for WriteByte

* test persistens for benchmark results

* Add tests & benchmark

* Update app_test.go

* Update benchmark.yml
pull/380/head
Fenny 2020-05-13 20:21:49 +02:00 committed by GitHub
parent f5ad2c0766
commit e0b13d9ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 9468 additions and 9378 deletions

View File

@ -22,7 +22,7 @@ jobs:
with:
tool: 'go'
output-file-path: output.txt
github-token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
github-token: ${{ secrets.BENCHMARK_TOKEN }}
fail-on-alert: true
comment-on-alert: true
auto-push: true

20
app.go
View File

@ -186,6 +186,16 @@ func (app *App) Use(args ...interface{}) *App {
return app
}
// Add : https://fiber.wiki/application#http-methods
func (app *App) Add(method, path string, handlers ...func(*Ctx)) *App {
method = strings.ToUpper(method)
if methodINT[method] == 0 && method != "GET" {
log.Fatalf("Add: Invalid HTTP method %s", method)
}
app.registerMethod(method, path, handlers...)
return app
}
// Connect : https://fiber.wiki/application#http-methods
func (app *App) Connect(path string, handlers ...func(*Ctx)) *App {
app.registerMethod(MethodConnect, path, handlers...)
@ -285,6 +295,16 @@ func (grp *Group) Use(args ...interface{}) *Group {
return grp
}
// Add : https://fiber.wiki/application#http-methods
func (grp *Group) Add(method, path string, handlers ...func(*Ctx)) *Group {
method = strings.ToUpper(method)
if methodINT[method] == 0 && method != "GET" {
log.Fatalf("Add: Invalid HTTP method %s", method)
}
grp.app.registerMethod(method, getGroupPath(grp.prefix, path), handlers...)
return grp
}
// Connect : https://fiber.wiki/application#http-methods
func (grp *Group) Connect(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodConnect, getGroupPath(grp.prefix, path), handlers...)

View File

@ -63,6 +63,24 @@ func Test_App_Use_Params(t *testing.T) {
assertEqual(t, nil, err, "app.Test(req)")
assertEqual(t, 200, resp.StatusCode, "Status code")
}
func Test_App_Use_Params_Group(t *testing.T) {
app := New()
group := app.Group("/prefix/:param/*")
group.Use("/", func(c *Ctx) {
c.Next()
})
group.Get("/test", func(c *Ctx) {
assertEqual(t, "john", c.Params("param"))
assertEqual(t, "doe", c.Params("*"))
})
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/john/doe/test", nil))
assertEqual(t, nil, err, "app.Test(req)")
assertEqual(t, 200, resp.StatusCode, "Status code")
}
func Test_App_Order(t *testing.T) {
app := New()
@ -241,16 +259,18 @@ func Test_App_Listen(t *testing.T) {
DisableStartupMessage: true,
})
go func() {
time.Sleep(1 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
assertEqual(t, nil, app.Shutdown())
}()
assertEqual(t, nil, app.Listen(3002))
assertEqual(t, nil, app.Listen(4003))
go func() {
time.Sleep(500 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
assertEqual(t, nil, app.Shutdown())
}()
assertEqual(t, nil, app.Listen("3003"))
assertEqual(t, nil, app.Listen("4010"))
}
func Test_App_Serve(t *testing.T) {
@ -258,11 +278,11 @@ func Test_App_Serve(t *testing.T) {
DisableStartupMessage: true,
Prefork: true,
})
ln, err := net.Listen("tcp4", ":3004")
ln, err := net.Listen("tcp4", ":4020")
assertEqual(t, nil, err)
go func() {
time.Sleep(500 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
assertEqual(t, nil, app.Shutdown())
}()

2
ctx.go
View File

@ -24,7 +24,7 @@ import (
"time"
schema "github.com/gorilla/schema"
"github.com/valyala/bytebufferpool"
bytebufferpool "github.com/valyala/bytebufferpool"
fasthttp "github.com/valyala/fasthttp"
)

View File

@ -13,7 +13,7 @@ import (
"github.com/valyala/fasthttp"
)
// go test -v ./... -run=^$ -bench=Benchmark_Ctx_Params -benchmem -count=3
// go test -v ./... -run=^$ -bench=Benchmark_Ctx -benchmem -count=3
func Benchmark_Ctx_Accepts(b *testing.B) {
c := AcquireCtx(&fasthttp.RequestCtx{})

View File

@ -7,7 +7,10 @@ package fiber
// go test -v ./... -run=^$ -bench=Benchmark_Router -benchmem -count=3
import (
"strings"
"testing"
fasthttp "github.com/valyala/fasthttp"
)
var routerBenchApp *App
@ -38,6 +41,53 @@ func init() {
}
}
func Benchmark_Router_CaseSensitive(b *testing.B) {
var path = "/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts"
var res string
for n := 0; n < b.N; n++ {
res = strings.ToLower(path)
}
assertEqual(b, "/repos/gofiber/fiber/issues/187643/comments", res)
}
func Benchmark_Router_StrictRouting(b *testing.B) {
var path = "/repos/gofiber/fiber/issues/187643/comments/"
var res string
for n := 0; n < b.N; n++ {
res = strings.TrimRight(path, "/")
}
assertEqual(b, "/repos/gofiber/fiber/issues/187643/comments", res)
}
func Benchmark_Router_Handler(b *testing.B) {
c := &fasthttp.RequestCtx{}
c.Request.Header.SetMethod("DELETE")
c.URI().SetPath("/user/keys/1337")
for n := 0; n < b.N; n++ {
routerBenchApp.handler(c)
}
}
func Benchmark_Router_NextRoute(b *testing.B) {
c := AcquireCtx(&fasthttp.RequestCtx{})
defer ReleaseCtx(c)
c.Fasthttp.Request.Header.SetMethod("DELETE")
c.Fasthttp.URI().SetPath("/user/keys/1337")
for n := 0; n < b.N; n++ {
routerBenchApp.nextRoute(c)
}
assertEqual(b, len(githubAPI)+1, c.index-1)
}
// go test -v ./... -run=^$ -bench=Benchmark_Router_Next_Stack -benchmem -count=3
func Benchmark_Router_Next_Stack(b *testing.B) {
for n := 0; n < b.N; n++ {

View File

@ -209,11 +209,11 @@ var methodINT = map[string]int{
MethodHead: 1,
MethodPost: 2,
MethodPut: 3,
MethodPatch: 4,
MethodDelete: 5,
MethodConnect: 6,
MethodOptions: 7,
MethodTrace: 8,
MethodDelete: 4,
MethodConnect: 5,
MethodOptions: 6,
MethodTrace: 7,
MethodPatch: 8,
}
// HTTP status codes were copied from net/http.

View File

@ -82,15 +82,15 @@ func Test_Utils_methodINT(t *testing.T) {
assertEqual(t, 2, res)
res = methodINT[MethodPut]
assertEqual(t, 3, res)
res = methodINT[MethodPatch]
assertEqual(t, 4, res)
res = methodINT[MethodDelete]
assertEqual(t, 5, res)
assertEqual(t, 4, res)
res = methodINT[MethodConnect]
assertEqual(t, 6, res)
assertEqual(t, 5, res)
res = methodINT[MethodOptions]
assertEqual(t, 7, res)
assertEqual(t, 6, res)
res = methodINT[MethodTrace]
assertEqual(t, 7, res)
res = methodINT[MethodPatch]
assertEqual(t, 8, res)
}