Skip to content

Commit

Permalink
Migrate benchmarks to the JSON State Test format
Browse files Browse the repository at this point in the history
  • Loading branch information
miles170 committed Oct 21, 2022
1 parent 13a7873 commit ab99342
Show file tree
Hide file tree
Showing 34 changed files with 37 additions and 213 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "evmc"]
path = evmc
url = https://github.com/ethereum/evmc
[submodule "test/evm-benchmarks"]
path = test/benchmarks
url = https://github.com/ipsilon/evm-benchmarks
2 changes: 1 addition & 1 deletion test/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

add_executable(evmone-bench)
target_include_directories(evmone-bench PRIVATE ${evmone_private_include_dir})
target_link_libraries(evmone-bench PRIVATE evmone testutils evmc::loader benchmark::benchmark)
target_link_libraries(evmone-bench PRIVATE evmone testutils evmone::statetestutils evmc::loader benchmark::benchmark)
target_sources(
evmone-bench PRIVATE
bench.cpp
Expand Down
69 changes: 19 additions & 50 deletions test/bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2019 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include "../statetest/statetest.hpp"
#include "helpers.hpp"
#include "synthetic_benchmarks.hpp"
#include <benchmark/benchmark.h>
Expand Down Expand Up @@ -46,51 +47,24 @@ struct BenchmarkCase
BenchmarkCase(std::string _name, bytes _code) noexcept
: name{std::move(_name)}, code{std::move(_code)}
{}
};


constexpr auto runtime_code_extension = ".bin-runtime";
constexpr auto inputs_extension = ".inputs";
/// Create a benchmark case with input.
BenchmarkCase(std::string _name, bytes _code, std::vector<Input> _inputs) noexcept
: name{std::move(_name)}, code{std::move(_code)}, inputs{std::move(_inputs)}
{}
};

/// Loads the benchmark case's inputs from the inputs file at the given path.
std::vector<BenchmarkCase::Input> load_inputs(const fs::path& path)
std::vector<BenchmarkCase::Input> load_inputs(const StateTransitionTest& state_test)
{
enum class state
{
name,
input,
expected_output
};

auto inputs_file = std::ifstream{path};

std::vector<BenchmarkCase::Input> inputs;
auto st = state::name;
std::string input_name;
bytes input;
for (std::string l; std::getline(inputs_file, l);)
for (size_t i = 0; i < state_test.multi_tx.inputs.size(); ++i)
{
switch (st)
{
case state::name:
if (l.empty())
continue; // Skip any empty line.
input_name = std::move(l);
st = state::input;
break;

case state::input:
input = from_hexx(l);
st = state::expected_output;
break;

case state::expected_output:
inputs.emplace_back(std::move(input_name), std::move(input), from_hexx(l));
input_name = {};
input = {};
st = state::name;
break;
}
const auto input_name = state_test.labels.at(i);
const auto input = state_test.multi_tx.inputs[i];
const bytes expected_output;

inputs.emplace_back(std::move(input_name), std::move(input), std::move(expected_output));
}

return inputs;
Expand All @@ -99,18 +73,13 @@ std::vector<BenchmarkCase::Input> load_inputs(const fs::path& path)
/// Loads a benchmark case from a file at `path` and all its inputs from the matching inputs file.
BenchmarkCase load_benchmark(const fs::path& path, const std::string& name_prefix)
{
const auto name = name_prefix + path.stem().string();

std::ifstream file{path};
std::string code_hexx{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
BenchmarkCase b{name, from_hexx(code_hexx)};
auto state_test = evmone::test::load_state_test(path);

auto inputs_path = path;
inputs_path.replace_extension(inputs_extension);
if (fs::exists(inputs_path))
b.inputs = load_inputs(inputs_path);
const auto name = name_prefix + path.stem().string();
const auto code = state_test.pre_state.get(state_test.multi_tx.to.value()).code;
const auto inputs = load_inputs(state_test);

return b;
return BenchmarkCase{name, code, inputs};
}

/// Loads all benchmark cases from the given directory and all its subdirectories.
Expand All @@ -124,7 +93,7 @@ std::vector<BenchmarkCase> load_benchmarks_from_dir( // NOLINT(misc-no-recursio
{
if (e.is_directory())
subdirs.emplace_back(e);
else if (e.path().extension() == runtime_code_extension)
else if (e.path().extension() == ".json")
code_files.emplace_back(e);
}

Expand Down
1 change: 1 addition & 0 deletions test/benchmarks
Submodule benchmarks added at 3a58fc
1 change: 0 additions & 1 deletion test/benchmarks/main/blake2b_huff.bin-runtime

This file was deleted.

20 changes: 0 additions & 20 deletions test/benchmarks/main/blake2b_huff.inputs

This file was deleted.

1 change: 0 additions & 1 deletion test/benchmarks/main/blake2b_shifts.bin-runtime

This file was deleted.

16 changes: 0 additions & 16 deletions test/benchmarks/main/blake2b_shifts.inputs

This file was deleted.

Loading

0 comments on commit ab99342

Please sign in to comment.