From eec09fa004f5c962001685ea627daf89046da9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 25 Jul 2019 14:07:44 +0200 Subject: [PATCH] bench: Add option to load external EVMC VM evmone-bench --- test/bench/CMakeLists.txt | 2 +- test/bench/bench.cpp | 53 +++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/test/bench/CMakeLists.txt b/test/bench/CMakeLists.txt index bf739e152c..8352a1d43e 100644 --- a/test/bench/CMakeLists.txt +++ b/test/bench/CMakeLists.txt @@ -12,7 +12,7 @@ find_package(benchmark CONFIG REQUIRED) add_executable(evmone-bench bench.cpp) target_include_directories(evmone-bench PRIVATE ${evmone_private_include_dir}) -target_link_libraries(evmone-bench PRIVATE evmone testutils benchmark::benchmark) +target_link_libraries(evmone-bench PRIVATE evmone testutils evmc::loader benchmark::benchmark) check_include_file_cxx(filesystem HAVE_FILESYSTEM) if(NOT HAVE_FILESYSTEM) diff --git a/test/bench/bench.cpp b/test/bench/bench.cpp index 8a9548d153..63d7e299a5 100644 --- a/test/bench/bench.cpp +++ b/test/bench/bench.cpp @@ -3,6 +3,7 @@ // Licensed under the Apache License, Version 2.0. #include +#include #include #include @@ -32,7 +33,7 @@ using namespace benchmark; namespace { constexpr auto gas_limit = std::numeric_limits::max(); -auto vm = evmc::vm{evmc_create_evmone()}; +auto vm = evmc::vm{}; constexpr auto inputs_extension = ".inputs"; @@ -174,7 +175,7 @@ void load_benchmark(fs::path path) } } -bool load_benchmarks_from_dir(const char* path) +void load_benchmarks_from_dir(const char* path) { for (auto& e : fs::directory_iterator{path}) { @@ -183,16 +184,44 @@ bool load_benchmarks_from_dir(const char* path) load_benchmark(e.path()); } - return true; } -bool parseargs(int argc, char** argv) +/// The error code for CLI arguments parsing error in evmone-bench. +/// The number tries to be different from EVMC loading error codes. +constexpr auto cli_parsing_error = -3; + +int parseargs(int argc, char** argv) { if (argc == 2) - return load_benchmarks_from_dir(argv[1]); + { + vm = evmc::vm{evmc_create_evmone()}; + std::cout << "Benchmarking evmone\n\n"; + load_benchmarks_from_dir(argv[1]); + return 0; + } + + if (argc == 3) + { + const auto evmc_config = argv[1]; + auto ec = evmc_loader_error_code{}; + vm = evmc::vm{evmc_load_and_configure(evmc_config, &ec)}; + + if (ec != EVMC_LOADER_SUCCESS) + { + if (const auto error = evmc_last_error_msg()) + std::cerr << "EVMC loading error: " << error << "\n"; + else + std::cerr << "EVMC loading error " << ec << "\n"; + return static_cast(ec); + } + + std::cout << "Benchmarking " << evmc_config << "\n\n"; + load_benchmarks_from_dir(argv[2]); + return 0; + } if (argc != 4) - return false; + return cli_parsing_error; std::ifstream file{argv[1]}; std::string code_hex{std::istreambuf_iterator{file}, std::istreambuf_iterator{}}; @@ -205,8 +234,7 @@ bool parseargs(int argc, char** argv) b.input = from_hex(argv[2]); b.expected_output = from_hex(argv[3]); RegisterBenchmark("external_evm_code", b)->Unit(kMicrosecond); - - return true; + return 0; } } // namespace @@ -216,8 +244,13 @@ int main(int argc, char** argv) { Initialize(&argc, argv); - if (!parseargs(argc, argv) && ReportUnrecognizedArguments(argc, argv)) - return 1; + const auto ec = parseargs(argc, argv); + + if (ec == cli_parsing_error && ReportUnrecognizedArguments(argc, argv)) + return ec; + + if (ec != 0) + return ec; RunSpecifiedBenchmarks(); return 0;