db_bench_tool_test.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "rocksdb/db_bench_tool.h"
  10. #include "options/options_parser.h"
  11. #include "rocksdb/utilities/options_util.h"
  12. #include "test_util/testharness.h"
  13. #include "test_util/testutil.h"
  14. #include "util/random.h"
  15. #ifdef GFLAGS
  16. #include "util/gflags_compat.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. namespace {
  19. static const int kMaxArgCount = 100;
  20. static const size_t kArgBufferSize = 100000;
  21. } // namespace
  22. class DBBenchTest : public testing::Test {
  23. public:
  24. DBBenchTest() : rnd_(0xFB) {
  25. test_path_ = test::PerThreadDBPath("db_bench_test");
  26. Env::Default()->CreateDir(test_path_);
  27. db_path_ = test_path_ + "/db";
  28. wal_path_ = test_path_ + "/wal";
  29. }
  30. ~DBBenchTest() {
  31. // DestroyDB(db_path_, Options());
  32. }
  33. void ResetArgs() {
  34. argc_ = 0;
  35. cursor_ = 0;
  36. memset(arg_buffer_, 0, kArgBufferSize);
  37. }
  38. void AppendArgs(const std::vector<std::string>& args) {
  39. for (const auto& arg : args) {
  40. ASSERT_LE(cursor_ + arg.size() + 1, kArgBufferSize);
  41. ASSERT_LE(argc_ + 1, kMaxArgCount);
  42. snprintf(arg_buffer_ + cursor_, arg.size() + 1, "%s", arg.c_str());
  43. argv_[argc_++] = arg_buffer_ + cursor_;
  44. cursor_ += arg.size() + 1;
  45. }
  46. }
  47. void RunDbBench(const std::string& options_file_name) {
  48. AppendArgs({"./db_bench", "--benchmarks=fillseq", "--use_existing_db=0",
  49. "--num=1000",
  50. std::string(std::string("--db=") + db_path_).c_str(),
  51. std::string(std::string("--wal_dir=") + wal_path_).c_str(),
  52. std::string(std::string("--options_file=") + options_file_name)
  53. .c_str()});
  54. ASSERT_EQ(0, db_bench_tool(argc(), argv()));
  55. }
  56. void VerifyOptions(const Options& opt) {
  57. DBOptions loaded_db_opts;
  58. std::vector<ColumnFamilyDescriptor> cf_descs;
  59. ASSERT_OK(LoadLatestOptions(db_path_, FileSystem::Default(),
  60. &loaded_db_opts, &cf_descs));
  61. ASSERT_OK(
  62. RocksDBOptionsParser::VerifyDBOptions(DBOptions(opt), loaded_db_opts));
  63. ASSERT_OK(RocksDBOptionsParser::VerifyCFOptions(ColumnFamilyOptions(opt),
  64. cf_descs[0].options));
  65. // check with the default rocksdb options and expect failure
  66. ASSERT_NOK(
  67. RocksDBOptionsParser::VerifyDBOptions(DBOptions(), loaded_db_opts));
  68. ASSERT_NOK(RocksDBOptionsParser::VerifyCFOptions(ColumnFamilyOptions(),
  69. cf_descs[0].options));
  70. }
  71. char** argv() { return argv_; }
  72. int argc() { return argc_; }
  73. std::string db_path_;
  74. std::string test_path_;
  75. std::string wal_path_;
  76. char arg_buffer_[kArgBufferSize];
  77. char* argv_[kMaxArgCount];
  78. int argc_ = 0;
  79. int cursor_ = 0;
  80. Random rnd_;
  81. };
  82. namespace {} // namespace
  83. TEST_F(DBBenchTest, OptionsFile) {
  84. const std::string kOptionsFileName = test_path_ + "/OPTIONS_test";
  85. Options opt;
  86. opt.create_if_missing = true;
  87. opt.max_open_files = 256;
  88. opt.max_background_compactions = 10;
  89. opt.arena_block_size = 8388608;
  90. ASSERT_OK(PersistRocksDBOptions(DBOptions(opt), {"default"},
  91. {ColumnFamilyOptions(opt)}, kOptionsFileName,
  92. Env::Default()));
  93. // override the following options as db_bench will not take these
  94. // options from the options file
  95. opt.wal_dir = wal_path_;
  96. RunDbBench(kOptionsFileName);
  97. VerifyOptions(opt);
  98. }
  99. TEST_F(DBBenchTest, OptionsFileUniversal) {
  100. const std::string kOptionsFileName = test_path_ + "/OPTIONS_test";
  101. Options opt;
  102. opt.compaction_style = kCompactionStyleUniversal;
  103. opt.num_levels = 1;
  104. opt.create_if_missing = true;
  105. opt.max_open_files = 256;
  106. opt.max_background_compactions = 10;
  107. opt.arena_block_size = 8388608;
  108. ASSERT_OK(PersistRocksDBOptions(DBOptions(opt), {"default"},
  109. {ColumnFamilyOptions(opt)}, kOptionsFileName,
  110. Env::Default()));
  111. // override the following options as db_bench will not take these
  112. // options from the options file
  113. opt.wal_dir = wal_path_;
  114. RunDbBench(kOptionsFileName);
  115. VerifyOptions(opt);
  116. }
  117. TEST_F(DBBenchTest, OptionsFileMultiLevelUniversal) {
  118. const std::string kOptionsFileName = test_path_ + "/OPTIONS_test";
  119. Options opt;
  120. opt.compaction_style = kCompactionStyleUniversal;
  121. opt.num_levels = 12;
  122. opt.create_if_missing = true;
  123. opt.max_open_files = 256;
  124. opt.max_background_compactions = 10;
  125. opt.arena_block_size = 8388608;
  126. ASSERT_OK(PersistRocksDBOptions(DBOptions(opt), {"default"},
  127. {ColumnFamilyOptions(opt)}, kOptionsFileName,
  128. Env::Default()));
  129. // override the following options as db_bench will not take these
  130. // options from the options file
  131. opt.wal_dir = wal_path_;
  132. RunDbBench(kOptionsFileName);
  133. VerifyOptions(opt);
  134. }
  135. const std::string options_file_content = R"OPTIONS_FILE(
  136. [Version]
  137. rocksdb_version=4.3.1
  138. options_file_version=1.1
  139. [DBOptions]
  140. wal_bytes_per_sync=1048576
  141. delete_obsolete_files_period_micros=0
  142. WAL_ttl_seconds=0
  143. WAL_size_limit_MB=0
  144. db_write_buffer_size=0
  145. max_subcompactions=1
  146. table_cache_numshardbits=4
  147. max_open_files=-1
  148. max_file_opening_threads=10
  149. max_background_compactions=5
  150. use_fsync=false
  151. use_adaptive_mutex=false
  152. max_total_wal_size=18446744073709551615
  153. compaction_readahead_size=0
  154. new_table_reader_for_compaction_inputs=false
  155. keep_log_file_num=10
  156. skip_stats_update_on_db_open=false
  157. max_manifest_file_size=18446744073709551615
  158. db_log_dir=
  159. skip_log_error_on_recovery=false
  160. writable_file_max_buffer_size=1048576
  161. paranoid_checks=true
  162. is_fd_close_on_exec=true
  163. bytes_per_sync=1048576
  164. enable_thread_tracking=true
  165. recycle_log_file_num=0
  166. create_missing_column_families=false
  167. log_file_time_to_roll=0
  168. max_background_flushes=1
  169. create_if_missing=true
  170. error_if_exists=false
  171. delayed_write_rate=1048576
  172. manifest_preallocation_size=4194304
  173. allow_mmap_reads=false
  174. allow_mmap_writes=false
  175. use_direct_reads=false
  176. use_direct_io_for_flush_and_compaction=false
  177. stats_dump_period_sec=600
  178. allow_fallocate=true
  179. max_log_file_size=83886080
  180. random_access_max_buffer_size=1048576
  181. advise_random_on_open=true
  182. [CFOptions "default"]
  183. compaction_filter_factory=nullptr
  184. table_factory=BlockBasedTable
  185. prefix_extractor=nullptr
  186. comparator=leveldb.BytewiseComparator
  187. compression_per_level=
  188. max_bytes_for_level_base=104857600
  189. bloom_locality=0
  190. target_file_size_base=10485760
  191. memtable_huge_page_size=0
  192. max_successive_merges=1000
  193. max_sequential_skip_in_iterations=8
  194. arena_block_size=52428800
  195. target_file_size_multiplier=1
  196. source_compaction_factor=1
  197. min_write_buffer_number_to_merge=1
  198. max_write_buffer_number=2
  199. write_buffer_size=419430400
  200. max_grandparent_overlap_factor=10
  201. max_bytes_for_level_multiplier=10
  202. memtable_factory=SkipListFactory
  203. compression=kSnappyCompression
  204. min_partial_merge_operands=2
  205. level0_stop_writes_trigger=100
  206. num_levels=1
  207. level0_slowdown_writes_trigger=50
  208. level0_file_num_compaction_trigger=10
  209. expanded_compaction_factor=25
  210. soft_rate_limit=0.000000
  211. max_write_buffer_number_to_maintain=0
  212. max_write_buffer_size_to_maintain=0
  213. verify_checksums_in_compaction=true
  214. merge_operator=nullptr
  215. memtable_prefix_bloom_bits=0
  216. memtable_whole_key_filtering=true
  217. paranoid_file_checks=false
  218. inplace_update_num_locks=10000
  219. optimize_filters_for_hits=false
  220. level_compaction_dynamic_level_bytes=false
  221. inplace_update_support=false
  222. compaction_style=kCompactionStyleUniversal
  223. memtable_prefix_bloom_probes=6
  224. purge_redundant_kvs_while_flush=true
  225. filter_deletes=false
  226. hard_pending_compaction_bytes_limit=0
  227. disable_auto_compactions=false
  228. compaction_measure_io_stats=false
  229. [TableOptions/BlockBasedTable "default"]
  230. format_version=0
  231. skip_table_builder_flush=false
  232. cache_index_and_filter_blocks=false
  233. flush_block_policy_factory=FlushBlockBySizePolicyFactory
  234. hash_index_allow_collision=true
  235. index_type=kBinarySearch
  236. whole_key_filtering=true
  237. checksum=kCRC32c
  238. no_block_cache=false
  239. block_size=32768
  240. block_size_deviation=10
  241. block_restart_interval=16
  242. filter_policy=rocksdb.BuiltinBloomFilter
  243. )OPTIONS_FILE";
  244. TEST_F(DBBenchTest, OptionsFileFromFile) {
  245. const std::string kOptionsFileName = test_path_ + "/OPTIONS_flash";
  246. std::unique_ptr<WritableFile> writable;
  247. ASSERT_OK(Env::Default()->NewWritableFile(kOptionsFileName, &writable,
  248. EnvOptions()));
  249. ASSERT_OK(writable->Append(options_file_content));
  250. ASSERT_OK(writable->Close());
  251. DBOptions db_opt;
  252. std::vector<ColumnFamilyDescriptor> cf_descs;
  253. ASSERT_OK(LoadOptionsFromFile(kOptionsFileName, Env::Default(), &db_opt,
  254. &cf_descs));
  255. Options opt(db_opt, cf_descs[0].options);
  256. opt.create_if_missing = true;
  257. // override the following options as db_bench will not take these
  258. // options from the options file
  259. opt.wal_dir = wal_path_;
  260. RunDbBench(kOptionsFileName);
  261. VerifyOptions(opt);
  262. }
  263. } // namespace ROCKSDB_NAMESPACE
  264. int main(int argc, char** argv) {
  265. ::testing::InitGoogleTest(&argc, argv);
  266. google::ParseCommandLineFlags(&argc, &argv, true);
  267. return RUN_ALL_TESTS();
  268. }
  269. #else
  270. int main(int argc, char** argv) {
  271. printf("Skip db_bench_tool_test as the required library GFLAG is missing.");
  272. }
  273. #endif // #ifdef GFLAGS