-
Notifications
You must be signed in to change notification settings - Fork 1
/
v8_isolate_test.go
134 lines (118 loc) · 2.52 KB
/
v8_isolate_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
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
package isolates
import (
"bufio"
"bytes"
"context"
"os"
"runtime"
"testing"
"time"
)
func TestMain(m *testing.M) {
StartTracer(SimpleTracer)
Initialize()
os.Exit(m.Run())
}
func DumpTracerForBenchmark(b *testing.B) {
ctx := v.context.isolate.GetExecutionContext().ctx
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
DumpTracer(ctx, w, false)
w.Flush()
b.Logf("\n%s", string(buf.Bytes()))
}
func TestIsolateCreate(t *testing.T) {
ctx := v.context.isolate.GetExecutionContext().ctx
i := NewIsolate()
if c, err := i.NewContext(ctx); err != nil {
t.Error(err)
} else if value, err := c.Create(ctx, 20); err != nil {
t.Error(err)
} else if fn, err := c.Run(ctx, `
(() => {
const fib = (n) => {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
return fib;
})()
`, "index.js"); err != nil {
t.Error(err)
} else if result, err := fn.Call(ctx, nil, value); err != nil {
t.Error(err)
} else if n, err := result.Int64(ctx); err != nil {
t.Error(err)
} else if n != 6765 {
t.Errorf("invalid result: %v", result)
return
}
i.Terminate()
}
func BenchmarkIsolateCreate(b *testing.B) {
runtime.GC()
finished := make(chan bool)
for n := 0; n < b.N; n++ {
i := NewIsolate()
go func(i *Isolate) {
ctx := WithContext(context.Background())
done := false
go func() {
time.Sleep(1 * time.Second)
if !done {
DumpTracerForBenchmark(b)
b.Error("isolate is locked")
}
}()
if c, err := i.NewContext(ctx); err != nil {
b.Error(err)
} else if value, err := c.Create(ctx, 20); err != nil {
b.Error(err)
} else if fn, err := c.Run(ctx, `
(() => {
const fib = (n) => {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
return fib;
})()
`, "index.js"); err != nil {
b.Error(err)
} else if result, err := fn.Call(ctx, nil, value); err != nil {
b.Error(err)
} else if n, err := result.Int64(ctx); err != nil {
b.Error(err)
} else if n != 6765 {
b.Errorf("invalid result: %v", result)
return
}
done = true
finished <- true
}(i)
}
i := 0
for {
select {
case <-time.After(20 * time.Second):
DumpTracerForBenchmark(b)
b.Error("v8 locked")
case <-finished:
i++
if i == b.N {
goto FINISHED
}
}
}
FINISHED:
close(finished)
for _, isolate := range isolateRefs.Refs() {
isolate.(*Isolate).Terminate()
}
runtime.GC()
if isolateRefs.Length() != 0 {
b.Errorf("%d isolates remaining after garbage collection", isolateRefs.Length())
}
}