table_reader_bench.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 GFLAGS
  6. #include <cstdio>
  7. int main() {
  8. fprintf(stderr, "Please install gflags to run rocksdb tools\n");
  9. return 1;
  10. }
  11. #else
  12. #include "db/db_impl/db_impl.h"
  13. #include "db/dbformat.h"
  14. #include "env/composite_env_wrapper.h"
  15. #include "file/random_access_file_reader.h"
  16. #include "monitoring/histogram.h"
  17. #include "rocksdb/db.h"
  18. #include "rocksdb/slice_transform.h"
  19. #include "rocksdb/table.h"
  20. #include "table/block_based/block_based_table_factory.h"
  21. #include "table/get_context.h"
  22. #include "table/internal_iterator.h"
  23. #include "table/plain/plain_table_factory.h"
  24. #include "table/table_builder.h"
  25. #include "test_util/testharness.h"
  26. #include "test_util/testutil.h"
  27. #include "util/gflags_compat.h"
  28. using GFLAGS_NAMESPACE::ParseCommandLineFlags;
  29. using GFLAGS_NAMESPACE::SetUsageMessage;
  30. namespace ROCKSDB_NAMESPACE {
  31. namespace {
  32. // Make a key that i determines the first 4 characters and j determines the
  33. // last 4 characters.
  34. static std::string MakeKey(int i, int j, bool through_db) {
  35. char buf[100];
  36. snprintf(buf, sizeof(buf), "%04d__key___%04d", i, j);
  37. if (through_db) {
  38. return std::string(buf);
  39. }
  40. // If we directly query table, which operates on internal keys
  41. // instead of user keys, we need to add 8 bytes of internal
  42. // information (row type etc) to user key to make an internal
  43. // key.
  44. InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
  45. return key.Encode().ToString();
  46. }
  47. uint64_t Now(Env* env, bool measured_by_nanosecond) {
  48. return measured_by_nanosecond ? env->NowNanos() : env->NowMicros();
  49. }
  50. } // namespace
  51. // A very simple benchmark that.
  52. // Create a table with roughly numKey1 * numKey2 keys,
  53. // where there are numKey1 prefixes of the key, each has numKey2 number of
  54. // distinguished key, differing in the suffix part.
  55. // If if_query_empty_keys = false, query the existing keys numKey1 * numKey2
  56. // times randomly.
  57. // If if_query_empty_keys = true, query numKey1 * numKey2 random empty keys.
  58. // Print out the total time.
  59. // If through_db=true, a full DB will be created and queries will be against
  60. // it. Otherwise, operations will be directly through table level.
  61. //
  62. // If for_terator=true, instead of just query one key each time, it queries
  63. // a range sharing the same prefix.
  64. namespace {
  65. void TableReaderBenchmark(Options& opts, EnvOptions& env_options,
  66. ReadOptions& read_options, int num_keys1,
  67. int num_keys2, int num_iter, int /*prefix_len*/,
  68. bool if_query_empty_keys, bool for_iterator,
  69. bool through_db, bool measured_by_nanosecond) {
  70. ROCKSDB_NAMESPACE::InternalKeyComparator ikc(opts.comparator);
  71. std::string file_name =
  72. test::PerThreadDBPath("rocksdb_table_reader_benchmark");
  73. std::string dbname = test::PerThreadDBPath("rocksdb_table_reader_bench_db");
  74. WriteOptions wo;
  75. Env* env = Env::Default();
  76. TableBuilder* tb = nullptr;
  77. DB* db = nullptr;
  78. Status s;
  79. const ImmutableCFOptions ioptions(opts);
  80. const ColumnFamilyOptions cfo(opts);
  81. const MutableCFOptions moptions(cfo);
  82. std::unique_ptr<WritableFileWriter> file_writer;
  83. if (!through_db) {
  84. std::unique_ptr<WritableFile> file;
  85. env->NewWritableFile(file_name, &file, env_options);
  86. std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
  87. int_tbl_prop_collector_factories;
  88. file_writer.reset(new WritableFileWriter(
  89. NewLegacyWritableFileWrapper(std::move(file)), file_name, env_options));
  90. int unknown_level = -1;
  91. tb = opts.table_factory->NewTableBuilder(
  92. TableBuilderOptions(
  93. ioptions, moptions, ikc, &int_tbl_prop_collector_factories,
  94. CompressionType::kNoCompression, 0 /* sample_for_compression */,
  95. CompressionOptions(), false /* skip_filters */,
  96. kDefaultColumnFamilyName, unknown_level),
  97. 0 /* column_family_id */, file_writer.get());
  98. } else {
  99. s = DB::Open(opts, dbname, &db);
  100. ASSERT_OK(s);
  101. ASSERT_TRUE(db != nullptr);
  102. }
  103. // Populate slightly more than 1M keys
  104. for (int i = 0; i < num_keys1; i++) {
  105. for (int j = 0; j < num_keys2; j++) {
  106. std::string key = MakeKey(i * 2, j, through_db);
  107. if (!through_db) {
  108. tb->Add(key, key);
  109. } else {
  110. db->Put(wo, key, key);
  111. }
  112. }
  113. }
  114. if (!through_db) {
  115. tb->Finish();
  116. file_writer->Close();
  117. } else {
  118. db->Flush(FlushOptions());
  119. }
  120. std::unique_ptr<TableReader> table_reader;
  121. if (!through_db) {
  122. std::unique_ptr<RandomAccessFile> raf;
  123. s = env->NewRandomAccessFile(file_name, &raf, env_options);
  124. if (!s.ok()) {
  125. fprintf(stderr, "Create File Error: %s\n", s.ToString().c_str());
  126. exit(1);
  127. }
  128. uint64_t file_size;
  129. env->GetFileSize(file_name, &file_size);
  130. std::unique_ptr<RandomAccessFileReader> file_reader(
  131. new RandomAccessFileReader(NewLegacyRandomAccessFileWrapper(raf),
  132. file_name));
  133. s = opts.table_factory->NewTableReader(
  134. TableReaderOptions(ioptions, moptions.prefix_extractor.get(),
  135. env_options, ikc),
  136. std::move(file_reader), file_size, &table_reader);
  137. if (!s.ok()) {
  138. fprintf(stderr, "Open Table Error: %s\n", s.ToString().c_str());
  139. exit(1);
  140. }
  141. }
  142. Random rnd(301);
  143. std::string result;
  144. HistogramImpl hist;
  145. for (int it = 0; it < num_iter; it++) {
  146. for (int i = 0; i < num_keys1; i++) {
  147. for (int j = 0; j < num_keys2; j++) {
  148. int r1 = rnd.Uniform(num_keys1) * 2;
  149. int r2 = rnd.Uniform(num_keys2);
  150. if (if_query_empty_keys) {
  151. r1++;
  152. r2 = num_keys2 * 2 - r2;
  153. }
  154. if (!for_iterator) {
  155. // Query one existing key;
  156. std::string key = MakeKey(r1, r2, through_db);
  157. uint64_t start_time = Now(env, measured_by_nanosecond);
  158. if (!through_db) {
  159. PinnableSlice value;
  160. MergeContext merge_context;
  161. SequenceNumber max_covering_tombstone_seq = 0;
  162. GetContext get_context(ioptions.user_comparator,
  163. ioptions.merge_operator, ioptions.info_log,
  164. ioptions.statistics, GetContext::kNotFound,
  165. Slice(key), &value, nullptr, &merge_context,
  166. true, &max_covering_tombstone_seq, env);
  167. s = table_reader->Get(read_options, key, &get_context, nullptr);
  168. } else {
  169. s = db->Get(read_options, key, &result);
  170. }
  171. hist.Add(Now(env, measured_by_nanosecond) - start_time);
  172. } else {
  173. int r2_len;
  174. if (if_query_empty_keys) {
  175. r2_len = 0;
  176. } else {
  177. r2_len = rnd.Uniform(num_keys2) + 1;
  178. if (r2_len + r2 > num_keys2) {
  179. r2_len = num_keys2 - r2;
  180. }
  181. }
  182. std::string start_key = MakeKey(r1, r2, through_db);
  183. std::string end_key = MakeKey(r1, r2 + r2_len, through_db);
  184. uint64_t total_time = 0;
  185. uint64_t start_time = Now(env, measured_by_nanosecond);
  186. Iterator* iter = nullptr;
  187. InternalIterator* iiter = nullptr;
  188. if (!through_db) {
  189. iiter = table_reader->NewIterator(
  190. read_options, /*prefix_extractor=*/nullptr, /*arena=*/nullptr,
  191. /*skip_filters=*/false, TableReaderCaller::kUncategorized);
  192. } else {
  193. iter = db->NewIterator(read_options);
  194. }
  195. int count = 0;
  196. for (through_db ? iter->Seek(start_key) : iiter->Seek(start_key);
  197. through_db ? iter->Valid() : iiter->Valid();
  198. through_db ? iter->Next() : iiter->Next()) {
  199. if (if_query_empty_keys) {
  200. break;
  201. }
  202. // verify key;
  203. total_time += Now(env, measured_by_nanosecond) - start_time;
  204. assert(Slice(MakeKey(r1, r2 + count, through_db)) ==
  205. (through_db ? iter->key() : iiter->key()));
  206. start_time = Now(env, measured_by_nanosecond);
  207. if (++count >= r2_len) {
  208. break;
  209. }
  210. }
  211. if (count != r2_len) {
  212. fprintf(
  213. stderr, "Iterator cannot iterate expected number of entries. "
  214. "Expected %d but got %d\n", r2_len, count);
  215. assert(false);
  216. }
  217. delete iter;
  218. total_time += Now(env, measured_by_nanosecond) - start_time;
  219. hist.Add(total_time);
  220. }
  221. }
  222. }
  223. }
  224. fprintf(
  225. stderr,
  226. "==================================================="
  227. "====================================================\n"
  228. "InMemoryTableSimpleBenchmark: %20s num_key1: %5d "
  229. "num_key2: %5d %10s\n"
  230. "==================================================="
  231. "===================================================="
  232. "\nHistogram (unit: %s): \n%s",
  233. opts.table_factory->Name(), num_keys1, num_keys2,
  234. for_iterator ? "iterator" : (if_query_empty_keys ? "empty" : "non_empty"),
  235. measured_by_nanosecond ? "nanosecond" : "microsecond",
  236. hist.ToString().c_str());
  237. if (!through_db) {
  238. env->DeleteFile(file_name);
  239. } else {
  240. delete db;
  241. db = nullptr;
  242. DestroyDB(dbname, opts);
  243. }
  244. }
  245. } // namespace
  246. } // namespace ROCKSDB_NAMESPACE
  247. DEFINE_bool(query_empty, false, "query non-existing keys instead of existing "
  248. "ones.");
  249. DEFINE_int32(num_keys1, 4096, "number of distinguish prefix of keys");
  250. DEFINE_int32(num_keys2, 512, "number of distinguish keys for each prefix");
  251. DEFINE_int32(iter, 3, "query non-existing keys instead of existing ones");
  252. DEFINE_int32(prefix_len, 16, "Prefix length used for iterators and indexes");
  253. DEFINE_bool(iterator, false, "For test iterator");
  254. DEFINE_bool(through_db, false, "If enable, a DB instance will be created and "
  255. "the query will be against DB. Otherwise, will be directly against "
  256. "a table reader.");
  257. DEFINE_bool(mmap_read, true, "Whether use mmap read");
  258. DEFINE_string(table_factory, "block_based",
  259. "Table factory to use: `block_based` (default), `plain_table` or "
  260. "`cuckoo_hash`.");
  261. DEFINE_string(time_unit, "microsecond",
  262. "The time unit used for measuring performance. User can specify "
  263. "`microsecond` (default) or `nanosecond`");
  264. int main(int argc, char** argv) {
  265. SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv[0]) +
  266. " [OPTIONS]...");
  267. ParseCommandLineFlags(&argc, &argv, true);
  268. std::shared_ptr<ROCKSDB_NAMESPACE::TableFactory> tf;
  269. ROCKSDB_NAMESPACE::Options options;
  270. if (FLAGS_prefix_len < 16) {
  271. options.prefix_extractor.reset(
  272. ROCKSDB_NAMESPACE::NewFixedPrefixTransform(FLAGS_prefix_len));
  273. }
  274. ROCKSDB_NAMESPACE::ReadOptions ro;
  275. ROCKSDB_NAMESPACE::EnvOptions env_options;
  276. options.create_if_missing = true;
  277. options.compression = ROCKSDB_NAMESPACE::CompressionType::kNoCompression;
  278. if (FLAGS_table_factory == "cuckoo_hash") {
  279. #ifndef ROCKSDB_LITE
  280. options.allow_mmap_reads = FLAGS_mmap_read;
  281. env_options.use_mmap_reads = FLAGS_mmap_read;
  282. ROCKSDB_NAMESPACE::CuckooTableOptions table_options;
  283. table_options.hash_table_ratio = 0.75;
  284. tf.reset(ROCKSDB_NAMESPACE::NewCuckooTableFactory(table_options));
  285. #else
  286. fprintf(stderr, "Plain table is not supported in lite mode\n");
  287. exit(1);
  288. #endif // ROCKSDB_LITE
  289. } else if (FLAGS_table_factory == "plain_table") {
  290. #ifndef ROCKSDB_LITE
  291. options.allow_mmap_reads = FLAGS_mmap_read;
  292. env_options.use_mmap_reads = FLAGS_mmap_read;
  293. ROCKSDB_NAMESPACE::PlainTableOptions plain_table_options;
  294. plain_table_options.user_key_len = 16;
  295. plain_table_options.bloom_bits_per_key = (FLAGS_prefix_len == 16) ? 0 : 8;
  296. plain_table_options.hash_table_ratio = 0.75;
  297. tf.reset(new ROCKSDB_NAMESPACE::PlainTableFactory(plain_table_options));
  298. options.prefix_extractor.reset(
  299. ROCKSDB_NAMESPACE::NewFixedPrefixTransform(FLAGS_prefix_len));
  300. #else
  301. fprintf(stderr, "Cuckoo table is not supported in lite mode\n");
  302. exit(1);
  303. #endif // ROCKSDB_LITE
  304. } else if (FLAGS_table_factory == "block_based") {
  305. tf.reset(new ROCKSDB_NAMESPACE::BlockBasedTableFactory());
  306. } else {
  307. fprintf(stderr, "Invalid table type %s\n", FLAGS_table_factory.c_str());
  308. }
  309. if (tf) {
  310. // if user provides invalid options, just fall back to microsecond.
  311. bool measured_by_nanosecond = FLAGS_time_unit == "nanosecond";
  312. options.table_factory = tf;
  313. ROCKSDB_NAMESPACE::TableReaderBenchmark(
  314. options, env_options, ro, FLAGS_num_keys1, FLAGS_num_keys2, FLAGS_iter,
  315. FLAGS_prefix_len, FLAGS_query_empty, FLAGS_iterator, FLAGS_through_db,
  316. measured_by_nanosecond);
  317. } else {
  318. return 1;
  319. }
  320. return 0;
  321. }
  322. #endif // GFLAGS