-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce merkle tree implementation #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
13f3d36
04f6899
e0f8466
a07da52
d6a218e
8d9fccb
464930d
573b68d
9f4801f
8fcebe1
cae439d
12faebd
9152495
414d0ee
e84efef
57ec932
3bb2b01
25d9fde
d0b7209
be8872e
a81f3a5
2ecaae8
70e7404
6ea5975
93f9f48
5d4431f
09a475a
0b48db3
3a2c38e
f011d21
0db15ee
3a402e4
de5ab3c
c3669e2
8573df7
5d749ab
a5b58e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| CompileFlags: | ||
| Add: [-D__cpp_concepts=202002L] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
|
|
||
| add_executable(set_get_benchmark merkle_tree/set_get.cpp) | ||
| target_link_libraries(set_get_benchmark benchmark::benchmark merkle_tree) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #include <cstddef> | ||
| #include <expected> | ||
| #include <filesystem> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <random> | ||
| #include <ranges> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
| #include <client/TracyScoped.hpp> | ||
| #include <tracy/Tracy.hpp> | ||
| #include <qtils/final_action.hpp> | ||
|
|
||
| #include <morum/archive_backend.hpp> | ||
| #include <morum/common.hpp> | ||
| #include <morum/db.hpp> | ||
| #include <morum/merkle_tree.hpp> | ||
|
|
||
| constexpr unsigned seed = 42; | ||
| static std::mt19937_64 rand_engine{seed}; | ||
|
|
||
| template <std::ranges::input_range R> | ||
| void fill_random(R &&span) { | ||
| static std::uniform_int_distribution<uint8_t> dist(0, 255); | ||
|
|
||
| for (auto &byte : span) { | ||
| byte = dist(rand_engine); | ||
| } | ||
| } | ||
|
|
||
| morum::Hash32 random_hash() { | ||
| morum::Hash32 hash; | ||
| fill_random(hash); | ||
| return hash; | ||
| } | ||
|
|
||
| qtils::ByteVec random_vector(size_t min_size = 1, size_t max_size = 128) { | ||
| std::uniform_int_distribution<size_t> dist(min_size, max_size); | ||
| size_t size = dist(rand_engine); | ||
|
|
||
| qtils::ByteVec v(size); | ||
| fill_random(v); | ||
| return v; | ||
| } | ||
|
|
||
| std::unique_ptr<morum::ArchiveTrieDb> trie_db; | ||
| morum::Hash32 last_root{}; | ||
|
|
||
| static void BM_SetGet(benchmark::State &state) { | ||
| rand_engine.seed(42); | ||
| constexpr int INSERTION_NUM = 1000; | ||
|
|
||
| for (auto _ : state) { | ||
| ZoneNamedN(loop_zone, "loop", true); | ||
|
|
||
| auto tree = trie_db->load_tree(last_root).value().value(); | ||
|
|
||
| std::vector<std::pair<morum::Hash32, qtils::ByteVec>> insertions; | ||
| for (int i = 0; i < INSERTION_NUM; i++) { | ||
| insertions.emplace_back(random_hash(), random_vector()); | ||
| } | ||
| { | ||
| ZoneNamedN(setter_zone, "set", true); | ||
| for (auto &[k, v] : insertions) { | ||
| tree->set(k, qtils::ByteVec{v}).value(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Возможно тут стоит вставить ассерт на наличие результата
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will throw an exception and kill the benchmark, which in this place works fine in my opinion. |
||
| } | ||
| } | ||
| { | ||
| ZoneNamedN(getter_zone, "get", true); | ||
|
|
||
| for (auto &[k, v] : insertions) { | ||
| tree->get(k).value(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Возможно тут стоит вставить ассерт на наличие результата
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will throw an exception and kill the benchmark, which in this place works fine in my opinion. |
||
| } | ||
| } | ||
| { | ||
| ZoneNamedN(calculate_hash_zone, "calculate_hash", true); | ||
| trie_db->get_root_and_store(*tree).value(); | ||
| } | ||
|
|
||
| FrameMark; | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(BM_SetGet); | ||
|
|
||
|
|
||
| int main(int argc, char **argv) { | ||
| char arg0_default[] = "benchmark"; | ||
| char *args_default = arg0_default; | ||
| if (argc == 0) { | ||
| argc = 1; | ||
| argv = &args_default; | ||
| } | ||
|
|
||
| qtils::FinalAction cleanup{[]() { | ||
| trie_db.reset(); | ||
| std::filesystem::remove_all("./test_db"); | ||
| }}; | ||
|
|
||
| auto db = std::shared_ptr{morum::open_db("./test_db").value()}; | ||
|
|
||
| trie_db = std::make_unique<morum::ArchiveTrieDb>(db); | ||
|
|
||
| auto tree = trie_db->empty_tree(); | ||
| constexpr int INSERTION_NUM = 5000; | ||
| { | ||
| ZoneNamedN(setter_zone, "initial insertions", true); | ||
|
|
||
| for (int i = 0; i < INSERTION_NUM; i++) { | ||
| tree->set(random_hash(), qtils::ByteVec{random_vector()}).value(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Возможно тут стоит вставить ассерт на наличие результата
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will throw an exception and kill the benchmark, which in this place works fine in my opinion. |
||
| } | ||
| } | ||
| last_root = trie_db->get_root_and_store(*tree).value(); | ||
| ::benchmark::Initialize(&argc, argv); | ||
| if (::benchmark::ReportUnrecognizedArguments(argc, argv)) { | ||
| return 1; | ||
| } | ||
| ::benchmark::RunSpecifiedBenchmarks(); | ||
| ::benchmark::Shutdown(); | ||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't off extensions, please. We are using "statement expressions" in some places.