Skip to content

Commit a6e61b4

Browse files
committed
core: Add tests for json defrag
Signed-off-by: Abhijat Malviya <[email protected]>
1 parent 16f261d commit a6e61b4

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/core/compact_object.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ void RobjWrapper::MakeInnerRoom(size_t current_cap, size_t desired, MemoryResour
723723
} // namespace detail
724724

725725
uint32_t JsonEnconding() {
726-
static thread_local uint32_t json_enc =
726+
thread_local uint32_t json_enc =
727727
absl::GetFlag(FLAGS_experimental_flat_json) ? kEncodingJsonFlat : kEncodingJsonCons;
728728
return json_enc;
729729
}

src/core/page_usage_stats_test.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,29 @@
44

55
#include "core/page_usage_stats.h"
66

7+
#include <absl/flags/flag.h>
8+
#include <absl/flags/reflection.h>
79
#include <gmock/gmock-matchers.h>
810

911
#include "base/gtest.h"
1012
#include "base/logging.h"
1113
#include "core/compact_object.h"
14+
#include "core/json/path.h"
1215
#include "core/mi_memory_resource.h"
1316
#include "core/qlist.h"
1417
#include "core/score_map.h"
1518
#include "core/small_string.h"
1619
#include "core/sorted_map.h"
1720
#include "core/string_map.h"
1821
#include "core/string_set.h"
22+
#include "redis/redis_aux.h"
1923

2024
extern "C" {
2125
#include "redis/zmalloc.h"
2226
}
2327

28+
ABSL_DECLARE_FLAG(bool, experimental_flat_json);
29+
2430
using namespace dfly;
2531

2632
class PageUsageStatsTest : public ::testing::Test {
@@ -45,6 +51,8 @@ class PageUsageStatsTest : public ::testing::Test {
4551
}
4652

4753
void SetUp() override {
54+
CompactObj::InitThreadLocal(&m_);
55+
4856
score_map_ = std::make_unique<ScoreMap>(&m_);
4957
sorted_map_ = std::make_unique<detail::SortedMap>(&m_);
5058
string_set_ = std::make_unique<StringSet>(&m_);
@@ -176,3 +184,56 @@ TEST_F(PageUsageStatsTest, StatCollection) {
176184
const CollectedPageStats::ShardUsageSummary expected{{50, 65}, {90, 85}, {99, 89}};
177185
EXPECT_EQ(usage.at(1), expected);
178186
}
187+
188+
TEST_F(PageUsageStatsTest, JSONCons) {
189+
std::string_view data{R"#({"data": "some", "count": 1, "checked": false})#"};
190+
191+
auto parsed = JsonFromString(data, &m_);
192+
EXPECT_TRUE(parsed.has_value());
193+
194+
c_obj_.SetJson(std::move(parsed.value()));
195+
196+
PageUsage p{CollectPageStats::YES, 0.1};
197+
p.SetForceReallocate(true);
198+
199+
c_obj_.DefragIfNeeded(&p);
200+
201+
const auto stats = p.CollectedStats();
202+
EXPECT_GT(stats.pages_scanned, 0);
203+
EXPECT_EQ(stats.objects_skipped_not_required, 0);
204+
205+
EXPECT_EQ(c_obj_.ObjType(), OBJ_JSON);
206+
207+
auto json_obj = c_obj_.GetJson();
208+
EXPECT_EQ(json_obj->at("data").as_string_view(), "some");
209+
EXPECT_EQ(json_obj->at("count").as_integer<uint8_t>(), 1);
210+
EXPECT_EQ(json_obj->at("checked").as_bool(), false);
211+
}
212+
213+
TEST_F(PageUsageStatsTest, JSONFlat) {
214+
absl::FlagSaver fs;
215+
absl::SetFlag(&FLAGS_experimental_flat_json, true);
216+
217+
std::string_view data{R"#({"data": "some", "count": 1, "checked": false})#"};
218+
219+
auto parsed = JsonFromString(data, &m_);
220+
EXPECT_TRUE(parsed.has_value());
221+
222+
flexbuffers::Builder b;
223+
json::FromJsonType(parsed.value(), &b);
224+
b.Finish();
225+
const auto& bytes = b.GetBuffer();
226+
c_obj_.SetJson(bytes.data(), bytes.size());
227+
228+
PageUsage p{CollectPageStats::YES, 0.1};
229+
p.SetForceReallocate(true);
230+
231+
c_obj_.DefragIfNeeded(&p);
232+
233+
const auto stats = p.CollectedStats();
234+
EXPECT_GT(stats.pages_scanned, 0);
235+
EXPECT_EQ(stats.objects_skipped_not_required, 0);
236+
237+
EXPECT_EQ(c_obj_.ObjType(), OBJ_JSON);
238+
// GetJSON does not work with flat encoding
239+
}

0 commit comments

Comments
 (0)