diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index b7d862c21..733391dbe 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -28,7 +28,7 @@ endif() add_library(dfly_transaction db_slice.cc blocking_controller.cc command_registry.cc cluster/cluster_utility.cc - journal/cmd_serializer.cc journal/tx_executor.cc namespaces.cc + journal/tx_executor.cc namespaces.cc common.cc journal/journal.cc journal/types.cc journal/journal_slice.cc server_state.cc table.cc top_keys.cc transaction.cc tx_base.cc serializer_commons.cc journal/serializer.cc journal/executor.cc journal/streamer.cc @@ -43,7 +43,7 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") cxx_test(tiered_storage_test dfly_test_lib LABELS DFLY) endif() -add_library(dragonfly_lib bloom_family.cc +add_library(dragonfly_lib bloom_family.cc journal/cmd_serializer.cc config_registry.cc conn_context.cc debugcmd.cc dflycmd.cc engine_shard.cc engine_shard_set.cc family_utils.cc generic_family.cc hset_family.cc http_api.cc json_family.cc @@ -115,6 +115,7 @@ cxx_test(zset_family_test dfly_test_lib LABELS DFLY) cxx_test(blocking_controller_test dfly_test_lib LABELS DFLY) cxx_test(json_family_test dfly_test_lib LABELS DFLY) cxx_test(journal/journal_test dfly_test_lib LABELS DFLY) +cxx_test(journal/cmd_serializer_test dfly_test_lib LABELS DFLY) cxx_test(top_keys_test dfly_test_lib LABELS DFLY) cxx_test(hll_family_test dfly_test_lib LABELS DFLY) cxx_test(bloom_family_test dfly_test_lib LABELS DFLY) diff --git a/src/server/journal/cmd_serializer_test.cc b/src/server/journal/cmd_serializer_test.cc new file mode 100644 index 000000000..5b8af50e8 --- /dev/null +++ b/src/server/journal/cmd_serializer_test.cc @@ -0,0 +1,82 @@ +// Copyright 2024, DragonflyDB authors. All rights reserved. +// See LICENSE for licensing terms. +// + +#include "server/journal/cmd_serializer.h" + +#include "base/gtest.h" +#include "base/logging.h" +#include "core/string_set.h" +#include "server/test_utils.h" + +extern "C" { +#include "redis/zmalloc.h" +} + +using namespace testing; +using namespace std; +using benchmark::DoNotOptimize; + +namespace dfly { + +static void SetUpTestSuite() { + InitRedisTables(); // to initialize server struct. + + auto* tlh = mi_heap_get_backing(); + init_zmalloc_threadlocal(tlh); + SmallString::InitThreadLocal(tlh); +} + +static void TearDownTestSuite() { + mi_heap_collect(mi_heap_get_backing(), true); +} + +// 2 args: threshold and value size +void BM_SerializerThresholdSet(benchmark::State& state) { + ServerState::Init(0, 1, nullptr); + SetUpTestSuite(); + MiMemoryResource mi(mi_heap_get_backing()); + CompactObj::InitThreadLocal(&mi); + + auto threshold = state.range(0); + auto val_size = state.range(1); + string_view k = "key"; + CmdSerializer serializer([](std::string s) { DoNotOptimize(s); }, threshold); + absl::InsecureBitGen eng; + + { + // Allocations must be done on the heap using mimalloc, as we use mimalloc API in many places + auto* key = CompactObj::AllocateMR(k); + auto* value = CompactObj::AllocateMR(); + + StringSet* s = CompactObj::AllocateMR(); + for (unsigned int i = 0; i < val_size; ++i) { + s->Add(GetRandomHex(eng, 100)); + } + value->InitRobj(OBJ_SET, kEncodingStrMap2, s); + + while (state.KeepRunning()) { + serializer.SerializeEntry(k, *key, *value, 0); + } + + CompactObj::DeleteMR(value); + CompactObj::DeleteMR(key); + } + + TearDownTestSuite(); + ServerState::Destroy(); +} + +BENCHMARK(BM_SerializerThresholdSet) + ->Args({0, 1}) + ->Args({100, 1}) + ->Args({0, 10}) + ->Args({1000, 10}) + ->Args({0, 100}) + ->Args({10'000, 100}) + ->Args({0, 1'000}) + ->Args({100'000, 1'000}) + ->Args({0, 1'000'000}) + ->Args({100'000, 1'000'000}); + +} // namespace dfly