diff --git a/tests/benchmark/CMakeLists.txt b/tests/benchmark/CMakeLists.txt index 6ec3fa644..61a4cd62a 100644 --- a/tests/benchmark/CMakeLists.txt +++ b/tests/benchmark/CMakeLists.txt @@ -81,4 +81,4 @@ add_benchmark(data_structures_contains.cpp) target_link_libraries(${test_prefix}data_structures_contains mg-utils mg-storage-v3) add_benchmark(data_structures_remove.cpp) -target_link_libraries(${test_prefix}data_structures_contains mg-utils mg-storage-v3) +target_link_libraries(${test_prefix}data_structures_remove mg-utils mg-storage-v3) diff --git a/tests/benchmark/data_structures_remove.cpp b/tests/benchmark/data_structures_remove.cpp new file mode 100644 index 000000000..3fbd54a38 --- /dev/null +++ b/tests/benchmark/data_structures_remove.cpp @@ -0,0 +1,138 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "btree_map.hpp" +#include "data_structures_common.hpp" +#include "storage/v3/key_store.hpp" +#include "storage/v3/lexicographically_ordered_vertex.hpp" +#include "storage/v3/mvcc.hpp" +#include "storage/v3/property_value.hpp" +#include "storage/v3/transaction.hpp" +#include "storage/v3/vertex.hpp" +#include "utils/skip_list.hpp" + +namespace memgraph::benchmark { + +/////////////////////////////////////////////////////////////////////////////// +// Testing Remove Operation +/////////////////////////////////////////////////////////////////////////////// +static void BM_BenchmarkRemoveSkipList(::benchmark::State &state) { + utils::SkipList skip_list; + PrepareData(skip_list, state.range(0)); + coordinator::Hlc start_timestamp; + storage::v3::IsolationLevel isolation_level{storage::v3::IsolationLevel::SNAPSHOT_ISOLATION}; + storage::v3::Transaction transaction{start_timestamp, isolation_level}; + auto *delta = storage::v3::CreateDeleteObjectDelta(&transaction); + + // So we can also have elements that does don't exist + std::mt19937 i_generator(std::random_device{}()); + std::uniform_int_distribution i_distribution(0, state.range(0) * 2); + int64_t removed_elems{0}; + for (auto _ : state) { + for (auto i{0}; i < state.range(0); ++i) { + int64_t value = i_distribution(i_generator); + auto acc = skip_list.access(); + if (acc.remove(storage::v3::LexicographicallyOrderedVertex{ + storage::v3::Vertex{delta, storage::v3::PrimaryKey{storage::v3::PropertyValue(value)}}})) { + removed_elems++; + } + } + } + state.SetItemsProcessed(removed_elems); +} + +static void BM_BenchmarkRemoveStdMap(::benchmark::State &state) { + std::map std_map; + PrepareData(std_map, state.range(0)); + + // So we can also have elements that does don't exist + std::mt19937 i_generator(std::random_device{}()); + std::uniform_int_distribution i_distribution(0, state.range(0) * 2); + int64_t removed_elems{0}; + for (auto _ : state) { + for (auto i{0}; i < state.range(0); ++i) { + int64_t value = i_distribution(i_generator); + if (std_map.erase(storage::v3::PrimaryKey{storage::v3::PropertyValue{value}}) > 0) { + removed_elems++; + } + } + } + state.SetItemsProcessed(removed_elems); +} + +static void BM_BenchmarkRemoveStdSet(::benchmark::State &state) { + std::set std_set; + PrepareData(std_set, state.range(0)); + coordinator::Hlc start_timestamp; + storage::v3::IsolationLevel isolation_level{storage::v3::IsolationLevel::SNAPSHOT_ISOLATION}; + storage::v3::Transaction transaction{start_timestamp, isolation_level}; + auto *delta = storage::v3::CreateDeleteObjectDelta(&transaction); + + // So we can also have elements that does don't exist + std::mt19937 i_generator(std::random_device{}()); + std::uniform_int_distribution i_distribution(0, state.range(0) * 2); + int64_t removed_elems{0}; + for (auto _ : state) { + for (auto i{0}; i < state.range(0); ++i) { + int64_t value = i_distribution(i_generator); + if (std_set.erase(storage::v3::LexicographicallyOrderedVertex{ + storage::v3::Vertex{delta, {storage::v3::PropertyValue{value}}}}) > 0) { + removed_elems++; + } + } + } + state.SetItemsProcessed(removed_elems); +} + +static void BM_BenchmarkRemoveBppTree(::benchmark::State &state) { + tlx::btree_map bpp_tree; + PrepareData(bpp_tree, state.range(0)); + + // So we can also have elements that does don't exist + std::mt19937 i_generator(std::random_device{}()); + std::uniform_int_distribution i_distribution(0, state.range(0) * 2); + int64_t removed_elems{0}; + for (auto _ : state) { + for (auto i{0}; i < state.range(0); ++i) { + int64_t value = i_distribution(i_generator); + if (bpp_tree.erase(storage::v3::PrimaryKey{storage::v3::PropertyValue{value}}) > 0) { + removed_elems++; + } + } + } + state.SetItemsProcessed(removed_elems); +} + +BENCHMARK(BM_BenchmarkRemoveSkipList)->Arg(1000); + +BENCHMARK(BM_BenchmarkRemoveStdMap)->Arg(1000); + +BENCHMARK(BM_BenchmarkRemoveStdSet)->Arg(1000); + +BENCHMARK(BM_BenchmarkRemoveBppTree)->Arg(1000); + +} // namespace memgraph::benchmark + +BENCHMARK_MAIN();