-
Notifications
You must be signed in to change notification settings - Fork 146
/
benchmark.go
122 lines (102 loc) · 2.55 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
package main
import (
"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/core/lru"
"github.com/PlatONnetwork/PlatON-Go/life/exec"
"bytes"
"errors"
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"os"
"runtime/pprof"
"time"
)
// The runner used in the unit test is mainly responsible for testing the Platonlib c++ library.
// According to the wasm file in the dir scan directory, the wasm is accessed from the main entry.
// The db is created according to --outdir. The test tool judges the test result based on the log information.
var (
wasmFileFlag = cli.StringFlag{
Name: "file",
Usage: "wasm file",
}
loopFlag = cli.IntFlag{
Name: "loop",
Usage: "execute count",
Value: 1,
}
profFlag = cli.StringFlag{
Name: "prof",
Usage: "write cpuprofie to file",
}
)
var benchmarkCommand = cli.Command{
Action: benchmarkCmd,
Name: "benchmark",
Usage: "benchmark wasm vm",
ArgsUsage: "<dir>",
Flags: []cli.Flag{
wasmFileFlag,
outDirFlag,
loopFlag,
profFlag,
},
HideHelp: false,
}
func benchmarkCmd(ctx *cli.Context) error {
wasmFile := ctx.String(wasmFileFlag.Name)
outDir := ctx.String(outDirFlag.Name)
loop := ctx.Int(loopFlag.Name)
profFile := ctx.String(profFlag.Name)
if profFile != "" {
f, err := os.Create(profFile)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
start := time.Now()
err := benchmark(wasmFile, outDir, loop)
if err != nil {
return err
}
end := time.Now()
fmt.Println("execute time:", end.Sub(start).String(), "loop:", loop)
return nil
}
func benchmark(wasmFile string, outDir string, loop int) error {
dbPath := outDir + testDBName
logStream := bytes.NewBuffer(make([]byte, 65535))
os.RemoveAll(dbPath)
db, err := leveldb.OpenFile(dbPath, nil)
if err != nil {
return errors.New(fmt.Sprintf("open leveldb %s failed :%v", dbPath, err))
}
code, err := ioutil.ReadFile(wasmFile)
if err != nil {
return err
}
m, functionCode, err := exec.ParseModuleAndFunc(code, nil)
if err != nil {
return err
}
if err := lru.SetWasmDB(outDir); err != nil {
return err
}
addr := common.HexToAddress("0x43355c787c50b647c425f594b441d4bd751951c1")
lru.WasmCache().Add(addr, &lru.WasmModule{m, functionCode})
for i := 0; i < loop; i++ {
m, ok := lru.WasmCache().Get(addr)
if !ok {
return errors.New("get wasm cache error")
}
if err := runModule(m.Module, m.FunctionCode, db, logStream); err != nil {
return err
}
//lru.WasmCache().Purge()
}
db.Close()
return nil
}