From f63bd1bb2f299b1b0b80c50157931937b480531d Mon Sep 17 00:00:00 2001 From: Moritz Marquardt Date: Thu, 8 Jul 2021 23:08:58 +0200 Subject: [PATCH] Add timing tests --- handler_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 handler_test.go diff --git a/handler_test.go b/handler_test.go new file mode 100644 index 0000000..3a6d4b2 --- /dev/null +++ b/handler_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "github.com/valyala/fasthttp" + "testing" + "time" +) + +func TestHandlerPerformance(t *testing.T) { + ctx := &fasthttp.RequestCtx{ + Request: *fasthttp.AcquireRequest(), + Response: *fasthttp.AcquireResponse(), + } + ctx.Request.SetRequestURI("http://mondstern.codeberg:8080/") + fmt.Printf("Start: %v\n", time.Now()) + start := time.Now() + handler(ctx) + end := time.Now() + fmt.Printf("Done: %v\n", time.Now()) + if ctx.Response.StatusCode() != 200 || len(ctx.Response.Body()) < 2048 { + t.Errorf("request failed with status code %d and body length %d", ctx.Response.StatusCode(), len(ctx.Response.Body())) + } else { + t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds()) + } + + ctx.Response.Reset() + ctx.Response.ResetBody() + fmt.Printf("Start: %v\n", time.Now()) + start = time.Now() + handler(ctx) + end = time.Now() + fmt.Printf("Done: %v\n", time.Now()) + if ctx.Response.StatusCode() != 200 || len(ctx.Response.Body()) < 2048 { + t.Errorf("request failed with status code %d and body length %d", ctx.Response.StatusCode(), len(ctx.Response.Body())) + } else { + t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds()) + } +}