-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: gnovm benchmarking tool #2241
Open
piux2
wants to merge
17
commits into
gnolang:master
Choose a base branch
from
piux2:feat_gnobench
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,194
−2
Open
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c781414
feat: gnovm benchmarking tool
piux2 f78ad0b
chore: fix typo
piux2 ae21be3
chore: fix bin flag
piux2 03ec596
chore: fix resetting benchmark stack
piux2 776f456
use array based timer to reduce the overhead, but won't support the l…
piux2 82211c2
chore: fix fmt
piux2 7f0be67
Delete benchmarking/results.csv
piux2 d5dce64
Delete benchmarking/results_stats.csv
piux2 a446401
removed stack since we don't need to benchmark static eval type
piux2 557084e
Merge remote-tracking branch 'origin' into feat_gnobench
piux2 3312bf5
remove store
piux2 87d8df2
Merge remote-tracking branch 'origin' into feat_gnobench
piux2 c3d0ebf
fix test
piux2 7b7260e
fixed naming
piux2 3e0b8ca
refactor bench measure
piux2 6c0f085
merged with upper master
piux2 46d7294
added cache wrap, removed recursive measurement, and commit tag
piux2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.PHONY: opcode storage build_opcode build_storage | ||
|
||
# Build target | ||
build_opcode: | ||
go build -tags "benchmarkingops" -o build/gnobench ./cmd | ||
|
||
# Build target | ||
build_storage: | ||
go build -tags "benchmarkingstorage" -o build/gnobench ./cmd | ||
|
||
|
||
# Run target | ||
opcode: build_opcode | ||
./build/gnobench | ||
|
||
# Run target | ||
storage: build_storage | ||
./build/gnobench |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# `gnobench` the time consumed for GnoVM OpCode execution and store access | ||
|
||
`gnobench` benchmarks the time consumed for each VM CPU OpCode and persistent access to the store, including marshalling and unmarshalling of realm objects. | ||
|
||
## Usage | ||
|
||
### Simple mode | ||
|
||
The benchmark only involves the GnoVM and the persistent store. It benchmarks the bare minimum components, and the results are isolated from other components. We use standardize gno contract to perform the benchmarking. | ||
|
||
This mode is the best for benchmarking each major release and/or changes in GnoVM. | ||
|
||
make opcode | ||
make storage | ||
|
||
### Production mode | ||
|
||
It benchmarks the node in the production environment with minimum overhead. | ||
We can not only benchmark with standardize the contract but also capture the live usage in production environment. | ||
It gives us a complete picture of the node perform. | ||
|
||
|
||
1. Build the production node with benchmarking flags: | ||
|
||
`go build -tags "benchmarkingstorage benchmarkingops" gno.land/cmd/gnoland` | ||
|
||
2. Run the node in the production environment. It will dump benchmark data to a benchmarks.bin file. | ||
|
||
3. call the realm contracts at `gno.land/r/x/benchmark/opcodes` and `gno.land/r/x/benchmark/storage` | ||
|
||
4. Stop the server after the benchmarking session is complete. | ||
|
||
5. Run the following command to convert the binary dump: | ||
|
||
`gnobench -bin path_to_benchmarks.bin` | ||
|
||
it converts the binary dump to results.csv and results_stats.csv. | ||
|
||
|
||
## Results | ||
|
||
The benchmarking results are stored in two files: | ||
1. The raw results are saved in results.csv. | ||
|
||
| Operation | Elapsed Time | Disk IO Bytes | | ||
|-----------------|--------------|---------------| | ||
| OpEval | 40333 | 0 | | ||
| OpPopBlock | 208 | 0 | | ||
| OpHalt | 167 | 0 | | ||
| OpEval | 500 | 0 | | ||
| OpInterfaceType | 458 | 0 | | ||
| OpPopBlock | 166 | 0 | | ||
| OpHalt | 125 | 0 | | ||
| OpInterfaceType | 21125 | 0 | | ||
| OpEval | 541 | 0 | | ||
| OpEval | 209 | 0 | | ||
| OpInterfaceType | 334 | 0 | | ||
|
||
|
||
|
||
2. The averages and standard deviations are summarized in results_stats.csv. | ||
|
||
| Operation | Avg Time | Avg Size | Time Std Dev | Count | | ||
|----------------|----------|----------|--------------|-------| | ||
| OpAdd | 101 | 0 | 45 | 300 | | ||
| OpAddAssign | 309 | 0 | 1620 | 100 | | ||
| OpArrayLit | 242 | 0 | 170 | 700 | | ||
| OpArrayType | 144 | 0 | 100 | 714 | | ||
| OpAssign | 136 | 0 | 95 | 2900 | | ||
| OpBand | 92 | 0 | 30 | 100 | | ||
| OpBandAssign | 127 | 0 | 62 | 100 | | ||
| OpBandn | 97 | 0 | 54 | 100 | | ||
| OpBandnAssign | 125 | 0 | 113 | 100 | | ||
| OpBinary1 | 128 | 0 | 767 | 502 | | ||
| OpBody | 127 | 0 | 145 | 13700 | | ||
|
||
## Design consideration | ||
|
||
### Minimum Overhead and Footprint | ||
|
||
- Constant build flags enable benchmarking. | ||
- Encode operations and measurements in binary. | ||
- Dump to a local file in binary. | ||
- No logging, printout, or network access involved. | ||
|
||
### Accurate | ||
|
||
- Pause the timer for storage access while performing VM opcode benchmarking. | ||
- Measure each OpCode execution in nanoseconds. | ||
- Store access includes the duration for Amino marshalling and unmarshalling. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
bm "github.com/gnolang/gno/benchmarking" | ||
) | ||
|
||
var ( | ||
outFlag = flag.String("out", "results.csv", "the out put file") | ||
benchFlag = flag.String("bench", "./gno", "the path to the benchmark contract") | ||
binFlag = flag.String("bin", "", "interpret the existing benchmarking file.") | ||
) | ||
|
||
// We dump the benchmark in bytes for speed and minimal overhead. | ||
const tmpFile = "benchmark.bin" | ||
|
||
func main() { | ||
flag.Parse() | ||
if *binFlag != "" { | ||
binFile, err := filepath.Abs(*binFlag) | ||
if err != nil { | ||
log.Fatal("unable to get absolute path for the file", err) | ||
} | ||
stats(binFile) | ||
return | ||
} | ||
bm.Init(tmpFile) | ||
bstore := benchmarkDiskStore() | ||
|
||
dir, err := filepath.Abs(*benchFlag) | ||
if err != nil { | ||
log.Fatal("unable to get absolute path for storage directory.", err) | ||
} | ||
|
||
// load stdlibs | ||
loadStdlibs(bstore) | ||
|
||
if bm.OpsEnabled { | ||
benchmarkOpCodes(bstore, dir) | ||
} | ||
if bm.StorageEnabled { | ||
benchmarkStorage(bstore, dir) | ||
} | ||
bm.Finish() | ||
stats(tmpFile) | ||
err = os.Remove(tmpFile) | ||
if err != nil { | ||
log.Printf("Error removing tmp file: %v", err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
gno "github.com/gnolang/gno/gnovm/pkg/gnolang" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadOpcodesPackage(t *testing.T) { | ||
dir := "../gno/opcodes" | ||
bstore := benchmarkDiskStore() | ||
pv := addPackage(bstore, dir, opcodesPkgPath) | ||
pb := pv.GetBlock(bstore) | ||
|
||
assert := assert.New(t) | ||
require := require.New(t) | ||
|
||
declTypes := []string{ | ||
"foo", | ||
"dog", | ||
"foofighter", | ||
} | ||
for i := 0; i < len(declTypes); i++ { | ||
tv := pb.Values[i] | ||
v, ok := tv.V.(gno.TypeValue) | ||
require.True(ok, "it should be a TypeValue") | ||
dtv, ok2 := v.Type.(*gno.DeclaredType) | ||
tn := declTypes[i] | ||
|
||
require.True(ok2, "it should be a DeclaredType") | ||
assert.Equal(tn, string(dtv.Name), "the declared type name should be "+tn) | ||
} | ||
|
||
// These are the functions used to benchmark the OpCode in the benchmarking contract. | ||
// We call each to benchmark a group of OpCodes. | ||
funcValues := []string{ | ||
"OpDecl", | ||
"OpEvalInt", | ||
"OpEvalFloat", | ||
"StmtOps", | ||
"ControlOps", | ||
"OpDefer", | ||
"OpUnary", | ||
"OpBinary", | ||
"ExprOps", | ||
"OpLor", | ||
"OpLand", | ||
"OpPanic", | ||
"OpTypeSwitch", | ||
"OpCallDeferNativeBody", | ||
"OpRange", | ||
"OpForLoop", | ||
"OpTypes", | ||
"OpOpValues", | ||
} | ||
|
||
for i := 3; i < 3+len(funcValues); i++ { | ||
j := i - 3 | ||
tv := pb.Values[i] | ||
fv, ok := tv.V.(*gno.FuncValue) | ||
require.True(ok, "it should be a FuncValue") | ||
fn := funcValues[j] | ||
assert.Equal(fn, string(fv.Name), "the declared type name should be "+fn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gnolang/gno/gnovm/pkg/gnoenv" | ||
gno "github.com/gnolang/gno/gnovm/pkg/gnolang" | ||
"github.com/gnolang/gno/gnovm/stdlibs" | ||
osm "github.com/gnolang/gno/tm2/pkg/os" | ||
) | ||
|
||
const ( | ||
opcodesPkgPath = "gno.land/r/x/benchmark/opcodes" | ||
rounds = 100 | ||
) | ||
|
||
func benchmarkOpCodes(bstore gno.Store, dir string) { | ||
opcodesPkgDir := filepath.Join(dir, "opcodes") | ||
|
||
pv := addPackage(bstore, opcodesPkgDir, opcodesPkgPath) | ||
for i := 0; i < rounds; i++ { | ||
callOpsBench(bstore, pv) | ||
} | ||
} | ||
|
||
func callOpsBench(bstore gno.Store, pv *gno.PackageValue) { | ||
// start | ||
pb := pv.GetBlock(bstore) | ||
for _, tv := range pb.Values { | ||
if fv, ok := tv.V.(*gno.FuncValue); ok { | ||
cx := gno.Call(fv.Name) | ||
callFunc(bstore, pv, cx) | ||
} | ||
} | ||
} | ||
|
||
const storagePkgPath = "gno.land/r/x/benchmark/storage" | ||
|
||
func benchmarkStorage(bstore gno.Store, dir string) { | ||
avlPkgDir := filepath.Join(dir, "avl") | ||
addPackage(bstore, avlPkgDir, "gno.land/p/demo/avl") | ||
|
||
storagePkgDir := filepath.Join(dir, "storage") | ||
pv := addPackage(bstore, storagePkgDir, storagePkgPath) | ||
benchStoreSet(bstore, pv) | ||
benchStoreGet(bstore, pv) | ||
} | ||
|
||
func benchStoreSet(bstore gno.Store, pv *gno.PackageValue) { | ||
title := "1KB content" | ||
content := strings.Repeat("a", 1024) | ||
|
||
// in forum.gno: func AddPost(title, content string) | ||
// one AddPost will be added to three different boards in the forum.gno contract | ||
|
||
for i := 0; i < rounds; i++ { | ||
cx := gno.Call("AddPost", gno.Str(title), gno.Str(content)) | ||
callFunc(bstore, pv, cx) | ||
} | ||
} | ||
|
||
func benchStoreGet(bstore gno.Store, pv *gno.PackageValue) { | ||
// in forum.gno: func GetPost(boardId, postId int) string in forum.gno | ||
// there are three different boards on the benchmarking forum contract | ||
for i := 0; i < 3; i++ { | ||
for j := 0; j < rounds; j++ { | ||
cx := gno.Call("GetPost", gno.X(i), gno.X(j)) | ||
callFunc(bstore, pv, cx) | ||
} | ||
} | ||
} | ||
|
||
func callFunc(bstore gno.Store, pv *gno.PackageValue, cx gno.Expr) []gno.TypedValue { | ||
m := gno.NewMachineWithOptions( | ||
gno.MachineOptions{ | ||
PkgPath: pv.PkgPath, | ||
Output: os.Stdout, // XXX | ||
Store: bstore, | ||
}) | ||
|
||
defer m.Release() | ||
|
||
m.SetActivePackage(pv) | ||
return m.Eval(cx) | ||
} | ||
|
||
// addPacakge | ||
|
||
func addPackage(bstore gno.Store, dir string, pkgPath string) *gno.PackageValue { | ||
// load benchmark contract | ||
m := gno.NewMachineWithOptions( | ||
gno.MachineOptions{ | ||
PkgPath: "", | ||
Output: os.Stdout, | ||
Store: bstore, | ||
}) | ||
defer m.Release() | ||
|
||
memPkg := gno.ReadMemPackage(dir, pkgPath) | ||
|
||
// pare the file, create pn, pv and save the values in m.store | ||
_, pv := m.RunMemPackage(memPkg, true) | ||
|
||
return pv | ||
} | ||
|
||
// load stdlibs | ||
func loadStdlibs(bstore gno.Store) { | ||
// copied from vm/builtin.go | ||
getPackage := func(pkgPath string, newStore gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) { | ||
stdlibDir := filepath.Join(gnoenv.RootDir(), "gnovm", "stdlibs") | ||
stdlibPath := filepath.Join(stdlibDir, pkgPath) | ||
if !osm.DirExists(stdlibPath) { | ||
// does not exist. | ||
return nil, nil | ||
} | ||
|
||
memPkg := gno.ReadMemPackage(stdlibPath, pkgPath) | ||
if memPkg.IsEmpty() { | ||
// no gno files are present, skip this package | ||
return nil, nil | ||
} | ||
|
||
m2 := gno.NewMachineWithOptions(gno.MachineOptions{ | ||
PkgPath: "gno.land/r/stdlibs/" + pkgPath, | ||
// PkgPath: pkgPath, | ||
Output: os.Stdout, | ||
Store: newStore, | ||
}) | ||
defer m2.Release() | ||
return m2.RunMemPackage(memPkg, true) | ||
} | ||
|
||
bstore.SetPackageGetter(getPackage) | ||
bstore.SetNativeStore(stdlibs.NativeStore) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be reorganised to:
gnovm/cmd/gnobench
+gnovm/pkg/gasbench
ORcontribs/gnobecnh
?I don't want to add another top-level directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gnovm/pkg/opsbench