refactor test

This commit is contained in:
6543 2022-03-27 20:24:13 +02:00
parent f5d0dc7447
commit 33a888c748
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE

View file

@ -25,46 +25,25 @@ func TestHandlerPerformance(t *testing.T) {
cache.NewKeyValueCache(), cache.NewKeyValueCache(),
) )
ctx := &fasthttp.RequestCtx{ testCase := func(uri string, status int) {
Request: *fasthttp.AcquireRequest(), ctx := &fasthttp.RequestCtx{
Response: *fasthttp.AcquireResponse(), Request: *fasthttp.AcquireRequest(),
} Response: *fasthttp.AcquireResponse(),
ctx.Request.SetRequestURI("http://mondstern.codeberg.page/") }
fmt.Printf("Start: %v\n", time.Now()) ctx.Request.SetRequestURI(uri)
start := time.Now() fmt.Printf("Start: %v\n", time.Now())
testHandler(ctx) start := time.Now()
end := time.Now() testHandler(ctx)
fmt.Printf("Done: %v\n", time.Now()) end := time.Now()
if ctx.Response.StatusCode() != 200 || len(ctx.Response.Body()) < 2048 { fmt.Printf("Done: %v\n", time.Now())
t.Errorf("request failed with status code %d and body length %d", ctx.Response.StatusCode(), len(ctx.Response.Body())) if ctx.Response.StatusCode() != status {
} else { t.Errorf("request failed with status code %d", ctx.Response.StatusCode())
t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds()) } else {
t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds())
}
} }
ctx.Response.Reset() testCase("https://mondstern.codeberg.page/", 200)
ctx.Response.ResetBody() testCase("https://mondstern.codeberg.page/", 200)
fmt.Printf("Start: %v\n", time.Now()) testCase("https://example.momar.xyz/", 200)
start = time.Now()
testHandler(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()
ctx.Request.SetRequestURI("http://example.momar.xyz/")
fmt.Printf("Start: %v\n", time.Now())
start = time.Now()
testHandler(ctx)
end = time.Now()
fmt.Printf("Done: %v\n", time.Now())
if ctx.Response.StatusCode() != 200 || len(ctx.Response.Body()) < 1 {
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())
}
} }