From 223a3255866b24e1bb15fa2c0339f4625626077e Mon Sep 17 00:00:00 2001 From: Hugo De la cruz Date: Thu, 29 May 2025 17:56:55 -0700 Subject: [PATCH] Add --time flag, print time spent applying block --- test/blockchaintest/blockchaintest.cpp | 23 ++++++---- test/blockchaintest/blockchaintest.hpp | 2 +- test/blockchaintest/blockchaintest_runner.cpp | 43 ++++++++++++++++--- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/test/blockchaintest/blockchaintest.cpp b/test/blockchaintest/blockchaintest.cpp index 9b61207737..582c3ea794 100644 --- a/test/blockchaintest/blockchaintest.cpp +++ b/test/blockchaintest/blockchaintest.cpp @@ -17,10 +17,11 @@ class BlockchainGTest : public testing::Test { fs::path m_json_test_file; evmc::VM& m_vm; + bool m_time; public: - explicit BlockchainGTest(fs::path json_test_file, evmc::VM& vm) noexcept - : m_json_test_file{std::move(json_test_file)}, m_vm{vm} + explicit BlockchainGTest(fs::path json_test_file, evmc::VM& vm, bool time) noexcept + : m_json_test_file{std::move(json_test_file)}, m_vm{vm}, m_time{time} {} void TestBody() final @@ -29,7 +30,8 @@ class BlockchainGTest : public testing::Test try { - evmone::test::run_blockchain_tests(evmone::test::load_blockchain_tests(f), m_vm); + evmone::test::run_blockchain_tests( + evmone::test::load_blockchain_tests(f), m_vm, m_time); } catch (const evmone::test::UnsupportedTestFeature& ex) { @@ -38,14 +40,14 @@ class BlockchainGTest : public testing::Test } }; -void register_test(const std::string& suite_name, const fs::path& file, evmc::VM& vm) +void register_test(const std::string& suite_name, const fs::path& file, evmc::VM& vm, bool time) { testing::RegisterTest(suite_name.c_str(), file.stem().string().c_str(), nullptr, nullptr, file.string().c_str(), 0, - [file, &vm]() -> testing::Test* { return new BlockchainGTest(file, vm); }); + [file, &vm, time]() -> testing::Test* { return new BlockchainGTest(file, vm, time); }); } -void register_test_files(const fs::path& root, evmc::VM& vm) +void register_test_files(const fs::path& root, evmc::VM& vm, bool time) { if (is_directory(root)) { @@ -57,11 +59,11 @@ void register_test_files(const fs::path& root, evmc::VM& vm) std::ranges::sort(test_files); for (const auto& p : test_files) - register_test(fs::relative(p, root).parent_path().string(), p, vm); + register_test(fs::relative(p, root).parent_path().string(), p, vm, time); } else // Treat as a file. { - register_test(root.parent_path().string(), root, vm); + register_test(root.parent_path().string(), root, vm, time); } } } // namespace @@ -85,6 +87,9 @@ int main(int argc, char* argv[]) bool trace_flag = false; app.add_flag("--trace", trace_flag, "Enable EVM tracing"); + bool time_flag = false; + app.add_flag("--time", time_flag, "Measure last block in test execution"); + CLI11_PARSE(app, argc, argv); evmc::VM vm{evmc_create_evmone()}; @@ -93,7 +98,7 @@ int main(int argc, char* argv[]) vm.set_option("trace", "1"); for (const auto& p : paths) - register_test_files(p, vm); + register_test_files(p, vm, time_flag); return RUN_ALL_TESTS(); } diff --git a/test/blockchaintest/blockchaintest.hpp b/test/blockchaintest/blockchaintest.hpp index 7a3b35cd69..ba853471de 100644 --- a/test/blockchaintest/blockchaintest.hpp +++ b/test/blockchaintest/blockchaintest.hpp @@ -73,6 +73,6 @@ struct BlockchainTest std::vector load_blockchain_tests(std::istream& input); -void run_blockchain_tests(std::span tests, evmc::VM& vm); +void run_blockchain_tests(std::span tests, evmc::VM& vm, bool time); } // namespace evmone::test diff --git a/test/blockchaintest/blockchaintest_runner.cpp b/test/blockchaintest/blockchaintest_runner.cpp index 0115d57302..ccd931cd27 100644 --- a/test/blockchaintest/blockchaintest_runner.cpp +++ b/test/blockchaintest/blockchaintest_runner.cpp @@ -10,6 +10,8 @@ #include "blockchaintest.hpp" #include +using namespace std::chrono; +using timer = high_resolution_clock; namespace evmone::test { @@ -221,7 +223,7 @@ std::string print_state(const TestState& s) } } // namespace -void run_blockchain_tests(std::span tests, evmc::VM& vm) +void run_blockchain_tests(std::span tests, evmc::VM& vm, bool time) { for (size_t case_index = 0; case_index != tests.size(); ++case_index) { @@ -253,6 +255,7 @@ void run_blockchain_tests(std::span tests, evmc::VM& vm) {&c.genesis_block_header, c.pre_state, c.genesis_block_header.difficulty}}}}; const auto* canonical_state = &c.pre_state; intx::uint256 max_total_difficulty = c.genesis_block_header.difficulty; + decltype(timer::now() - timer::now()) duration; for (size_t i = 0; i < c.test_blocks.size(); ++i) { @@ -276,10 +279,19 @@ void run_blockchain_tests(std::span tests, evmc::VM& vm) // Block being valid guarantees its parent was found. assert(parent_data_it != block_data.end()); const auto& pre_state = parent_data_it->second.post_state; - - auto res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, - rev, mining_reward(rev)); - + TransitionResult res; + if (time && i == c.test_blocks.size() - 1) + { + const auto start_timer = timer::now(); + res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev, + mining_reward(rev)); + duration = timer::now() - start_timer; + } + else + { + res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev, + mining_reward(rev)); + } ASSERT_TRUE(res.requests.has_value()); block_hashes[test_block.expected_block_header.block_number] = @@ -330,8 +342,19 @@ void run_blockchain_tests(std::span tests, evmc::VM& vm) assert(parent_data_it != block_data.end()); const auto& pre_state = parent_data_it->second.post_state; - const auto res = apply_block(pre_state, vm, bi, block_hashes, - test_block.transactions, rev, mining_reward(rev)); + TransitionResult res; + if (time && i == c.test_blocks.size() - 1) + { + const auto start_timer = timer::now(); + res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev, + mining_reward(rev)); + duration = timer::now() - start_timer; + } + else + { + res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev, + mining_reward(rev)); + } if (!res.requests.has_value()) continue; if (!res.rejected.empty()) @@ -362,6 +385,12 @@ void run_blockchain_tests(std::span tests, evmc::VM& vm) EXPECT_TRUE(false) << "Expected block to be invalid but resulted valid"; } } + if (time) + { + std::cout << " \t" << c.name + << " last block time: " << duration_cast(duration).count() + << " ns\n"; + } const auto expected_post_hash = std::holds_alternative(c.expectation.post_state) ? state::mpt_hash(std::get(c.expectation.post_state)) :