Skip to content

Commit

Permalink
Merge pull request #216 from ethereum/bench_cli
Browse files Browse the repository at this point in the history
Fix and clean up CLI handling in evmone-bench
  • Loading branch information
chfast authored Nov 26, 2019
2 parents 0be855b + 85416e5 commit 284f4bf
Showing 1 changed file with 62 additions and 24 deletions.
86 changes: 62 additions & 24 deletions test/bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Copyright 2019 The evmone Authors.
// Licensed under the Apache License, Version 2.0.

#include <benchmark/benchmark.h>
#include <evmc/evmc.hpp>
#include <evmc/loader.h>
#include <evmone/analysis.hpp>
#include <evmone/evmone.h>

#include <benchmark/benchmark.h>
#include <test/utils/utils.hpp>

#include <cctype>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -195,19 +195,49 @@ void load_benchmarks_from_dir(const fs::path& path, const std::string& name_pref
/// The number tries to be different from EVMC loading error codes.
constexpr auto cli_parsing_error = -3;


/// Parses evmone-bench CLI arguments and registers benchmark cases.
///
/// The following variants of number arguments are supported (including argv[0]):
///
/// 2: evmone-bench benchmarks_dir
/// Uses evmone VM, loads all benchmarks from benchmarks_dir.
/// 3: evmone-bench evmc_config benchmarks_dir
/// The same as (2) but loads custom EVMC VM.
/// 4: evmone-bench code_hex_file input_hex expected_output_hex.
/// Uses evmone VM, registers custom benchmark with the code from the given file,
/// and the given input. The benchmark will compare the output with the provided
/// expected one.
int parseargs(int argc, char** argv)
{
// Arguments' placeholders:
const char* evmc_config{};
const char* benchmarks_dir{};
const char* code_hex_file{};
const char* input_hex{};
const char* expected_output_hex{};

if (argc == 2)
{
vm = evmc::VM{evmc_create_evmone()};
std::cout << "Benchmarking evmone\n\n";
load_benchmarks_from_dir(argv[1]);
return 0;
benchmarks_dir = argv[1];
}
else if (argc == 3)
{
evmc_config = argv[1];
benchmarks_dir = argv[2];
}
else if (argc == 4)
{
code_hex_file = argv[1];
input_hex = argv[2];
expected_output_hex = argv[3];
}
else
return cli_parsing_error; // Incorrect number of arguments.

if (argc == 3)

if (evmc_config)
{
const auto evmc_config = argv[1];
auto ec = evmc_loader_error_code{};
vm = evmc::VM{evmc_load_and_configure(evmc_config, &ec)};

Expand All @@ -221,24 +251,32 @@ int parseargs(int argc, char** argv)
}

std::cout << "Benchmarking " << evmc_config << "\n\n";
load_benchmarks_from_dir(argv[2]);
return 0;
}
else
{
vm = evmc::VM{evmc_create_evmone()};
std::cout << "Benchmarking evmone\n\n";
}

if (argc != 4)
return cli_parsing_error;

std::ifstream file{argv[1]};
std::string code_hex{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
code_hex.erase(
std::remove_if(code_hex.begin(), code_hex.end(), [](auto x) { return std::isspace(x); }),
code_hex.end());

auto b = benchmark_case{};
b.code = std::make_shared<bytes>(from_hex(code_hex));
b.input = from_hex(argv[2]);
b.expected_output = from_hex(argv[3]);
RegisterBenchmark("external_evm_code", b)->Unit(kMicrosecond);
if (benchmarks_dir)
{
load_benchmarks_from_dir(benchmarks_dir);
}
else
{
std::ifstream file{code_hex_file};
std::string code_hex{
std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
code_hex.erase(std::remove_if(code_hex.begin(), code_hex.end(),
[](auto x) { return std::isspace(x); }),
code_hex.end());

auto b = benchmark_case{};
b.code = std::make_shared<bytes>(from_hex(code_hex));
b.input = from_hex(input_hex);
b.expected_output = from_hex(expected_output_hex);
RegisterBenchmark(code_hex_file, b)->Unit(kMicrosecond);
}
return 0;
}
} // namespace
Expand Down

0 comments on commit 284f4bf

Please sign in to comment.