-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Several improvements to trie pruner * Move merkle value caching to trie nodes * Fix pruner caching * Add pruner benchmark * Fix db editor * Ignore empty child trees on polkadot * Fix valueandhash comparator
- Loading branch information
Showing
67 changed files
with
2,191 additions
and
1,560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# | ||
# Copyright Quadrivium LLC | ||
# All Rights Reserved | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
add_executable(trie_pruner_benchmark storage/trie_pruner_benchmark.cpp) | ||
target_link_libraries(trie_pruner_benchmark | ||
storage | ||
benchmark::benchmark | ||
GTest::gmock_main | ||
hasher | ||
log_configurator | ||
) | ||
target_include_directories(trie_pruner_benchmark PRIVATE "${CMAKE_SOURCE_DIR}/test") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <rocksdb/options.h> | ||
|
||
#include <boost/filesystem/operations.hpp> | ||
#include <cstdlib> | ||
#include <memory> | ||
#include <random> | ||
#include <soralog/level.hpp> | ||
#include <soralog/macro.hpp> | ||
|
||
#include "crypto/hasher/hasher_impl.hpp" | ||
#include "gmock/gmock.h" | ||
#include "log/logger.hpp" | ||
#include "primitives/common.hpp" | ||
#include "storage/in_memory/in_memory_spaced_storage.hpp" | ||
#include "storage/rocksdb/rocksdb.hpp" | ||
#include "storage/trie/impl/trie_storage_backend_impl.hpp" | ||
#include "storage/trie/polkadot_trie/polkadot_trie_factory.hpp" | ||
#include "storage/trie/polkadot_trie/polkadot_trie_factory_impl.hpp" | ||
#include "storage/trie/serialization/polkadot_codec.hpp" | ||
#include "storage/trie/serialization/trie_serializer_impl.hpp" | ||
#include "storage/trie/types.hpp" | ||
#include "storage/trie_pruner/impl/trie_pruner_impl.hpp" | ||
#include "testutil/prepare_loggers.hpp" | ||
|
||
#include "mock/core/application/app_configuration_mock.hpp" | ||
#include "mock/core/application/app_state_manager_mock.hpp" | ||
#include "utils/thread_pool.hpp" | ||
|
||
namespace storage = kagome::storage; | ||
namespace trie = storage::trie; | ||
|
||
struct TriePrunerBenchmark { | ||
TriePrunerBenchmark() { | ||
testutil::prepareLoggers(soralog::Level::DEBUG); | ||
app_state_manager = | ||
std::make_shared<kagome::application::AppStateManagerMock>(); | ||
EXPECT_CALL(*app_state_manager, atPrepare(testing::_)) | ||
.WillRepeatedly(testing::Return()); | ||
EXPECT_CALL(*app_state_manager, atLaunch(testing::_)) | ||
.WillRepeatedly(testing::Return()); | ||
EXPECT_CALL(*app_state_manager, atShutdown(testing::_)) | ||
.WillRepeatedly(testing::Return()); | ||
|
||
app_config = std::make_shared<kagome::application::AppConfigurationMock>(); | ||
EXPECT_CALL(*app_config, statePruningDepth()) | ||
.WillRepeatedly(testing::Return(100)); | ||
EXPECT_CALL(*app_config, enableThoroughPruning()) | ||
.WillRepeatedly(testing::Return(true)); | ||
|
||
hasher = std::make_shared<kagome::crypto::HasherImpl>(); | ||
codec = std::make_shared<trie::PolkadotCodec>(); | ||
rocksdb::Options options{}; | ||
options.create_if_missing = true; | ||
storage = | ||
storage::RocksDb::create( | ||
std::filesystem::path((boost::filesystem::temp_directory_path() | ||
/ "kagome_pruner_benchmark" | ||
/ boost::filesystem::unique_path()) | ||
.string()), | ||
options) | ||
.value(); | ||
storage_backend = std::make_shared<trie::TrieStorageBackendImpl>(storage); | ||
trie_factory = std::make_shared<trie::PolkadotTrieFactoryImpl>(); | ||
serializer = std::make_shared<trie::TrieSerializerImpl>( | ||
trie_factory, codec, storage_backend); | ||
thread_pool = std::make_shared<kagome::common::WorkerThreadPool>( | ||
kagome::TestThreadPool{}); | ||
} | ||
|
||
auto createPruner() { | ||
return std::make_unique<kagome::storage::trie_pruner::TriePrunerImpl>( | ||
app_state_manager, | ||
storage_backend, | ||
serializer, | ||
codec, | ||
storage, | ||
hasher, | ||
app_config, | ||
thread_pool); | ||
} | ||
|
||
std::shared_ptr<kagome::application::AppStateManagerMock> app_state_manager; | ||
std::shared_ptr<kagome::application::AppConfigurationMock> app_config; | ||
std::shared_ptr<kagome::crypto::HasherImpl> hasher; | ||
std::shared_ptr<trie::PolkadotCodec> codec; | ||
std::shared_ptr<storage::SpacedStorage> storage; | ||
std::shared_ptr<trie::TrieStorageBackendImpl> storage_backend; | ||
std::shared_ptr<trie::PolkadotTrieFactoryImpl> trie_factory; | ||
std::shared_ptr<trie::TrieSerializerImpl> serializer; | ||
std::shared_ptr<kagome::common::WorkerThreadPool> thread_pool; | ||
}; | ||
|
||
auto createRandomTrie(trie::PolkadotTrieFactory &factory, | ||
size_t values_num, | ||
size_t max_value_len) { | ||
std::mt19937_64 random; | ||
|
||
auto trie = factory.createEmpty(trie::PolkadotTrie::RetrieveFunctions{}); | ||
for (size_t i = 0; i < values_num; i++) { | ||
storage::Buffer key; | ||
key.resize(random() % 128); | ||
for (auto &byte : key) { | ||
byte = random() % 256; | ||
} | ||
storage::Buffer value; | ||
value.resize(random() % max_value_len); | ||
for (auto &byte : value) { | ||
byte = random() % 256; | ||
} | ||
trie->put(key, std::move(value)).value(); | ||
} | ||
return trie; | ||
} | ||
|
||
static void registerStateBenchmark(benchmark::State &state) { | ||
TriePrunerBenchmark benchmark; | ||
|
||
auto trie = createRandomTrie(*benchmark.trie_factory, 10000, 70); | ||
auto logger = kagome::log::createLogger("Benchmark"); | ||
|
||
for (auto _ : state) { | ||
auto pruner = benchmark.createPruner(); | ||
pruner->addNewState(*trie, kagome::storage::trie::StateVersion::V1).value(); | ||
} | ||
} | ||
|
||
static void pruneStateBenchmark(benchmark::State &state) { | ||
TriePrunerBenchmark benchmark; | ||
|
||
auto trie = createRandomTrie(*benchmark.trie_factory, 10000, 70); | ||
|
||
for (auto _ : state) { | ||
auto pruner = benchmark.createPruner(); | ||
pruner->addNewState(*trie, trie::StateVersion::V1).value(); | ||
auto [root, batch] = | ||
benchmark.serializer | ||
->storeTrie(*trie, kagome::storage::trie::StateVersion::V1) | ||
.value(); | ||
pruner | ||
->pruneFinalized( | ||
root, | ||
kagome::primitives::BlockInfo{kagome::primitives::BlockHash{}, 0}) | ||
.value(); | ||
batch->commit().value(); | ||
} | ||
} | ||
|
||
BENCHMARK(registerStateBenchmark) | ||
->Unit(benchmark::TimeUnit::kMillisecond) | ||
->Iterations(10); | ||
|
||
BENCHMARK(pruneStateBenchmark) | ||
->Unit(benchmark::TimeUnit::kMillisecond) | ||
->Iterations(10); | ||
|
||
BENCHMARK_MAIN(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.