forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_test.go
72 lines (59 loc) · 1.49 KB
/
load_test.go
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
package launcher_test
import (
"context"
"math/rand"
"sync"
"testing"
"github.com/assetnote/rod"
"github.com/assetnote/rod/lib/cdp"
"github.com/assetnote/rod/lib/launcher"
"github.com/assetnote/rod/lib/utils"
"github.com/ysmood/got"
)
func BenchmarkManager(b *testing.B) {
const concurrent = 30 // how many browsers will run at the same time
const num = 300 // how many browsers we will launch
limiter := make(chan int, concurrent)
s := got.New(b).Serve()
// docker run --rm -p 7317:7317 ghcr.io/go-rod/rod
s.HostURL.Host = "host.docker.internal"
s.Route("/", ".html", `<html><body>
ok
</body><script>
function wait() {
return new Promise(r => setTimeout(r, 1000 * Math.random()))
}
</script></html>`)
wg := &sync.WaitGroup{}
wg.Add(num)
for i := 0; i < num; i++ {
limiter <- 0
go func() {
utils.Sleep(rand.Float64())
ctx, cancel := context.WithCancel(context.Background())
defer func() {
go func() {
utils.Sleep(2)
cancel()
}()
}()
l := launcher.MustNewManaged("")
u, h := l.ClientHeader()
browser := rod.New().Client(cdp.MustStartWithURL(ctx, u, h)).MustConnect()
page := browser.MustPage()
wait := page.MustWaitNavigation()
page.MustNavigate(s.URL())
wait()
page.MustEval(`wait()`)
if rand.Int()%10 == 0 {
// 10% we will drop the websocket connection without call the api to gracefully close the browser
cancel()
} else {
browser.MustClose()
}
wg.Done()
<-limiter
}()
}
wg.Wait()
}