pages-server/server/handler_test.go

71 lines
2.1 KiB
Go
Raw Normal View History

2021-12-05 13:45:17 +00:00
package server
2021-07-08 21:08:58 +00:00
import (
"fmt"
"testing"
"time"
2021-12-05 14:02:44 +00:00
"github.com/valyala/fasthttp"
2021-12-05 14:02:44 +00:00
"codeberg.org/codeberg/pages/server/cache"
2021-07-08 21:08:58 +00:00
)
func TestHandlerPerformance(t *testing.T) {
2021-12-05 13:45:17 +00:00
testHandler := Handler(
[]byte("codeberg.page"),
[]byte("raw.codeberg.org"),
2021-12-03 02:46:21 +00:00
"https://codeberg.org",
2021-12-05 13:45:17 +00:00
"https://docs.codeberg.org/pages/raw-content/",
"",
[][]byte{[]byte("/.well-known/acme-challenge/")},
[][]byte{[]byte("raw.codeberg.org"), []byte("fonts.codeberg.org"), []byte("design.codeberg.org")},
2021-12-05 14:02:44 +00:00
cache.NewKeyValueCache(),
cache.NewKeyValueCache(),
2021-12-05 14:53:46 +00:00
cache.NewKeyValueCache(),
cache.NewKeyValueCache(),
2021-12-05 13:45:17 +00:00
)
2021-07-08 21:08:58 +00:00
ctx := &fasthttp.RequestCtx{
Request: *fasthttp.AcquireRequest(),
Response: *fasthttp.AcquireResponse(),
}
2021-07-13 08:28:06 +00:00
ctx.Request.SetRequestURI("http://mondstern.codeberg.page/")
2021-07-08 21:08:58 +00:00
fmt.Printf("Start: %v\n", time.Now())
start := time.Now()
2021-12-05 13:45:17 +00:00
testHandler(ctx)
2021-07-08 21:08:58 +00:00
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()
2021-12-05 13:45:17 +00:00
testHandler(ctx)
2021-07-08 21:08:58 +00:00
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())
}
2021-07-13 08:28:06 +00:00
ctx.Response.Reset()
ctx.Response.ResetBody()
ctx.Request.SetRequestURI("http://example.momar.xyz/")
fmt.Printf("Start: %v\n", time.Now())
start = time.Now()
2021-12-05 13:45:17 +00:00
testHandler(ctx)
2021-07-13 08:28:06 +00:00
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())
}
2021-07-08 21:08:58 +00:00
}