Skip to content

Commit

Permalink
refactor the time measure
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Jan 15, 2024
1 parent 3b3c03a commit fd288ff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
30 changes: 11 additions & 19 deletions api/benchmarking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (m Module) Run(dataPtr int32, dataLen int32) int64 {

measuredDurations := []int64{}

benchmarking.StoreSnapshotDb()

// Always do at least one internal repeat.
repeats := int(benchmarkConfig.InternalRepeats)
if repeats < 1 {
Expand All @@ -73,6 +75,8 @@ func (m Module) Run(dataPtr int32, dataLen int32) int64 {
// allowing to rollback and reset the state after each iteration.
// as an alternative of providing before hook.

benchmarking.RestoreSnapshotDb()

// Does nothing, for now
benchmarking.WipeDb()

Expand Down Expand Up @@ -101,30 +105,18 @@ func (m Module) Run(dataPtr int32, dataLen int32) int64 {

benchmarking.StartDbTracker()

// Calculate the diff caused by the benchmark.
var start, end int64

_, _ = m.transactional.WithStorageLayer(
start = benchmarking.CurrentTime()
_, err := m.transactional.WithStorageLayer(
func() (primitives.PostDispatchInfo, error) {
start = benchmarking.CurrentTime()

postInfo, err := function.Dispatch(origin, args)
if err != nil {
m.logger.Critical(err.Error())
}

end = benchmarking.CurrentTime()

// Return an error to rollback to the initial state
// so each iteration starts from the same state.
if i == repeats {
// Do not revert the state on the last iteration, so
// the state is available for the validation.
return postInfo, nil
}
return postInfo, resetStateErr
return function.Dispatch(origin, args)
},
)
end = benchmarking.CurrentTime()
if err != nil {
m.logger.Critical(err.Error())
}

// Calculate the diff caused by the benchmark.
measuredDurations = append(measuredDurations, end-start)
Expand Down
Binary file modified build/runtime.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions env/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ func ExtBenchmarkingDbReadCountVersion1() int32 {
func ExtBenchmarkingDbWriteCountVersion1() int32 {
panic("not implemented")
}

func ExtBenchmarkingStoreSnapshotDbVersion1() {
panic("not implemented")
}

func ExtBenchmarkingRestoreSnapshotDbVersion1() {
panic("not implemented")
}
6 changes: 6 additions & 0 deletions env/utils_benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ func ExtBenchmarkingDbReadCountVersion1() int32

//go:wasmimport env ext_benchmarking_db_write_count_version_1
func ExtBenchmarkingDbWriteCountVersion1() int32

//go:wasmimport env ext_benchmarking_store_snapshot_db_version_1
func ExtBenchmarkingStoreSnapshotDbVersion1()

//go:wasmimport env ext_benchmarking_restore_snapshot_db_version_1
func ExtBenchmarkingRestoreSnapshotDbVersion1()
16 changes: 12 additions & 4 deletions primitives/benchmarking/benchmarking.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func StopDbTracker() {
env.ExtBenchmarkingStopDbTrackerVersion1()
}

func DbReadCount() int32 {
return env.ExtBenchmarkingDbReadCountVersion1()
}

func DbWriteCount() int32 {
return env.ExtBenchmarkingDbWriteCountVersion1()
}

func WipeDb() {
env.ExtBenchmarkingWipeDbVersion1()
}
Expand All @@ -146,10 +154,10 @@ func CommitDb() {
env.ExtBenchmarkingCommitDbVersion1()
}

func DbReadCount() int32 {
return env.ExtBenchmarkingDbReadCountVersion1()
func StoreSnapshotDb() {
env.ExtBenchmarkingStoreSnapshotDbVersion1()
}

func DbWriteCount() int32 {
return env.ExtBenchmarkingDbWriteCountVersion1()
func RestoreSnapshotDb() {
env.ExtBenchmarkingRestoreSnapshotDbVersion1()
}
2 changes: 0 additions & 2 deletions runtime/benchmark_timestamp_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ func benchmarkTimestampSet(b *testing.B) {

metadata := newBenchmarkingRuntimeMetadata(b, rt)

// TODO: switch to Gosemble types

// Setup the input params
now := uint64(time.Now().UnixMilli())
call, err := ctypes.NewCall(metadata, "Timestamp.set", ctypes.NewUCompactFromUInt(now))
Expand Down

0 comments on commit fd288ff

Please sign in to comment.