Added Server interface

This commit is contained in:
2024-03-14 12:52:03 +01:00
parent e604017ecc
commit 1e4161de0c
6 changed files with 135 additions and 67 deletions

View File

@ -2,38 +2,62 @@ package server_test
import (
"net/http/httptest"
"strings"
"testing"
"git.akyoto.dev/go/router/testdata"
"git.akyoto.dev/go/server"
)
func BenchmarkHello(b *testing.B) {
request := httptest.NewRequest("GET", "/", nil)
response := &NullResponse{}
func BenchmarkStatic(b *testing.B) {
paths := []string{
"/",
"/hello",
"/hello/world",
}
s := server.New()
s.Get("/", func(ctx server.Context) error {
return ctx.String("Hello")
})
for _, path := range paths {
s.Get(path, func(ctx server.Context) error {
return ctx.String("Hello")
})
}
for range b.N {
s.ServeHTTP(response, request)
for _, path := range paths {
b.Run(strings.TrimPrefix(path, "/"), func(b *testing.B) {
request := httptest.NewRequest("GET", path, nil)
response := &NullResponse{}
for range b.N {
s.ServeHTTP(response, request)
}
})
}
}
func BenchmarkGitHub(b *testing.B) {
request := httptest.NewRequest("GET", "/repos/:owner/:repo", nil)
response := &NullResponse{}
paths := []string{
"/gists/:id",
"/repos/:a/:b",
}
s := server.New()
for _, route := range testdata.Routes("testdata/github.txt") {
s.Router.Add(route.Method, route.Path, func(server.Context) error {
return nil
s.Router().Add(route.Method, route.Path, func(ctx server.Context) error {
return ctx.String("Hello")
})
}
for range b.N {
s.ServeHTTP(response, request)
for _, path := range paths {
b.Run(strings.TrimPrefix(path, "/"), func(b *testing.B) {
request := httptest.NewRequest("GET", path, nil)
response := &NullResponse{}
for range b.N {
s.ServeHTTP(response, request)
}
})
}
}