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