-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware_bench_test.go
More file actions
181 lines (173 loc) · 4.79 KB
/
Copy pathmiddleware_bench_test.go
File metadata and controls
181 lines (173 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package caddy_chrome
import (
"fmt"
"github.com/caddyserver/caddy/v2/caddytest"
"io"
"net/http"
"os"
"strconv"
"testing"
"time"
)
// BenchmarkRender measures end-to-end rendering latency through the chrome
// middleware for a handful of representative pages. By default it benchmarks
// against a locally-exec'd Chrome; set CADDY_CHROME_TEST_BROWSER_URL to point
// at a remote browser (e.g. Lightpanda) to compare. The "Browser" label in the
// benchmark name reflects which backend was used.
//
// Run all:
//
// go test -bench=BenchmarkRender -benchtime=10x -run=^$ ./...
//
// Compare Chrome vs Lightpanda:
//
// go test -bench=BenchmarkRender -benchtime=20x -run=^$ -count=3 ./... | tee chrome.txt
// CADDY_CHROME_TEST_BROWSER_URL=http://127.0.0.1:9223/ \
// go test -bench=BenchmarkRender -benchtime=20x -run=^$ -count=3 ./... | tee lightpanda.txt
// benchstat chrome.txt lightpanda.txt
func BenchmarkRender(b *testing.B) {
caddytest.Default.LoadRequestTimeout = 30 * time.Second
tester := caddytest.NewTester(b)
tester.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
browserConfig := "exec"
browserLabel := "default_exec"
if p := os.Getenv("CADDY_CHROME_TEST_EXEC_PATH"); p != "" {
browserConfig = "exec " + strconv.Quote(p)
}
if u := os.Getenv("CADDY_CHROME_TEST_BROWSER_URL"); u != "" {
browserConfig = "url " + u
}
if l := os.Getenv("CADDY_CHROME_TEST_BROWSER_LABEL"); l != "" {
browserLabel = l
}
tester.InitServer(fmt.Sprintf(`
{
skip_install_trust
admin localhost:2999
http_port 9080
https_port 9443
log default {
output discard
}
}
http://localhost:9080 {
chrome {
%s
}
root ./testdata
file_server
}`, browserConfig), "caddyfile")
pages := []struct {
name string
path string
}{
{"static_html", "/html.html"},
{"javascript_module", "/javascript_module.html"},
{"shadow_dom", "/shadow_dom.html"},
{"fetch_get", "/fetch_get.html"},
{"pending_task", "/pending_task.html"},
}
// One warm-up per page so the first measured request doesn't pay
// connection / process startup costs.
for _, p := range pages {
req, _ := http.NewRequest("GET", "http://localhost:9080"+p.path, nil)
res, err := tester.Client.Do(req)
if err != nil {
b.Fatalf("warmup %s: %v", p.path, err)
}
_, _ = io.Copy(io.Discard, res.Body)
res.Body.Close()
}
for _, p := range pages {
b.Run(browserLabel+"/"+p.name, func(b *testing.B) {
url := "http://localhost:9080" + p.path
b.ResetTimer()
for i := 0; i < b.N; i++ {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
b.Fatal(err)
}
res, err := tester.Client.Do(req)
if err != nil {
b.Fatalf("%s: %v", p.path, err)
}
if res.StatusCode != 200 {
res.Body.Close()
b.Fatalf("%s: status %d", p.path, res.StatusCode)
}
_, err = io.Copy(io.Discard, res.Body)
res.Body.Close()
if err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkRenderParallel measures throughput with concurrent requests. This
// is the metric where Lightpanda's connection-per-request mode (auto-enabled)
// should pull ahead of the serialized single-target path: every goroutine
// gets its own CDP connection and therefore its own browser.
//
// go test -bench=BenchmarkRenderParallel -benchtime=20x -run=^$ -cpu=4 ./...
func BenchmarkRenderParallel(b *testing.B) {
caddytest.Default.LoadRequestTimeout = 30 * time.Second
tester := caddytest.NewTester(b)
tester.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
browserConfig := "exec"
browserLabel := "default_exec"
if p := os.Getenv("CADDY_CHROME_TEST_EXEC_PATH"); p != "" {
browserConfig = "exec " + strconv.Quote(p)
}
if u := os.Getenv("CADDY_CHROME_TEST_BROWSER_URL"); u != "" {
browserConfig = "url " + u
}
if l := os.Getenv("CADDY_CHROME_TEST_BROWSER_LABEL"); l != "" {
browserLabel = l
}
tester.InitServer(fmt.Sprintf(`
{
skip_install_trust
admin localhost:2999
http_port 9080
https_port 9443
log default {
output discard
}
}
http://localhost:9080 {
chrome {
%s
}
root ./testdata
file_server
}`, browserConfig), "caddyfile")
url := "http://localhost:9080/javascript_module.html"
// Warm up.
req, _ := http.NewRequest("GET", url, nil)
res, err := tester.Client.Do(req)
if err != nil {
b.Fatal(err)
}
_, _ = io.Copy(io.Discard, res.Body)
res.Body.Close()
b.Run(browserLabel+"/javascript_module", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
client := &http.Client{Timeout: 30 * time.Second}
for pb.Next() {
req, _ := http.NewRequest("GET", url, nil)
res, err := client.Do(req)
if err != nil {
b.Fatal(err)
}
_, _ = io.Copy(io.Discard, res.Body)
res.Body.Close()
}
})
})
}