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): gno test now has gas used information #2571

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/random"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
"github.com/gnolang/gno/tm2/pkg/testutils"
)

Expand Down Expand Up @@ -194,7 +195,7 @@
sort.Strings(pkg.FiletestGnoFiles)

startedAt := time.Now()
err = gnoTestPkg(pkg.Dir, pkg.TestGnoFiles, pkg.FiletestGnoFiles, cfg, io)
gasUsed, err := gnoTestPkg(pkg.Dir, pkg.TestGnoFiles, pkg.FiletestGnoFiles, cfg, io)
duration := time.Since(startedAt)
dstr := fmtDuration(duration)

Expand All @@ -205,7 +206,7 @@
io.ErrPrintfln("FAIL")
testErrCount++
} else {
io.ErrPrintfln("ok %s \t%s", pkg.Dir, dstr)
io.ErrPrintfln("ok %s \ttotal gas used: %d \t(%s)", pkg.Dir, gasUsed, dstr)
}
}
if testErrCount > 0 || buildErrCount > 0 {
Expand All @@ -222,7 +223,7 @@
filetestFiles []string,
cfg *testCfg,
io commands.IO,
) error {
) (int64, error) {
var (
verbose = cfg.verbose
rootDir = cfg.rootDir
Expand All @@ -234,7 +235,7 @@
stderr = io.Err()
errs error
)

var gasUsed int64
mode := tests.ImportModeStdlibsOnly
if cfg.withNativeFallback {
// XXX: display a warn?
Expand Down Expand Up @@ -271,7 +272,7 @@
})

if hasError {
return commands.ExitCodeError(1)
return gasUsed, commands.ExitCodeError(1)

Check warning on line 275 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L275

Added line #L275 was not covered by tests
}
testPkgName := getPkgNameFromFileset(ifiles)

Expand All @@ -287,6 +288,8 @@
}

m := tests.TestMachine(testStore, stdout, gnoPkgPath)
// initial new gasMeter
m.GasMeter = store.NewGasMeter(10_000_000_000)
if printRuntimeMetrics {
// from tm2/pkg/sdk/vm/keeper.go
// XXX: make maxAllocTx configurable.
Expand All @@ -295,7 +298,10 @@
m.Alloc = gno.NewAllocator(maxAllocTx)
}
m.RunMemPackage(memPkg, true)
beforeGasUsed := m.GasMeter.GasConsumed()
err := runTestFiles(m, tfiles, memPkg.Name, verbose, printRuntimeMetrics, runFlag, io)
afterGasUsed := m.GasMeter.GasConsumed()
gasUsed += (afterGasUsed - beforeGasUsed)
if err != nil {
errs = multierr.Append(errs, err)
}
Expand Down Expand Up @@ -357,7 +363,9 @@
}

testFilePath := filepath.Join(pkgPath, testFileName)
err := tests.RunFileTest(rootDir, testFilePath, tests.WithSyncWanted(cfg.updateGoldenTests))
var err error
gasUsedInThisPeriod, err := tests.RunFileTest(rootDir, testFilePath, tests.WithSyncWanted(cfg.updateGoldenTests))
gasUsed += gasUsedInThisPeriod
duration := time.Since(startedAt)
dstr := fmtDuration(duration)

Expand All @@ -375,13 +383,13 @@
}

if verbose {
io.ErrPrintfln("--- PASS: %s (%s)", testName, dstr)
io.ErrPrintfln("--- PASS: %s (%s) with gas used: %d", testName, dstr, gasUsedInThisPeriod)
}
// XXX: add per-test metrics
}
}

return errs
return gasUsed, errs
}

// attempts to determine the full gno pkg path by analyzing the directory.
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/gno_test/error_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gno test -v .
stdout 'Machine\.RunMain\(\) panic: oups'
stderr '=== RUN file/x_filetest.gno'
stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)'
stderr 'ok \. \d\.\d\ds'
stderr 'ok . total gas used: 0'

-- x_filetest.gno --
package main
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/testdata/gno_test/minim2.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
gno test .

! stdout .+
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 11'

-- minim.gno --
package minim
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/testdata/gno_test/minim3.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
gno test .

! stdout .+
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 10'

-- minim.gno --
package minim
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/testdata/gno_test/output_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gno test -v .
! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)'
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 28'

-- x_filetest.gno --
package main
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/testdata/gno_test/output_sync.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gno test -v . -update-golden-tests
! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)'
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 28'

cmp x_filetest.gno x_filetest.gno.golden

Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/testdata/gno_test/realm_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gno test -v .
! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)'
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 17'

-- x_filetest.gno --
// PKGPATH: gno.land/r/x
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/testdata/gno_test/realm_sync.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gno test -v . -update-golden-tests
! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)'
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 17'

cmp x_filetest.gno x_filetest.gno.golden

Expand Down
7 changes: 4 additions & 3 deletions gnovm/cmd/gno/testdata/gno_test/valid_filetest.txtar
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Test with a valid _filetest.gno file

# add the expectation of gasUsed information in stdout
gno test .

! stdout .+
stderr 'ok \. \d\.\d\ds'
stderr 'ok . total gas used: 19'

