/
benchmark.go
153 lines (142 loc) · 3.19 KB
/
benchmark.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"io/ioutil"
"log"
"sync"
"time"
"github.com/dop251/otto"
"github.com/ry/v8worker"
)
func main() {
benchmarkOttoParallel()
benchmarkV8WorkerParallel()
benchmarkGolangParallel()
}
func octaneBenchmark() {
benchmarks := []string{
"octane-benchmark/base.js",
"octane-benchmark/box2d.js",
"octane-benchmark/code-load.js",
"octane-benchmark/crypto.js",
"octane-benchmark/deltablue.js",
"octane-benchmark/earley-boyer.js",
"octane-benchmark/gbemu-part1.js",
"octane-benchmark/gbemu-part2.js",
"octane-benchmark/mandreel.js",
"octane-benchmark/navier-stokes.js",
"octane-benchmark/pdfjs.js",
"octane-benchmark/raytrace.js",
// "octane-benchmark/regexp.js",
"octane-benchmark/richards.js",
"octane-benchmark/run.js",
"octane-benchmark/splay.js",
//"octane-benchmark/typescript-compiler.js",
//"octane-benchmark/typescript-input.js",
//"octane-benchmark/typescript.js",
// "octane-benchmark/zlib-data.js",
// "octane-benchmark/zlib.js",
}
benchmarkV8Worker(benchmarks)
benchmarkOtto(benchmarks)
}
func mustReadFile(source string) string {
buf, err := ioutil.ReadFile(source)
if err != nil {
log.Fatal("Failed to read file: %s", source)
return ""
}
return string(buf)
}
func benchmarkOtto(benchmarks []string) {
log.Println("benchmark otto\n")
vm := otto.New()
for _, source := range benchmarks {
script, err := vm.Compile(source, mustReadFile(source))
if err != nil {
log.Fatal("error code : %s %s", source, err)
}
vm.Run(script)
}
source := "benchmark_otto.js"
script, err := vm.Compile(source, mustReadFile(source))
if err != nil {
log.Fatal("error code : %s %s", source, err)
}
vm.Run(script)
}
func benchmarkV8Worker(benchmarks []string) {
log.Println("benchmark v8Worker\n")
worker := v8worker.New(func(msg string) {
log.Printf("message: %s \n", msg)
}, nil)
for _, source := range benchmarks {
worker.Load(source, mustReadFile(source))
}
source := "benchmark_v8.js"
worker.Load(source, mustReadFile(source))
}
func benchmarkOttoParallel() {
fmt.Println("otto parallel test")
var wg sync.WaitGroup
now := time.Now()
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
vm := otto.New()
source := `
var sum = 0;
for(var i = 0 ; i < 10000000 ; i++){
sum++;
}
`
vm.Run(source)
wg.Done()
}()
}
wg.Wait()
fmt.Println("otto parallel thread :", time.Now().Sub(now))
}
func benchmarkV8WorkerParallel() {
fmt.Println("v8 parallel test")
var wg sync.WaitGroup
now := time.Now()
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
worker := v8worker.New(func(msg string) {
log.Printf("message: %s \n", msg)
}, nil)
source := `
$recv(function(msg) {
var sum = 0;
for(var i = 0 ; i < 10000000 ; i++){
sum++;
}
});
`
worker.Load("test", source)
worker.Send("test")
wg.Done()
}()
}
wg.Wait()
fmt.Println("v8 parallel thread :", time.Now().Sub(now))
}
func benchmarkGolangParallel() {
fmt.Println("golang parallel test")
var wg sync.WaitGroup
now := time.Now()
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
sum := 0
for j := 0; j < 10000000; j++ {
sum++
}
wg.Done()
}()
}
wg.Wait()
fmt.Println("golang parallel thread :", time.Now().Sub(now))
}