Skip to content
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): add benchmark system #1624

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions gnovm/pkg/gnolang/benchdata/fib.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// param: 4 8 16

package main

func main() {
for i := 0; i < {{ .N }}; i++ {
fib({{ .Param }})
}
}

func fib(n int) int {
if n < 2 {
return 1
}
return fib(n-1) + fib(n-2)
}
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/benchdata/loop.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
for i := 0; i < {{ .N }}; i++ {}
}
90 changes: 90 additions & 0 deletions gnovm/pkg/gnolang/benchdata/matrix.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// param: 3 4 5 6

package main

func main() {
const p = {{ .Param }}
for i := 0; i < {{ .N }}; i++ {
_ = det(mul(dist(p), mul(id(p), dist(p))))
}
}

// identity matrix
func id(sz int) [][]int {
r := make([][]int, sz)
for i := range r {
r[i] = make([]int, sz)
r[i][i] = 1
}
return r
}

// distance from corner
func dist(sz int) [][]int {
half := sz / 2
r := make([][]int, sz)
for i := range r {
r[i] = make([]int, sz)
vdist := i
if vdist >= half {
vdist = sz - 1 - i
}
for j := range r[i] {
hdist := j
if hdist >= half {
hdist = sz - 1 - j
}
r[i][j] = vdist + hdist
}
}
return r
}

func det(m [][]int) int {
size := len(m)
if size == 2 {
return m[0][0]*m[1][1] - m[0][1]*m[1][0]
}
subMatrix := make([][]int, size-1)
for j := range subMatrix {
subMatrix[j] = make([]int, size-1)
}

determinant := 0
for i := 0; i < size; i++ {
for j := 1; j < size; j++ {
t := 0
for k := 0; k < size; k++ {
if k == i {
continue
}
subMatrix[j-1][t] = m[j][k]
t++
}
}
sign := 1
if i % 2 == 1 {
sign = -1
}
determinant += m[0][i] * det(subMatrix) * sign
}
return determinant
}

func mul(m1, m2 [][]int) [][]int {
size := len(m1)
result := make([][]int, size)
for i := range result {
result[i] = make([]int, size)
}

for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
for k := 0; k < size; k++ {
result[i][j] += m1[i][k] * m2[k][j]
}
}
}

return result
}
103 changes: 75 additions & 28 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package gnolang
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"text/template"
"unsafe"

// "github.com/davecgh/go-spew/spew"
"github.com/jaekwon/testify/assert"
"github.com/jaekwon/testify/require"
)

// run empty main().
Expand Down Expand Up @@ -192,43 +198,84 @@ func main() {
// Benchmarks

func BenchmarkPreprocess(b *testing.B) {
for i := 0; i < b.N; i++ {
// stop timer
b.StopTimer()
pkg := &PackageNode{
PkgName: "main",
PkgPath: ".main",
FileSet: nil,
}
pkg.InitStaticBlock(pkg, nil)
main := FuncD("main", nil, nil, Ss(
A("mx", ":=", "1000000"),
For(
A("i", ":=", "0"),
X("i < mx"),
Inc("i"),
),
))
b.StartTimer()
// timer started
main = Preprocess(nil, pkg, main).(*FuncDecl)
pkg := &PackageNode{
PkgName: "main",
PkgPath: ".main",
FileSet: nil,
}
}

func BenchmarkLoopyMain(b *testing.B) {
m := NewMachine("test", nil)
pkg.InitStaticBlock(pkg, nil)
main := FuncD("main", nil, nil, Ss(
A("mx", ":=", "10000000"),
A("mx", ":=", "1000000"),
For(
A("i", ":=", "0"),
// X("i < 10000000"),
X("i < mx"),
Inc("i"),
),
))
m.RunDeclaration(main)
copies := make([]*FuncDecl, b.N)
for i := 0; i < b.N; i++ {
copies[i] = main.Copy().(*FuncDecl)
}
b.ResetTimer()

for i := 0; i < b.N; i++ {
m.RunMain()
main = Preprocess(nil, pkg, copies[i]).(*FuncDecl)
}
}

type bdataParams struct {
N int
Param string
}

func BenchmarkBenchdata(b *testing.B) {
const bdDir = "./benchdata"
files, err := os.ReadDir(bdDir)
require.NoError(b, err)
for _, file := range files {
// Read file and parse template.
bcont, err := os.ReadFile(filepath.Join(bdDir, file.Name()))
cont := string(bcont)
require.NoError(b, err)
tpl, err := template.New("").Parse(cont)
require.NoError(b, err)

// Determine parameters.
const paramString = "// param: "
var params []string
pos := strings.Index(cont, paramString)
if pos >= 0 {
paramsRaw := strings.SplitN(cont[pos+len(paramString):], "\n", 2)[0]
params = strings.Fields(paramsRaw)
} else {
params = []string{""}
}

for _, param := range params {
name := file.Name()
if param != "" {
name += "_param:" + param
}
b.Run(name, func(b *testing.B) {
// Gen template with N and param.
var buf bytes.Buffer
require.NoError(b, tpl.Execute(&buf, bdataParams{
N: b.N,
Param: param,
}))

// Set up machine.
m := NewMachineWithOptions(MachineOptions{
PkgPath: "main",
Output: io.Discard,
})
n := MustParseFile("main.go", buf.String())
m.RunFiles(n)

b.ResetTimer()
m.RunMain()
})
}
}
}

Expand Down
Loading
Loading