-
Notifications
You must be signed in to change notification settings - Fork 78
/
benchmarks_test.go
79 lines (65 loc) · 1.44 KB
/
benchmarks_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
package v8
import "testing"
func BenchmarkGetValue(b *testing.B) {
ctx := NewIsolate().NewContext()
_, err := ctx.Eval(`var hello = "test"`, "bench.js")
if err != nil {
b.Fatal(err)
}
glob := ctx.Global()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := glob.Get("hello"); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkGetNumberValue(b *testing.B) {
ctx := NewIsolate().NewContext()
val, err := ctx.Eval(`(157)`, "bench.js")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n += 2 {
if res := val.Int64(); res != 157 {
b.Fatal("Wrong value: ", res)
}
if res := val.Float64(); res != 157 {
b.Fatal("Wrong value: ", res)
}
}
}
func BenchmarkContextCreate(b *testing.B) {
ctx := NewIsolate().NewContext()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := ctx.Create(map[string]interface{}{}); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkEval(b *testing.B) {
iso := NewIsolate()
ctx := iso.NewContext()
script := `"hello"`
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := ctx.Eval(script, "bench-eval.js"); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkCallback(b *testing.B) {
ctx := NewIsolate().NewContext()
ctx.Global().Set("cb", ctx.Bind("cb", func(in CallbackArgs) (*Value, error) {
return nil, nil
}))
script := `cb()`
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := ctx.Eval(script, "bench-cb.js"); err != nil {
b.Fatal(err)
}
}
}