mirror of
https://github.com/dunglas/frankenphp.git
synced 2025-12-24 13:38:11 +08:00
tests: improve benchmarks
This commit is contained in:
@@ -782,8 +782,9 @@ func BenchmarkHelloWorld(b *testing.B) {
|
||||
cwd, _ := os.Getwd()
|
||||
testDataDir := cwd + "/testdata/"
|
||||
|
||||
opt := frankenphp.WithRequestDocumentRoot(testDataDir, false)
|
||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false))
|
||||
req, err := frankenphp.NewRequestWithContext(r, opt)
|
||||
require.NoError(b, err)
|
||||
|
||||
require.NoError(b, frankenphp.ServeHTTP(w, req))
|
||||
@@ -804,8 +805,9 @@ func BenchmarkEcho(b *testing.B) {
|
||||
cwd, _ := os.Getwd()
|
||||
testDataDir := cwd + "/testdata/"
|
||||
|
||||
opt := frankenphp.WithRequestDocumentRoot(testDataDir, false)
|
||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false))
|
||||
req, err := frankenphp.NewRequestWithContext(r, opt)
|
||||
require.NoError(b, err)
|
||||
|
||||
require.NoError(b, frankenphp.ServeHTTP(w, req))
|
||||
@@ -907,8 +909,57 @@ func BenchmarkServerSuperGlobal(b *testing.B) {
|
||||
|
||||
preparedEnv := frankenphp.PrepareEnv(env)
|
||||
|
||||
opts := []frankenphp.RequestOption{frankenphp.WithRequestDocumentRoot(testDataDir, false), frankenphp.WithRequestPreparedEnv(preparedEnv)}
|
||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false), frankenphp.WithRequestPreparedEnv(preparedEnv))
|
||||
req, err := frankenphp.NewRequestWithContext(r, opts...)
|
||||
require.NoError(b, err)
|
||||
|
||||
r.Header = headers
|
||||
|
||||
require.NoError(b, frankenphp.ServeHTTP(w, req))
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "http://example.com/server-variable.php", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
for b.Loop() {
|
||||
handler(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUncommonHeaders(b *testing.B) {
|
||||
require.NoError(b, frankenphp.Init())
|
||||
b.Cleanup(frankenphp.Shutdown)
|
||||
|
||||
cwd, _ := os.Getwd()
|
||||
testDataDir := cwd + "/testdata/"
|
||||
|
||||
// Mimics headers of a request sent by Firefox to GitHub
|
||||
headers := http.Header{}
|
||||
headers.Add(strings.Clone("Accept"), strings.Clone("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"))
|
||||
headers.Add(strings.Clone("Accept-Encoding"), strings.Clone("gzip, deflate, br"))
|
||||
headers.Add(strings.Clone("Accept-Language"), strings.Clone("fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"))
|
||||
headers.Add(strings.Clone("Cache-Control"), strings.Clone("no-cache"))
|
||||
headers.Add(strings.Clone("Connection"), strings.Clone("keep-alive"))
|
||||
headers.Add(strings.Clone("Cookie"), strings.Clone("user_session=myrandomuuid; __Host-user_session_same_site=myotherrandomuuid; dotcom_user=dunglas; logged_in=yes; _foo=barbarbarbarbarbar; _device_id=anotherrandomuuid; color_mode=foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar; preferred_color_mode=light; tz=Europe%2FParis; has_recent_activity=1"))
|
||||
headers.Add(strings.Clone("DNT"), strings.Clone("1"))
|
||||
headers.Add(strings.Clone("Host"), strings.Clone("example.com"))
|
||||
headers.Add(strings.Clone("Pragma"), strings.Clone("no-cache"))
|
||||
headers.Add(strings.Clone("Sec-Fetch-Dest"), strings.Clone("document"))
|
||||
headers.Add(strings.Clone("Sec-Fetch-Mode"), strings.Clone("navigate"))
|
||||
headers.Add(strings.Clone("Sec-Fetch-Site"), strings.Clone("cross-site"))
|
||||
headers.Add(strings.Clone("Sec-GPC"), strings.Clone("1"))
|
||||
headers.Add(strings.Clone("Upgrade-Insecure-Requests"), strings.Clone("1"))
|
||||
headers.Add(strings.Clone("User-Agent"), strings.Clone("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:122.0) Gecko/20100101 Firefox/122.0"))
|
||||
// Some uncommon headers
|
||||
headers.Add(strings.Clone("X-Super-Custom"), strings.Clone("Foo"))
|
||||
headers.Add(strings.Clone("Super-Super-Custom"), strings.Clone("Foo"))
|
||||
headers.Add(strings.Clone("Super-Super-Custom"), strings.Clone("Bar"))
|
||||
headers.Add(strings.Clone("Very-Custom"), strings.Clone("1"))
|
||||
|
||||
opt := frankenphp.WithRequestDocumentRoot(testDataDir, false)
|
||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := frankenphp.NewRequestWithContext(r, opt)
|
||||
require.NoError(b, err)
|
||||
|
||||
r.Header = headers
|
||||
|
||||
Reference in New Issue
Block a user