forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisolate_test.go
95 lines (80 loc) · 1.93 KB
/
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
package v8go_test
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"testing"
"time"
"rogchap.com/v8go"
)
func TestIsolateTermination(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)
// ctx2, _ := v8go.NewContext(iso)
err := make(chan error, 1)
go func() {
_, e := ctx.RunScript(`while (true) { }`, "forever.js")
err <- e
}()
go func() {
// [RC] find a better way to know when a script has started execution
time.Sleep(time.Millisecond)
iso.TerminateExecution()
}()
if e := <-err; e == nil || !strings.HasPrefix(e.Error(), "ExecutionTerminated") {
t.Errorf("unexpected error: %v", e)
}
}
func TestGetHeapStatistics(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
v8go.NewContext(iso)
v8go.NewContext(iso)
hs := iso.GetHeapStatistics()
if hs.NumberOfNativeContexts != 2 {
t.Error("expect NumberOfNativeContexts return 2, got", hs.NumberOfNativeContexts)
}
if hs.NumberOfDetachedContexts != 0 {
t.Error("expect NumberOfDetachedContexts return 0, got", hs.NumberOfDetachedContexts)
}
}
func BenchmarkIsolateInitialization(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
vm, _ := v8go.NewIsolate()
vm.Close() // force disposal of the VM
}
}
func BenchmarkIsolateInitAndRun(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
vm, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(vm)
ctx.RunScript(script, "main.js")
str, _ := json.Marshal(makeObject())
cmd := fmt.Sprintf("process(%s)", str)
ctx.RunScript(cmd, "cmd.js")
ctx.Close()
vm.Close() // force disposal of the VM
}
}
const script = `
const process = (record) => {
const res = [];
for (let [k, v] of Object.entries(record)) {
res.push({
name: k,
value: v,
});
}
return JSON.stringify(res);
};
`
func makeObject() interface{} {
return map[string]interface{}{
"a": rand.Intn(1000000),
"b": "AAAABBBBAAAABBBBAAAABBBBAAAABBBBAAAABBBB",
}
}