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 4351c41 commit f965a83
Show file tree
Hide file tree
Showing 35 changed files with 65 additions and 211 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/benchmarks"]
path = test/benchmarks
url = https://github.com/ipsilon/evm-benchmarks
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

cmake_minimum_required(VERSION 3.16...3.23)

if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/evmc/.git)
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/evmc/.git OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmarks/.git)
message(FATAL_ERROR "Git submodules not initialized, execute:\n git submodule update --init")
endif()

Expand Down
1 change: 1 addition & 0 deletions test/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_sources(
bench.cpp
helpers.hpp
synthetic_benchmarks.cpp synthetic_benchmarks.hpp
${CMAKE_CURRENT_SOURCE_DIR}/../statetest/statetest_loader.cpp
)

# Tests
Expand Down
67 changes: 20 additions & 47 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 All @@ -12,6 +13,7 @@
#include <fstream>
#include <iostream>
#include <span>
#include <string>

namespace fs = std::filesystem;

Expand Down Expand Up @@ -48,49 +50,22 @@ struct BenchmarkCase
{}
};


constexpr auto runtime_code_extension = ".bin-runtime";
constexpr auto inputs_extension = ".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
std::vector<BenchmarkCase::Input> inputs;
const std::string input_name_prefix = ":label ";
for (size_t i = 0; i < state_test.multi_tx.inputs.size(); ++i)
{
name,
input,
expected_output
};
auto input_name = state_test.info.labels.at(i);
const auto prefix_pos = input_name.find_first_of(input_name_prefix);
if (prefix_pos != std::string::npos)
input_name = input_name.substr(prefix_pos + input_name_prefix.size());

auto inputs_file = std::ifstream{path};
const auto input = state_test.multi_tx.inputs[i];
bytes expected_output;

std::vector<BenchmarkCase::Input> inputs;
auto st = state::name;
std::string input_name;
bytes input;
for (std::string l; std::getline(inputs_file, l);)
{
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;
}
inputs.emplace_back(std::move(input_name), std::move(input), std::move(expected_output));
}

return inputs;
Expand All @@ -99,16 +74,14 @@ 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();
auto state_test = evmone::test::load_state_test(path);

std::ifstream file{path};
std::string code_hexx{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
BenchmarkCase b{name, from_hexx(code_hexx)};
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);

auto inputs_path = path;
inputs_path.replace_extension(inputs_extension);
if (fs::exists(inputs_path))
b.inputs = load_inputs(inputs_path);
BenchmarkCase b{name, code};
b.inputs = inputs;

return b;
}
Expand All @@ -124,7 +97,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 849b3e
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.

Loading

0 comments on commit f965a83

Please sign in to comment.