Skip to content

Commit

Permalink
bench: Run execution benchmarks with MockedHost
Browse files Browse the repository at this point in the history
This allows using state-access instructions in benchmarks. Previously
such benchmarks would crash.
  • Loading branch information
chfast committed May 13, 2021
1 parent 4a7161b commit a7f404f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions test/bench/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "test/utils/utils.hpp"
#include <benchmark/benchmark.h>
#include <evmc/evmc.hpp>
#include <evmc/mocked_host.hpp>
#include <evmone/analysis.hpp>
#include <evmone/baseline.hpp>

Expand Down Expand Up @@ -46,23 +47,26 @@ inline void baseline_analyze(benchmark::State& state, bytes_view code) noexcept
state.counters["rate"] = Counter(static_cast<double>(bytes_analysed), Counter::kIsRate);
}

inline evmc::result execute(
evmc::VM& vm, evmc_revision rev, int64_t gas_limit, bytes_view code, bytes_view input) noexcept
inline evmc::result execute(evmc::VM& vm, evmc::Host& host, evmc_revision rev, int64_t gas_limit,
bytes_view code, bytes_view input) noexcept
{
auto msg = evmc_message{};
msg.gas = gas_limit;
msg.input_data = input.data();
msg.input_size = input.size();
return vm.execute(rev, msg, code.data(), code.size());
return vm.execute(host, rev, msg, code.data(), code.size());
}

inline void execute(benchmark::State& state, evmc::VM& vm, bytes_view code, bytes_view input = {},
bytes_view expected_output = {}) noexcept
{
constexpr auto rev = default_revision;
constexpr auto gas_limit = default_gas_limit;

evmc::MockedHost host;

{ // Test run.
const auto r = execute(vm, rev, gas_limit, code, input);
const auto r = execute(vm, host, rev, gas_limit, code, input);
if (r.status_code != EVMC_SUCCESS)
{
state.SkipWithError(("failure: " + std::to_string(r.status_code)).c_str());
Expand All @@ -85,7 +89,7 @@ inline void execute(benchmark::State& state, evmc::VM& vm, bytes_view code, byte
auto iteration_gas_used = int64_t{0};
for (auto _ : state)
{
auto r = execute(vm, rev, gas_limit, code, input);
auto r = execute(vm, host, rev, gas_limit, code, input);
iteration_gas_used = gas_limit - r.gas_left;
total_gas_used += iteration_gas_used;
}
Expand Down

0 comments on commit a7f404f

Please sign in to comment.