|
| 1 | +#include <library/cpp/malloc/api/malloc.h> |
| 2 | +#include <library/cpp/monlib/dynamic_counters/counters.h> |
1 | 3 | #include <library/cpp/testing/unittest/registar.h>
|
2 | 4 | #include <ydb/library/actors/core/events.h>
|
3 | 5 | #include <ydb/library/actors/core/event_local.h>
|
4 | 6 | #include <ydb/library/actors/interconnect/interconnect_common.h>
|
5 |
| -#include <library/cpp/monlib/dynamic_counters/counters.h> |
6 | 7 | #include <ydb/library/actors/interconnect/event_holder_pool.h>
|
7 | 8 |
|
| 9 | +#include <contrib/libs/tcmalloc/tcmalloc/malloc_extension.h> |
| 10 | + |
8 | 11 | #include <atomic>
|
9 | 12 |
|
10 | 13 | using namespace NActors;
|
@@ -56,4 +59,64 @@ Y_UNIT_TEST_SUITE(EventHolderPool) {
|
56 | 59 | freeQ.clear(); // if we don't this, we may probablty crash due to the order of object destruction
|
57 | 60 | }
|
58 | 61 |
|
| 62 | + struct TMemProfiler { |
| 63 | + size_t UsedAtStart = 0; |
| 64 | + |
| 65 | + |
| 66 | + TMemProfiler() |
| 67 | + : UsedAtStart(0) |
| 68 | + { |
| 69 | + UsedAtStart = GetUsed(); |
| 70 | + |
| 71 | + const auto &info = NMalloc::MallocInfo(); |
| 72 | + bool tcmallocIsUsed = TStringBuf(info.Name).StartsWith("tc"); |
| 73 | + UNIT_ASSERT(tcmallocIsUsed); |
| 74 | + } |
| 75 | + |
| 76 | + size_t GetUsed() { |
| 77 | + auto properties = tcmalloc::MallocExtension::GetProperties(); |
| 78 | + auto x = properties["generic.bytes_in_use_by_app"]; |
| 79 | + return x.value - UsedAtStart; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + void MemComsumption(size_t repeats, size_t buffSize) { |
| 84 | + TDeque<THolder<IEventBase>> freeQ; |
| 85 | + auto callback = [&](THolder<IEventBase> event) { |
| 86 | + freeQ.push_back(std::move(event)); |
| 87 | + }; |
| 88 | + auto pool = Setup(std::move(callback)); |
| 89 | + |
| 90 | + std::list<TEventHolder> q; |
| 91 | + TMemProfiler prof; |
| 92 | + |
| 93 | + for (ui32 i = 0; i < repeats; i++) { |
| 94 | + TEventHolder& event = pool.Allocate(q); |
| 95 | + TString data = TString::Uninitialized(buffSize); |
| 96 | + auto holder = MakeHolder<IEventHandle>(TActorId{}, TActorId{}, new TEvents::TEvBlob(data)); |
| 97 | + event.Fill(*holder); |
| 98 | + |
| 99 | + pool.Release(q, q.begin()); |
| 100 | + UNIT_ASSERT_LT_C(prof.GetUsed(), TEventHolderPool::MaxBytesPerMessage * 2, prof.GetUsed()); |
| 101 | + } |
| 102 | + |
| 103 | + for (ui32 i = 0; i < repeats; i++) { |
| 104 | + TEventHolder& event = pool.Allocate(q); |
| 105 | + TString data = TString::Uninitialized(buffSize); |
| 106 | + auto holder = MakeHolder<IEventHandle>(TActorId{}, TActorId{}, new TEvents::TEvBlob(data)); |
| 107 | + event.Fill(*holder); |
| 108 | + } |
| 109 | + for (ui32 i = 0; i < repeats; i++) { |
| 110 | + pool.Release(q, q.begin()); |
| 111 | + } |
| 112 | + UNIT_ASSERT_LT_C(prof.GetUsed(), TEventHolderPool::MaxBytesPerMessage * 2, prof.GetUsed()); |
| 113 | + } |
| 114 | + |
| 115 | + Y_UNIT_TEST(MemConsumptionSmall) { |
| 116 | + MemComsumption(100'000, 4); |
| 117 | + } |
| 118 | + |
| 119 | + Y_UNIT_TEST(MemConsumptionLarge) { |
| 120 | + MemComsumption(10'000, 1024*1024); |
| 121 | + } |
59 | 122 | }
|
0 commit comments