gno test -v .

! stdout .+
stderr '=== RUN file/valid_filetest.gno'
stderr '--- PASS: file/valid_filetest.gno \(\d\.\d\ds\)'
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 19'

-- valid.gno --
package valid
Expand Down
5 changes: 3 additions & 2 deletions gnovm/cmd/gno/testdata/gno_test/valid_test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
gno test .

! stdout .+
stderr 'ok \. \d\.\d\ds'
# expected gasUsed information
stderr 'ok . total gas used: 353'

gno test ./...

! stdout .+
stderr 'ok \. \d\.\d\ds'
stderr 'ok . total gas used: 353'

-- valid.gno --
package valid
Expand Down
7 changes: 4 additions & 3 deletions gnovm/stdlibs/testing/testing.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strconv"
"strings"
// gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
// "github.com/gnolang/gno/tm2/pkg/store"
)

// ----------------------------------------
Expand Down Expand Up @@ -326,7 +328,6 @@ func tRunner(t *T, fn testingFunc, verbose bool) {
if !t.shouldRun(t.name) {
return
}

start := unixNano()

defer func() {
Expand All @@ -341,15 +342,15 @@ func tRunner(t *T, fn testingFunc, verbose bool) {

dur := unixNano() - start
t.dur = formatDur(dur)

// gasUsed := m.GasMeter.GasConsumed()
if t.verbose {
switch {
case t.Failed():
fmt.Fprintf(os.Stderr, "--- FAIL: %s (%s)\n", t.name, t.dur)
case t.skipped:
fmt.Fprintf(os.Stderr, "--- SKIP: %s (%s)\n", t.name, t.dur)
case t.verbose:
fmt.Fprintf(os.Stderr, "--- PASS: %s (%s)\n", t.name, t.dur)
fmt.Fprintf(os.Stderr, "--- PASS: %s \n", t.name)
}
}
}()
Expand Down
17 changes: 13 additions & 4 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store/types"
"github.com/pmezard/go-difflib/difflib"
)

Expand Down Expand Up @@ -104,8 +105,9 @@

// RunFileTest executes the filetest at the given path, using rootDir as
// the directory where to find the "stdlibs" directory.
func RunFileTest(rootDir string, path string, opts ...RunFileTestOption) error {
func RunFileTest(rootDir string, path string, opts ...RunFileTestOption) (int64, error) {
var f runFileTestOptions
var gasUsed int64
for _, opt := range opts {
opt(&f)
}
Expand All @@ -125,8 +127,10 @@
store := TestStore(rootDir, "./files", stdin, stdout, stderr, mode)
store.SetLogStoreOps(true)
m := testMachineCustom(store, pkgPath, stdout, maxAlloc, send)
// Set a gas meter for machine that runs the tests.
m.GasMeter = types.NewGasMeter(10000 * 1000 * 1000)
thinhnx-var marked this conversation as resolved.
Show resolved Hide resolved
beforeGas := m.GasMeter.GasConsumed()
checkMachineIsEmpty := true

// TODO support stdlib groups, but make testing safe;
// e.g. not be able to make network connections.
// interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr})
Expand All @@ -135,7 +139,7 @@
// m.Use(unsafe.Symbols)
bz, err := os.ReadFile(path)
if err != nil {
return err
return gasUsed, err

Check warning on line 142 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L142

Added line #L142 was not covered by tests
}
{ // Validate result, errors, etc.
var pnc interface{}
Expand Down Expand Up @@ -284,6 +288,7 @@
// NOTE: ignores any gno.GetDebugErrors().
gno.ClearDebugErrors()
checkMachineIsEmpty = false // nothing more to do.
return gasUsed, nil
} else {
// record errors when errWanted is empty and pnc not nil
if pnc != nil {
Expand Down Expand Up @@ -312,6 +317,7 @@
}
// pnc is nil, errWanted empty, no gno debug errors
checkMachineIsEmpty = false
return gasUsed, nil

Check warning on line 320 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L320

Added line #L320 was not covered by tests
}
case "Output":
// panic if got unexpected error
Expand Down Expand Up @@ -404,9 +410,12 @@
checkMachineIsEmpty = false
default:
checkMachineIsEmpty = false
return gasUsed, nil

Check warning on line 413 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L413

Added line #L413 was not covered by tests
}
}
}
afterGas := m.GasMeter.GasConsumed()
gasUsed = afterGas - beforeGas

if checkMachineIsEmpty {
// Check that machine is empty.
Expand All @@ -418,7 +427,7 @@
panic(fmt.Sprintf("fail on %s: machine not empty after main: %v", path, err))
}
}
return nil
return gasUsed, nil
}

func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops, stacktrace string, maxAlloc int64, send std.Coins) {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func runFileTest(t *testing.T, path string, opts ...RunFileTestOption) {
logger = t.Log
}
rootDir := filepath.Join("..", "..")
err := RunFileTest(rootDir, path, append(opts, WithLoggerFunc(logger))...)
_, err := RunFileTest(rootDir, path, append(opts, WithLoggerFunc(logger))...)
if err != nil {
t.Fatalf("got error: %v", err)
}
Expand Down
Loading