forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler_test.go
102 lines (89 loc) · 2 KB
/
profiler_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
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
package goja
import (
"sync/atomic"
"testing"
"time"
)
func TestProfiler(t *testing.T) {
err := StartProfile(nil)
if err != nil {
t.Fatal(err)
}
vm := New()
go func() {
_, err := vm.RunScript("test123.js", `
const a = 2 + 2;
function loop() {
for(;;) {}
}
loop();
`)
if err != nil {
if _, ok := err.(*InterruptedError); !ok {
panic(err)
}
}
}()
time.Sleep(200 * time.Millisecond)
atomic.StoreInt32(&globalProfiler.enabled, 0)
pr := globalProfiler.p.stop()
if len(pr.Sample) == 0 {
t.Fatal("No samples were recorded")
}
var running bool
for i := 0; i < 10; i++ {
time.Sleep(10 * time.Millisecond)
globalProfiler.p.mu.Lock()
running = globalProfiler.p.running
globalProfiler.p.mu.Unlock()
if !running {
break
}
}
if running {
t.Fatal("The profiler is still running")
}
vm.Interrupt(nil)
}
func TestProfiler1(t *testing.T) {
t.Skip("This test takes too long with race detector enabled and is non-deterministic. It's left here mostly for documentation purposes.")
err := StartProfile(nil)
if err != nil {
t.Fatal(err)
}
go func() {
sleep := func() {
time.Sleep(1 * time.Second)
}
// Spawn new vms faster than once every 10ms (the profiler interval) and make sure they don't finish too soon.
// This means (*profiler).run() won't be fast enough to collect the samples, so they must be collected
// after the profiler is stopped.
for i := 0; i < 500; i++ {
go func() {
vm := New()
vm.Set("sleep", sleep)
_, err := vm.RunScript("test123.js", `
function loop() {
for (let i = 0; i < 50000; i++) {
const a = Math.pow(Math.Pi, Math.Pi);
}
}
loop();
sleep();
`)
if err != nil {
if _, ok := err.(*InterruptedError); !ok {
panic(err)
}
}
}()
time.Sleep(1 * time.Millisecond)
}
}()
time.Sleep(500 * time.Millisecond)
atomic.StoreInt32(&globalProfiler.enabled, 0)
pr := globalProfiler.p.stop()
if len(pr.Sample) == 0 {
t.Fatal("No samples were recorded")
}
}