cuckoo_table_reader_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #ifndef ROCKSDB_LITE
  6. #ifndef GFLAGS
  7. #include <cstdio>
  8. int main() {
  9. fprintf(stderr, "Please install gflags to run this test... Skipping...\n");
  10. return 0;
  11. }
  12. #else
  13. #include <cinttypes>
  14. #include <map>
  15. #include <string>
  16. #include <vector>
  17. #include "memory/arena.h"
  18. #include "table/cuckoo/cuckoo_table_builder.h"
  19. #include "table/cuckoo/cuckoo_table_factory.h"
  20. #include "table/cuckoo/cuckoo_table_reader.h"
  21. #include "table/get_context.h"
  22. #include "table/meta_blocks.h"
  23. #include "test_util/testharness.h"
  24. #include "test_util/testutil.h"
  25. #include "util/gflags_compat.h"
  26. #include "util/random.h"
  27. #include "util/string_util.h"
  28. using GFLAGS_NAMESPACE::ParseCommandLineFlags;
  29. using GFLAGS_NAMESPACE::SetUsageMessage;
  30. DEFINE_string(file_dir, "", "Directory where the files will be created"
  31. " for benchmark. Added for using tmpfs.");
  32. DEFINE_bool(enable_perf, false, "Run Benchmark Tests too.");
  33. DEFINE_bool(write, false,
  34. "Should write new values to file in performance tests?");
  35. DEFINE_bool(identity_as_first_hash, true, "use identity as first hash");
  36. namespace ROCKSDB_NAMESPACE {
  37. namespace {
  38. const uint32_t kNumHashFunc = 10;
  39. // Methods, variables related to Hash functions.
  40. std::unordered_map<std::string, std::vector<uint64_t>> hash_map;
  41. void AddHashLookups(const std::string& s, uint64_t bucket_id,
  42. uint32_t num_hash_fun) {
  43. std::vector<uint64_t> v;
  44. for (uint32_t i = 0; i < num_hash_fun; i++) {
  45. v.push_back(bucket_id + i);
  46. }
  47. hash_map[s] = v;
  48. }
  49. uint64_t GetSliceHash(const Slice& s, uint32_t index,
  50. uint64_t /*max_num_buckets*/) {
  51. return hash_map[s.ToString()][index];
  52. }
  53. } // namespace
  54. class CuckooReaderTest : public testing::Test {
  55. public:
  56. using testing::Test::SetUp;
  57. CuckooReaderTest() {
  58. options.allow_mmap_reads = true;
  59. env = options.env;
  60. env_options = EnvOptions(options);
  61. }
  62. void SetUp(int num) {
  63. num_items = num;
  64. hash_map.clear();
  65. keys.clear();
  66. keys.resize(num_items);
  67. user_keys.clear();
  68. user_keys.resize(num_items);
  69. values.clear();
  70. values.resize(num_items);
  71. }
  72. std::string NumToStr(int64_t i) {
  73. return std::string(reinterpret_cast<char*>(&i), sizeof(i));
  74. }
  75. void CreateCuckooFileAndCheckReader(
  76. const Comparator* ucomp = BytewiseComparator()) {
  77. std::unique_ptr<WritableFile> writable_file;
  78. ASSERT_OK(env->NewWritableFile(fname, &writable_file, env_options));
  79. std::unique_ptr<WritableFileWriter> file_writer(new WritableFileWriter(
  80. NewLegacyWritableFileWrapper(std::move(writable_file)), fname,
  81. env_options));
  82. CuckooTableBuilder builder(
  83. file_writer.get(), 0.9, kNumHashFunc, 100, ucomp, 2, false, false,
  84. GetSliceHash, 0 /* column_family_id */, kDefaultColumnFamilyName);
  85. ASSERT_OK(builder.status());
  86. for (uint32_t key_idx = 0; key_idx < num_items; ++key_idx) {
  87. builder.Add(Slice(keys[key_idx]), Slice(values[key_idx]));
  88. ASSERT_OK(builder.status());
  89. ASSERT_EQ(builder.NumEntries(), key_idx + 1);
  90. }
  91. ASSERT_OK(builder.Finish());
  92. ASSERT_EQ(num_items, builder.NumEntries());
  93. file_size = builder.FileSize();
  94. ASSERT_OK(file_writer->Close());
  95. // Check reader now.
  96. std::unique_ptr<RandomAccessFile> read_file;
  97. ASSERT_OK(env->NewRandomAccessFile(fname, &read_file, env_options));
  98. std::unique_ptr<RandomAccessFileReader> file_reader(
  99. new RandomAccessFileReader(NewLegacyRandomAccessFileWrapper(read_file),
  100. fname));
  101. const ImmutableCFOptions ioptions(options);
  102. CuckooTableReader reader(ioptions, std::move(file_reader), file_size, ucomp,
  103. GetSliceHash);
  104. ASSERT_OK(reader.status());
  105. // Assume no merge/deletion
  106. for (uint32_t i = 0; i < num_items; ++i) {
  107. PinnableSlice value;
  108. GetContext get_context(ucomp, nullptr, nullptr, nullptr,
  109. GetContext::kNotFound, Slice(user_keys[i]), &value,
  110. nullptr, nullptr, true, nullptr, nullptr);
  111. ASSERT_OK(
  112. reader.Get(ReadOptions(), Slice(keys[i]), &get_context, nullptr));
  113. ASSERT_STREQ(values[i].c_str(), value.data());
  114. }
  115. }
  116. void UpdateKeys(bool with_zero_seqno) {
  117. for (uint32_t i = 0; i < num_items; i++) {
  118. ParsedInternalKey ikey(user_keys[i],
  119. with_zero_seqno ? 0 : i + 1000, kTypeValue);
  120. keys[i].clear();
  121. AppendInternalKey(&keys[i], ikey);
  122. }
  123. }
  124. void CheckIterator(const Comparator* ucomp = BytewiseComparator()) {
  125. std::unique_ptr<RandomAccessFile> read_file;
  126. ASSERT_OK(env->NewRandomAccessFile(fname, &read_file, env_options));
  127. std::unique_ptr<RandomAccessFileReader> file_reader(
  128. new RandomAccessFileReader(NewLegacyRandomAccessFileWrapper(read_file),
  129. fname));
  130. const ImmutableCFOptions ioptions(options);
  131. CuckooTableReader reader(ioptions, std::move(file_reader), file_size, ucomp,
  132. GetSliceHash);
  133. ASSERT_OK(reader.status());
  134. InternalIterator* it = reader.NewIterator(
  135. ReadOptions(), /*prefix_extractor=*/nullptr, /*arena=*/nullptr,
  136. /*skip_filters=*/false, TableReaderCaller::kUncategorized);
  137. ASSERT_OK(it->status());
  138. ASSERT_TRUE(!it->Valid());
  139. it->SeekToFirst();
  140. int cnt = 0;
  141. while (it->Valid()) {
  142. ASSERT_OK(it->status());
  143. ASSERT_TRUE(Slice(keys[cnt]) == it->key());
  144. ASSERT_TRUE(Slice(values[cnt]) == it->value());
  145. ++cnt;
  146. it->Next();
  147. }
  148. ASSERT_EQ(static_cast<uint32_t>(cnt), num_items);
  149. it->SeekToLast();
  150. cnt = static_cast<int>(num_items) - 1;
  151. ASSERT_TRUE(it->Valid());
  152. while (it->Valid()) {
  153. ASSERT_OK(it->status());
  154. ASSERT_TRUE(Slice(keys[cnt]) == it->key());
  155. ASSERT_TRUE(Slice(values[cnt]) == it->value());
  156. --cnt;
  157. it->Prev();
  158. }
  159. ASSERT_EQ(cnt, -1);
  160. cnt = static_cast<int>(num_items) / 2;
  161. it->Seek(keys[cnt]);
  162. while (it->Valid()) {
  163. ASSERT_OK(it->status());
  164. ASSERT_TRUE(Slice(keys[cnt]) == it->key());
  165. ASSERT_TRUE(Slice(values[cnt]) == it->value());
  166. ++cnt;
  167. it->Next();
  168. }
  169. ASSERT_EQ(static_cast<uint32_t>(cnt), num_items);
  170. delete it;
  171. Arena arena;
  172. it = reader.NewIterator(ReadOptions(), /*prefix_extractor=*/nullptr, &arena,
  173. /*skip_filters=*/false,
  174. TableReaderCaller::kUncategorized);
  175. ASSERT_OK(it->status());
  176. ASSERT_TRUE(!it->Valid());
  177. it->Seek(keys[num_items/2]);
  178. ASSERT_TRUE(it->Valid());
  179. ASSERT_OK(it->status());
  180. ASSERT_TRUE(keys[num_items/2] == it->key());
  181. ASSERT_TRUE(values[num_items/2] == it->value());
  182. ASSERT_OK(it->status());
  183. it->~InternalIterator();
  184. }
  185. std::vector<std::string> keys;
  186. std::vector<std::string> user_keys;
  187. std::vector<std::string> values;
  188. uint64_t num_items;
  189. std::string fname;
  190. uint64_t file_size;
  191. Options options;
  192. Env* env;
  193. EnvOptions env_options;
  194. };
  195. TEST_F(CuckooReaderTest, WhenKeyExists) {
  196. SetUp(kNumHashFunc);
  197. fname = test::PerThreadDBPath("CuckooReader_WhenKeyExists");
  198. for (uint64_t i = 0; i < num_items; i++) {
  199. user_keys[i] = "key" + NumToStr(i);
  200. ParsedInternalKey ikey(user_keys[i], i + 1000, kTypeValue);
  201. AppendInternalKey(&keys[i], ikey);
  202. values[i] = "value" + NumToStr(i);
  203. // Give disjoint hash values.
  204. AddHashLookups(user_keys[i], i, kNumHashFunc);
  205. }
  206. CreateCuckooFileAndCheckReader();
  207. // Last level file.
  208. UpdateKeys(true);
  209. CreateCuckooFileAndCheckReader();
  210. // Test with collision. Make all hash values collide.
  211. hash_map.clear();
  212. for (uint32_t i = 0; i < num_items; i++) {
  213. AddHashLookups(user_keys[i], 0, kNumHashFunc);
  214. }
  215. UpdateKeys(false);
  216. CreateCuckooFileAndCheckReader();
  217. // Last level file.
  218. UpdateKeys(true);
  219. CreateCuckooFileAndCheckReader();
  220. }
  221. TEST_F(CuckooReaderTest, WhenKeyExistsWithUint64Comparator) {
  222. SetUp(kNumHashFunc);
  223. fname = test::PerThreadDBPath("CuckooReaderUint64_WhenKeyExists");
  224. for (uint64_t i = 0; i < num_items; i++) {
  225. user_keys[i].resize(8);
  226. memcpy(&user_keys[i][0], static_cast<void*>(&i), 8);
  227. ParsedInternalKey ikey(user_keys[i], i + 1000, kTypeValue);
  228. AppendInternalKey(&keys[i], ikey);
  229. values[i] = "value" + NumToStr(i);
  230. // Give disjoint hash values.
  231. AddHashLookups(user_keys[i], i, kNumHashFunc);
  232. }
  233. CreateCuckooFileAndCheckReader(test::Uint64Comparator());
  234. // Last level file.
  235. UpdateKeys(true);
  236. CreateCuckooFileAndCheckReader(test::Uint64Comparator());
  237. // Test with collision. Make all hash values collide.
  238. hash_map.clear();
  239. for (uint32_t i = 0; i < num_items; i++) {
  240. AddHashLookups(user_keys[i], 0, kNumHashFunc);
  241. }
  242. UpdateKeys(false);
  243. CreateCuckooFileAndCheckReader(test::Uint64Comparator());
  244. // Last level file.
  245. UpdateKeys(true);
  246. CreateCuckooFileAndCheckReader(test::Uint64Comparator());
  247. }
  248. TEST_F(CuckooReaderTest, CheckIterator) {
  249. SetUp(2*kNumHashFunc);
  250. fname = test::PerThreadDBPath("CuckooReader_CheckIterator");
  251. for (uint64_t i = 0; i < num_items; i++) {
  252. user_keys[i] = "key" + NumToStr(i);
  253. ParsedInternalKey ikey(user_keys[i], 1000, kTypeValue);
  254. AppendInternalKey(&keys[i], ikey);
  255. values[i] = "value" + NumToStr(i);
  256. // Give disjoint hash values, in reverse order.
  257. AddHashLookups(user_keys[i], num_items-i-1, kNumHashFunc);
  258. }
  259. CreateCuckooFileAndCheckReader();
  260. CheckIterator();
  261. // Last level file.
  262. UpdateKeys(true);
  263. CreateCuckooFileAndCheckReader();
  264. CheckIterator();
  265. }
  266. TEST_F(CuckooReaderTest, CheckIteratorUint64) {
  267. SetUp(2*kNumHashFunc);
  268. fname = test::PerThreadDBPath("CuckooReader_CheckIterator");
  269. for (uint64_t i = 0; i < num_items; i++) {
  270. user_keys[i].resize(8);
  271. memcpy(&user_keys[i][0], static_cast<void*>(&i), 8);
  272. ParsedInternalKey ikey(user_keys[i], 1000, kTypeValue);
  273. AppendInternalKey(&keys[i], ikey);
  274. values[i] = "value" + NumToStr(i);
  275. // Give disjoint hash values, in reverse order.
  276. AddHashLookups(user_keys[i], num_items-i-1, kNumHashFunc);
  277. }
  278. CreateCuckooFileAndCheckReader(test::Uint64Comparator());
  279. CheckIterator(test::Uint64Comparator());
  280. // Last level file.
  281. UpdateKeys(true);
  282. CreateCuckooFileAndCheckReader(test::Uint64Comparator());
  283. CheckIterator(test::Uint64Comparator());
  284. }
  285. TEST_F(CuckooReaderTest, WhenKeyNotFound) {
  286. // Add keys with colliding hash values.
  287. SetUp(kNumHashFunc);
  288. fname = test::PerThreadDBPath("CuckooReader_WhenKeyNotFound");
  289. for (uint64_t i = 0; i < num_items; i++) {
  290. user_keys[i] = "key" + NumToStr(i);
  291. ParsedInternalKey ikey(user_keys[i], i + 1000, kTypeValue);
  292. AppendInternalKey(&keys[i], ikey);
  293. values[i] = "value" + NumToStr(i);
  294. // Make all hash values collide.
  295. AddHashLookups(user_keys[i], 0, kNumHashFunc);
  296. }
  297. auto* ucmp = BytewiseComparator();
  298. CreateCuckooFileAndCheckReader();
  299. std::unique_ptr<RandomAccessFile> read_file;
  300. ASSERT_OK(env->NewRandomAccessFile(fname, &read_file, env_options));
  301. std::unique_ptr<RandomAccessFileReader> file_reader(
  302. new RandomAccessFileReader(NewLegacyRandomAccessFileWrapper(read_file),
  303. fname));
  304. const ImmutableCFOptions ioptions(options);
  305. CuckooTableReader reader(ioptions, std::move(file_reader), file_size, ucmp,
  306. GetSliceHash);
  307. ASSERT_OK(reader.status());
  308. // Search for a key with colliding hash values.
  309. std::string not_found_user_key = "key" + NumToStr(num_items);
  310. std::string not_found_key;
  311. AddHashLookups(not_found_user_key, 0, kNumHashFunc);
  312. ParsedInternalKey ikey(not_found_user_key, 1000, kTypeValue);
  313. AppendInternalKey(&not_found_key, ikey);
  314. PinnableSlice value;
  315. GetContext get_context(ucmp, nullptr, nullptr, nullptr, GetContext::kNotFound,
  316. Slice(not_found_key), &value, nullptr, nullptr, true,
  317. nullptr, nullptr);
  318. ASSERT_OK(
  319. reader.Get(ReadOptions(), Slice(not_found_key), &get_context, nullptr));
  320. ASSERT_TRUE(value.empty());
  321. ASSERT_OK(reader.status());
  322. // Search for a key with an independent hash value.
  323. std::string not_found_user_key2 = "key" + NumToStr(num_items + 1);
  324. AddHashLookups(not_found_user_key2, kNumHashFunc, kNumHashFunc);
  325. ParsedInternalKey ikey2(not_found_user_key2, 1000, kTypeValue);
  326. std::string not_found_key2;
  327. AppendInternalKey(&not_found_key2, ikey2);
  328. value.Reset();
  329. GetContext get_context2(ucmp, nullptr, nullptr, nullptr,
  330. GetContext::kNotFound, Slice(not_found_key2), &value,
  331. nullptr, nullptr, true, nullptr, nullptr);
  332. ASSERT_OK(
  333. reader.Get(ReadOptions(), Slice(not_found_key2), &get_context2, nullptr));
  334. ASSERT_TRUE(value.empty());
  335. ASSERT_OK(reader.status());
  336. // Test read when key is unused key.
  337. std::string unused_key =
  338. reader.GetTableProperties()->user_collected_properties.at(
  339. CuckooTablePropertyNames::kEmptyKey);
  340. // Add hash values that map to empty buckets.
  341. AddHashLookups(ExtractUserKey(unused_key).ToString(),
  342. kNumHashFunc, kNumHashFunc);
  343. value.Reset();
  344. GetContext get_context3(ucmp, nullptr, nullptr, nullptr,
  345. GetContext::kNotFound, Slice(unused_key), &value,
  346. nullptr, nullptr, true, nullptr, nullptr);
  347. ASSERT_OK(
  348. reader.Get(ReadOptions(), Slice(unused_key), &get_context3, nullptr));
  349. ASSERT_TRUE(value.empty());
  350. ASSERT_OK(reader.status());
  351. }
  352. // Performance tests
  353. namespace {
  354. void GetKeys(uint64_t num, std::vector<std::string>* keys) {
  355. keys->clear();
  356. IterKey k;
  357. k.SetInternalKey("", 0, kTypeValue);
  358. std::string internal_key_suffix = k.GetInternalKey().ToString();
  359. ASSERT_EQ(static_cast<size_t>(8), internal_key_suffix.size());
  360. for (uint64_t key_idx = 0; key_idx < num; ++key_idx) {
  361. uint64_t value = 2 * key_idx;
  362. std::string new_key(reinterpret_cast<char*>(&value), sizeof(value));
  363. new_key += internal_key_suffix;
  364. keys->push_back(new_key);
  365. }
  366. }
  367. std::string GetFileName(uint64_t num) {
  368. if (FLAGS_file_dir.empty()) {
  369. FLAGS_file_dir = test::TmpDir();
  370. }
  371. return test::PerThreadDBPath(FLAGS_file_dir, "cuckoo_read_benchmark") +
  372. ToString(num / 1000000) + "Mkeys";
  373. }
  374. // Create last level file as we are interested in measuring performance of
  375. // last level file only.
  376. void WriteFile(const std::vector<std::string>& keys,
  377. const uint64_t num, double hash_ratio) {
  378. Options options;
  379. options.allow_mmap_reads = true;
  380. Env* env = options.env;
  381. EnvOptions env_options = EnvOptions(options);
  382. std::string fname = GetFileName(num);
  383. std::unique_ptr<WritableFile> writable_file;
  384. ASSERT_OK(env->NewWritableFile(fname, &writable_file, env_options));
  385. std::unique_ptr<WritableFileWriter> file_writer(new WritableFileWriter(
  386. NewLegacyWritableFileWrapper(std::move(writable_file)), fname,
  387. env_options));
  388. CuckooTableBuilder builder(
  389. file_writer.get(), hash_ratio, 64, 1000, test::Uint64Comparator(), 5,
  390. false, FLAGS_identity_as_first_hash, nullptr, 0 /* column_family_id */,
  391. kDefaultColumnFamilyName);
  392. ASSERT_OK(builder.status());
  393. for (uint64_t key_idx = 0; key_idx < num; ++key_idx) {
  394. // Value is just a part of key.
  395. builder.Add(Slice(keys[key_idx]), Slice(&keys[key_idx][0], 4));
  396. ASSERT_EQ(builder.NumEntries(), key_idx + 1);
  397. ASSERT_OK(builder.status());
  398. }
  399. ASSERT_OK(builder.Finish());
  400. ASSERT_EQ(num, builder.NumEntries());
  401. ASSERT_OK(file_writer->Close());
  402. uint64_t file_size;
  403. env->GetFileSize(fname, &file_size);
  404. std::unique_ptr<RandomAccessFile> read_file;
  405. ASSERT_OK(env->NewRandomAccessFile(fname, &read_file, env_options));
  406. std::unique_ptr<RandomAccessFileReader> file_reader(
  407. new RandomAccessFileReader(NewLegacyRandomAccessFileWrapper(read_file),
  408. fname));
  409. const ImmutableCFOptions ioptions(options);
  410. CuckooTableReader reader(ioptions, std::move(file_reader), file_size,
  411. test::Uint64Comparator(), nullptr);
  412. ASSERT_OK(reader.status());
  413. ReadOptions r_options;
  414. PinnableSlice value;
  415. // Assume only the fast path is triggered
  416. GetContext get_context(nullptr, nullptr, nullptr, nullptr,
  417. GetContext::kNotFound, Slice(), &value, nullptr,
  418. nullptr, true, nullptr, nullptr);
  419. for (uint64_t i = 0; i < num; ++i) {
  420. value.Reset();
  421. value.clear();
  422. ASSERT_OK(reader.Get(r_options, Slice(keys[i]), &get_context, nullptr));
  423. ASSERT_TRUE(Slice(keys[i]) == Slice(&keys[i][0], 4));
  424. }
  425. }
  426. void ReadKeys(uint64_t num, uint32_t batch_size) {
  427. Options options;
  428. options.allow_mmap_reads = true;
  429. Env* env = options.env;
  430. EnvOptions env_options = EnvOptions(options);
  431. std::string fname = GetFileName(num);
  432. uint64_t file_size;
  433. env->GetFileSize(fname, &file_size);
  434. std::unique_ptr<RandomAccessFile> read_file;
  435. ASSERT_OK(env->NewRandomAccessFile(fname, &read_file, env_options));
  436. std::unique_ptr<RandomAccessFileReader> file_reader(
  437. new RandomAccessFileReader(NewLegacyRandomAccessFileWrapper(read_file),
  438. fname));
  439. const ImmutableCFOptions ioptions(options);
  440. CuckooTableReader reader(ioptions, std::move(file_reader), file_size,
  441. test::Uint64Comparator(), nullptr);
  442. ASSERT_OK(reader.status());
  443. const UserCollectedProperties user_props =
  444. reader.GetTableProperties()->user_collected_properties;
  445. const uint32_t num_hash_fun = *reinterpret_cast<const uint32_t*>(
  446. user_props.at(CuckooTablePropertyNames::kNumHashFunc).data());
  447. const uint64_t table_size = *reinterpret_cast<const uint64_t*>(
  448. user_props.at(CuckooTablePropertyNames::kHashTableSize).data());
  449. fprintf(stderr, "With %" PRIu64 " items, utilization is %.2f%%, number of"
  450. " hash functions: %u.\n", num, num * 100.0 / (table_size), num_hash_fun);
  451. ReadOptions r_options;
  452. std::vector<uint64_t> keys;
  453. keys.reserve(num);
  454. for (uint64_t i = 0; i < num; ++i) {
  455. keys.push_back(2 * i);
  456. }
  457. std::random_shuffle(keys.begin(), keys.end());
  458. PinnableSlice value;
  459. // Assume only the fast path is triggered
  460. GetContext get_context(nullptr, nullptr, nullptr, nullptr,
  461. GetContext::kNotFound, Slice(), &value, nullptr,
  462. nullptr, true, nullptr, nullptr);
  463. uint64_t start_time = env->NowMicros();
  464. if (batch_size > 0) {
  465. for (uint64_t i = 0; i < num; i += batch_size) {
  466. for (uint64_t j = i; j < i+batch_size && j < num; ++j) {
  467. reader.Prepare(Slice(reinterpret_cast<char*>(&keys[j]), 16));
  468. }
  469. for (uint64_t j = i; j < i+batch_size && j < num; ++j) {
  470. reader.Get(r_options, Slice(reinterpret_cast<char*>(&keys[j]), 16),
  471. &get_context, nullptr);
  472. }
  473. }
  474. } else {
  475. for (uint64_t i = 0; i < num; i++) {
  476. reader.Get(r_options, Slice(reinterpret_cast<char*>(&keys[i]), 16),
  477. &get_context, nullptr);
  478. }
  479. }
  480. float time_per_op = (env->NowMicros() - start_time) * 1.0f / num;
  481. fprintf(stderr,
  482. "Time taken per op is %.3fus (%.1f Mqps) with batch size of %u\n",
  483. time_per_op, 1.0 / time_per_op, batch_size);
  484. }
  485. } // namespace.
  486. TEST_F(CuckooReaderTest, TestReadPerformance) {
  487. if (!FLAGS_enable_perf) {
  488. return;
  489. }
  490. double hash_ratio = 0.95;
  491. // These numbers are chosen to have a hash utilization % close to
  492. // 0.9, 0.75, 0.6 and 0.5 respectively.
  493. // They all create 128 M buckets.
  494. std::vector<uint64_t> nums = {120*1024*1024, 100*1024*1024, 80*1024*1024,
  495. 70*1024*1024};
  496. #ifndef NDEBUG
  497. fprintf(stdout,
  498. "WARNING: Not compiled with DNDEBUG. Performance tests may be slow.\n");
  499. #endif
  500. for (uint64_t num : nums) {
  501. if (FLAGS_write ||
  502. Env::Default()->FileExists(GetFileName(num)).IsNotFound()) {
  503. std::vector<std::string> all_keys;
  504. GetKeys(num, &all_keys);
  505. WriteFile(all_keys, num, hash_ratio);
  506. }
  507. ReadKeys(num, 0);
  508. ReadKeys(num, 10);
  509. ReadKeys(num, 25);
  510. ReadKeys(num, 50);
  511. ReadKeys(num, 100);
  512. fprintf(stderr, "\n");
  513. }
  514. }
  515. } // namespace ROCKSDB_NAMESPACE
  516. int main(int argc, char** argv) {
  517. if (ROCKSDB_NAMESPACE::port::kLittleEndian) {
  518. ::testing::InitGoogleTest(&argc, argv);
  519. ParseCommandLineFlags(&argc, &argv, true);
  520. return RUN_ALL_TESTS();
  521. } else {
  522. fprintf(stderr, "SKIPPED as Cuckoo table doesn't support Big Endian\n");
  523. return 0;
  524. }
  525. }
  526. #endif // GFLAGS.
  527. #else
  528. #include <stdio.h>
  529. int main(int /*argc*/, char** /*argv*/) {
  530. fprintf(stderr, "SKIPPED as Cuckoo table is not supported in ROCKSDB_LITE\n");
  531. return 0;
  532. }
  533. #endif // ROCKSDB_LITE