Skip to content

Commit

Permalink
Add perf tests, fix warning.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg7mdp committed Mar 29, 2024
1 parent 8ea41a4 commit d2f1c92
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
5 changes: 2 additions & 3 deletions libraries/chain/include/eosio/chain/merkle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ inline digest_type calculate_merkle_pow2(const It& start, const It& end) {
}
}


template <class It>
requires std::is_same_v<std::decay_t<typename std::iterator_traits<It>::value_type>, digest_type>
inline digest_type calculate_merkle(const It& start, const It& end) {
assert(end >= start);
auto size = end - start;
auto size = static_cast<size_t>(end - start);
if (size <= 1)
return (size == 0) ? digest_type{} : *start;

auto midpoint = detail::bit_floor(static_cast<size_t>(size));
auto midpoint = detail::bit_floor(size);
if (size == midpoint)
return calculate_merkle_pow2(start, end);

Expand Down
53 changes: 53 additions & 0 deletions unittests/merkle_tree_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <eosio/chain/incremental_merkle_legacy.hpp>
#include <boost/test/unit_test.hpp>
#include <fc/crypto/sha256.hpp>
#include <chrono>

using namespace eosio::chain;
using eosio::chain::detail::make_legacy_digest_pair;
Expand Down Expand Up @@ -254,5 +255,57 @@ BOOST_AUTO_TEST_CASE(stack_check) {
}
}

class stopwatch {
public:
stopwatch(std::string msg) : _msg(std::move(msg)) { _start = clock::now(); }

~stopwatch() { std::cout << _msg << get_time_us()/1000000 << " s\n"; }

double get_time_us() const {
using duration_t = std::chrono::duration<double, std::micro>;
return std::chrono::duration_cast<duration_t>(clock::now() - _start).count();
}

using clock = std::chrono::high_resolution_clock;
using point = std::chrono::time_point<clock>;

std::string _msg;
point _start;
};

BOOST_AUTO_TEST_CASE(perf_test) {
auto perf_test = [](const std::string& type, auto&& incr_tree, auto&& calc_fn) {
using namespace std::string_literals;
constexpr size_t num_digests = 1024ull * 1024ull;

std::vector<digest_type> digests = create_test_digests(num_digests);
deque<digest_type> deq { digests.begin(), digests.end() };

auto incr_root = [&]() {
stopwatch s("time for "s + type + " incremental_merkle: ");
for (const auto& d : digests)
incr_tree.append(d);
return incr_tree.get_root();
}();

auto calc_root = [&]() {
stopwatch s("time for "s + type + " calculate_merkle: ");
return calc_fn(deq);
}();

return std::make_pair(incr_root, calc_root);
};

{
auto [incr_root, calc_root] = perf_test("new", incremental_merkle_tree(), calculate_merkle);
BOOST_CHECK_EQUAL(incr_root, calc_root);
}

{
auto [incr_root, calc_root] = perf_test("legacy", incremental_merkle_tree_legacy(), calculate_merkle_legacy);
BOOST_CHECK_EQUAL(incr_root, calc_root);
}
}


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit d2f1c92

Please sign in to comment.