trace_analyzer_test.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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) 2012 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. #ifndef GFLAGS
  10. #include <cstdio>
  11. int main() {
  12. fprintf(stderr, "Please install gflags to run trace_analyzer test\n");
  13. return 0;
  14. }
  15. #else
  16. #include <chrono>
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <sstream>
  20. #include <thread>
  21. #include "db/db_test_util.h"
  22. #include "file/line_file_reader.h"
  23. #include "rocksdb/db.h"
  24. #include "rocksdb/env.h"
  25. #include "rocksdb/status.h"
  26. #include "rocksdb/trace_reader_writer.h"
  27. #include "test_util/testharness.h"
  28. #include "test_util/testutil.h"
  29. #include "tools/trace_analyzer_tool.h"
  30. #include "trace_replay/trace_replay.h"
  31. #include "utilities/fault_injection_env.h"
  32. #include "utilities/trace/file_trace_reader_writer.h"
  33. namespace ROCKSDB_NAMESPACE {
  34. namespace {
  35. static const int kMaxArgCount = 100;
  36. static const size_t kArgBufferSize = 100000;
  37. } // namespace
  38. // Note that, the QPS part verification of the analyzing result is not robost
  39. // enough and causes the failure in some rare cases. Disable them temporally and
  40. // wait for future refactor.
  41. // The helper functions for the test
  42. class TraceAnalyzerTest : public testing::Test {
  43. public:
  44. TraceAnalyzerTest() : rnd_(0xFB) {
  45. // test_path_ = test::TmpDir() + "trace_analyzer_test";
  46. test_path_ = test::PerThreadDBPath("trace_analyzer_test");
  47. env_ = ROCKSDB_NAMESPACE::Env::Default();
  48. env_->CreateDir(test_path_).PermitUncheckedError();
  49. dbname_ = test_path_ + "/db";
  50. }
  51. ~TraceAnalyzerTest() override = default;
  52. void GenerateTrace(std::string trace_path) {
  53. Options options;
  54. options.create_if_missing = true;
  55. options.merge_operator = MergeOperators::CreatePutOperator();
  56. Slice upper_bound("a");
  57. Slice lower_bound("abce");
  58. ReadOptions ro;
  59. ro.iterate_upper_bound = &upper_bound;
  60. ro.iterate_lower_bound = &lower_bound;
  61. WriteOptions wo;
  62. TraceOptions trace_opt;
  63. DB* db_ = nullptr;
  64. std::string value;
  65. std::unique_ptr<TraceWriter> trace_writer;
  66. Iterator* single_iter = nullptr;
  67. ASSERT_OK(
  68. NewFileTraceWriter(env_, env_options_, trace_path, &trace_writer));
  69. ASSERT_OK(DB::Open(options, dbname_, &db_));
  70. ASSERT_OK(db_->StartTrace(trace_opt, std::move(trace_writer)));
  71. WriteBatch batch;
  72. ASSERT_OK(batch.Put("a", "aaaaaaaaa"));
  73. ASSERT_OK(batch.Merge("b", "aaaaaaaaaaaaaaaaaaaa"));
  74. ASSERT_OK(batch.Delete("c"));
  75. ASSERT_OK(batch.SingleDelete("d"));
  76. ASSERT_OK(batch.DeleteRange("e", "f"));
  77. ASSERT_OK(db_->Write(wo, &batch));
  78. std::vector<Slice> keys;
  79. keys.emplace_back("a");
  80. keys.emplace_back("b");
  81. keys.emplace_back("df");
  82. keys.emplace_back("gege");
  83. keys.emplace_back("hjhjhj");
  84. std::vector<std::string> values;
  85. std::vector<Status> ss = db_->MultiGet(ro, keys, &values);
  86. ASSERT_GE(ss.size(), 0);
  87. ASSERT_OK(ss[0]);
  88. ASSERT_NOK(ss[2]);
  89. std::vector<ColumnFamilyHandle*> cfs(2, db_->DefaultColumnFamily());
  90. std::vector<PinnableSlice> values2(keys.size());
  91. db_->MultiGet(ro, 2, cfs.data(), keys.data(), values2.data(), ss.data(),
  92. false);
  93. ASSERT_OK(ss[0]);
  94. db_->MultiGet(ro, db_->DefaultColumnFamily(), 2, keys.data() + 3,
  95. values2.data(), ss.data(), false);
  96. ASSERT_OK(db_->Get(ro, "a", &value));
  97. single_iter = db_->NewIterator(ro);
  98. single_iter->Seek("a");
  99. ASSERT_OK(single_iter->status());
  100. single_iter->SeekForPrev("b");
  101. ASSERT_OK(single_iter->status());
  102. delete single_iter;
  103. std::this_thread::sleep_for(std::chrono::seconds(1));
  104. db_->Get(ro, "g", &value).PermitUncheckedError();
  105. ASSERT_OK(db_->EndTrace());
  106. ASSERT_OK(env_->FileExists(trace_path));
  107. std::unique_ptr<WritableFile> whole_f;
  108. std::string whole_path = test_path_ + "/0.txt";
  109. ASSERT_OK(env_->NewWritableFile(whole_path, &whole_f, env_options_));
  110. std::string whole_str = "0x61\n0x62\n0x63\n0x64\n0x65\n0x66\n";
  111. ASSERT_OK(whole_f->Append(whole_str));
  112. delete db_;
  113. ASSERT_OK(DestroyDB(dbname_, options));
  114. }
  115. void RunTraceAnalyzer(const std::vector<std::string>& args) {
  116. char arg_buffer[kArgBufferSize];
  117. char* argv[kMaxArgCount];
  118. int argc = 0;
  119. int cursor = 0;
  120. for (const auto& arg : args) {
  121. ASSERT_LE(cursor + arg.size() + 1, kArgBufferSize);
  122. ASSERT_LE(argc + 1, kMaxArgCount);
  123. snprintf(arg_buffer + cursor, arg.size() + 1, "%s", arg.c_str());
  124. argv[argc++] = arg_buffer + cursor;
  125. cursor += static_cast<int>(arg.size()) + 1;
  126. }
  127. ASSERT_EQ(0, ROCKSDB_NAMESPACE::trace_analyzer_tool(argc, argv));
  128. }
  129. void CheckFileContent(const std::vector<std::string>& cnt,
  130. std::string file_path, bool full_content) {
  131. const auto& fs = env_->GetFileSystem();
  132. FileOptions fopts(env_options_);
  133. ASSERT_OK(fs->FileExists(file_path, fopts.io_options, nullptr));
  134. std::unique_ptr<FSSequentialFile> file;
  135. ASSERT_OK(fs->NewSequentialFile(file_path, fopts, &file, nullptr));
  136. LineFileReader lf_reader(std::move(file), file_path,
  137. 4096 /* filereadahead_size */);
  138. std::vector<std::string> result;
  139. std::string line;
  140. while (
  141. lf_reader.ReadLine(&line, Env::IO_TOTAL /* rate_limiter_priority */)) {
  142. result.push_back(line);
  143. }
  144. ASSERT_OK(lf_reader.GetStatus());
  145. size_t min_size = std::min(cnt.size(), result.size());
  146. for (size_t i = 0; i < min_size; i++) {
  147. if (full_content) {
  148. ASSERT_EQ(result[i], cnt[i]);
  149. } else {
  150. ASSERT_EQ(result[i][0], cnt[i][0]);
  151. }
  152. }
  153. }
  154. void AnalyzeTrace(std::vector<std::string>& paras_diff,
  155. std::string output_path, std::string trace_path) {
  156. std::vector<std::string> paras = {"./trace_analyzer",
  157. "-convert_to_human_readable_trace",
  158. "-output_key_stats",
  159. "-output_access_count_stats",
  160. "-output_prefix=test",
  161. "-output_prefix_cut=1",
  162. "-output_time_series",
  163. "-output_value_distribution",
  164. "-output_qps_stats",
  165. "-no_key",
  166. "-no_print"};
  167. for (auto& para : paras_diff) {
  168. paras.push_back(para);
  169. }
  170. Status s = env_->FileExists(trace_path);
  171. if (!s.ok()) {
  172. GenerateTrace(trace_path);
  173. }
  174. ASSERT_OK(env_->CreateDir(output_path));
  175. RunTraceAnalyzer(paras);
  176. }
  177. ROCKSDB_NAMESPACE::Env* env_;
  178. EnvOptions env_options_;
  179. std::string test_path_;
  180. std::string dbname_;
  181. Random rnd_;
  182. };
  183. TEST_F(TraceAnalyzerTest, Get) {
  184. std::string trace_path = test_path_ + "/trace";
  185. std::string output_path = test_path_ + "/get";
  186. std::string file_path;
  187. std::vector<std::string> paras = {
  188. "-analyze_get=true", "-analyze_put=false",
  189. "-analyze_delete=false", "-analyze_single_delete=false",
  190. "-analyze_range_delete=false", "-analyze_iterator=false",
  191. "-analyze_multiget=false"};
  192. paras.push_back("-output_dir=" + output_path);
  193. paras.push_back("-trace_path=" + trace_path);
  194. paras.push_back("-key_space_dir=" + test_path_);
  195. AnalyzeTrace(paras, output_path, trace_path);
  196. // check the key_stats file
  197. std::vector<std::string> k_stats = {"0 10 0 1 1.000000", "0 10 1 1 1.000000"};
  198. file_path = output_path + "/test-get-0-accessed_key_stats.txt";
  199. CheckFileContent(k_stats, file_path, true);
  200. // Check the access count distribution
  201. std::vector<std::string> k_dist = {"access_count: 1 num: 2"};
  202. file_path = output_path + "/test-get-0-accessed_key_count_distribution.txt";
  203. CheckFileContent(k_dist, file_path, true);
  204. // Check the trace sequence
  205. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  206. "8", "8", "8", "8", "8", "8",
  207. "8", "8", "0", "6", "7", "0"};
  208. file_path = output_path + "/test-human_readable_trace.txt";
  209. CheckFileContent(k_sequence, file_path, false);
  210. // Check the prefix
  211. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30",
  212. "1 1 1 1.000000 1.000000 0x61"};
  213. file_path = output_path + "/test-get-0-accessed_key_prefix_cut.txt";
  214. CheckFileContent(k_prefix, file_path, true);
  215. // Check the time series
  216. std::vector<std::string> k_series = {"0 1533000630 0", "0 1533000630 1"};
  217. file_path = output_path + "/test-get-0-time_series.txt";
  218. CheckFileContent(k_series, file_path, false);
  219. // Check the accessed key in whole key space
  220. std::vector<std::string> k_whole_access = {"0 1"};
  221. file_path = output_path + "/test-get-0-whole_key_stats.txt";
  222. CheckFileContent(k_whole_access, file_path, true);
  223. // Check the whole key prefix cut
  224. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  225. "3 0x64", "4 0x65", "5 0x66"};
  226. file_path = output_path + "/test-get-0-whole_key_prefix_cut.txt";
  227. CheckFileContent(k_whole_prefix, file_path, true);
  228. /*
  229. // Check the overall qps
  230. std::vector<std::string> all_qps = {"1 0 0 0 0 0 0 0 0 1"};
  231. file_path = output_path + "/test-qps_stats.txt";
  232. CheckFileContent(all_qps, file_path, true);
  233. // Check the qps of get
  234. std::vector<std::string> get_qps = {"1"};
  235. file_path = output_path + "/test-get-0-qps_stats.txt";
  236. CheckFileContent(get_qps, file_path, true);
  237. // Check the top k qps prefix cut
  238. std::vector<std::string> top_qps = {"At time: 0 with QPS: 1",
  239. "The prefix: 0x61 Access count: 1"};
  240. file_path = output_path + "/test-get-0-accessed_top_k_qps_prefix_cut.txt";
  241. CheckFileContent(top_qps, file_path, true);
  242. */
  243. }
  244. // Test analyzing of Put
  245. TEST_F(TraceAnalyzerTest, Put) {
  246. std::string trace_path = test_path_ + "/trace";
  247. std::string output_path = test_path_ + "/put";
  248. std::string file_path;
  249. std::vector<std::string> paras = {
  250. "-analyze_get=false", "-analyze_put=true",
  251. "-analyze_delete=false", "-analyze_single_delete=false",
  252. "-analyze_range_delete=false", "-analyze_iterator=false",
  253. "-analyze_multiget=false"};
  254. paras.push_back("-output_dir=" + output_path);
  255. paras.push_back("-trace_path=" + trace_path);
  256. paras.push_back("-key_space_dir=" + test_path_);
  257. AnalyzeTrace(paras, output_path, trace_path);
  258. // check the key_stats file
  259. std::vector<std::string> k_stats = {"0 9 0 1 1.000000"};
  260. file_path = output_path + "/test-put-0-accessed_key_stats.txt";
  261. CheckFileContent(k_stats, file_path, true);
  262. // Check the access count distribution
  263. std::vector<std::string> k_dist = {"access_count: 1 num: 1"};
  264. file_path = output_path + "/test-put-0-accessed_key_count_distribution.txt";
  265. CheckFileContent(k_dist, file_path, true);
  266. // Check the trace sequence
  267. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  268. "8", "8", "8", "8", "8", "8",
  269. "8", "8", "0", "6", "7", "0"};
  270. file_path = output_path + "/test-human_readable_trace.txt";
  271. CheckFileContent(k_sequence, file_path, false);
  272. // Check the prefix
  273. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30"};
  274. file_path = output_path + "/test-put-0-accessed_key_prefix_cut.txt";
  275. CheckFileContent(k_prefix, file_path, true);
  276. // Check the time series
  277. std::vector<std::string> k_series = {"1 1533056278 0"};
  278. file_path = output_path + "/test-put-0-time_series.txt";
  279. CheckFileContent(k_series, file_path, false);
  280. // Check the accessed key in whole key space
  281. std::vector<std::string> k_whole_access = {"0 1"};
  282. file_path = output_path + "/test-put-0-whole_key_stats.txt";
  283. CheckFileContent(k_whole_access, file_path, true);
  284. // Check the whole key prefix cut
  285. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  286. "3 0x64", "4 0x65", "5 0x66"};
  287. file_path = output_path + "/test-put-0-whole_key_prefix_cut.txt";
  288. CheckFileContent(k_whole_prefix, file_path, true);
  289. // Check the overall qps
  290. std::vector<std::string> all_qps = {"0 1 0 0 0 0 0 0 0 0 1"};
  291. file_path = output_path + "/test-qps_stats.txt";
  292. CheckFileContent(all_qps, file_path, true);
  293. /*
  294. // Check the qps of Put
  295. std::vector<std::string> get_qps = {"1"};
  296. file_path = output_path + "/test-put-0-qps_stats.txt";
  297. CheckFileContent(get_qps, file_path, true);
  298. // Check the top k qps prefix cut
  299. std::vector<std::string> top_qps = {"At time: 0 with QPS: 1",
  300. "The prefix: 0x61 Access count: 1"};
  301. file_path = output_path + "/test-put-0-accessed_top_k_qps_prefix_cut.txt";
  302. CheckFileContent(top_qps, file_path, true);
  303. // Check the value size distribution
  304. std::vector<std::string> value_dist = {
  305. "Number_of_value_size_between 0 and 16 is: 1"};
  306. file_path = output_path + "/test-put-0-accessed_value_size_distribution.txt";
  307. CheckFileContent(value_dist, file_path, true);
  308. */
  309. }
  310. // Test analyzing of delete
  311. TEST_F(TraceAnalyzerTest, Delete) {
  312. std::string trace_path = test_path_ + "/trace";
  313. std::string output_path = test_path_ + "/delete";
  314. std::string file_path;
  315. std::vector<std::string> paras = {
  316. "-analyze_get=false", "-analyze_put=false",
  317. "-analyze_delete=true", "-analyze_single_delete=false",
  318. "-analyze_range_delete=false", "-analyze_iterator=false",
  319. "-analyze_multiget=false"};
  320. paras.push_back("-output_dir=" + output_path);
  321. paras.push_back("-trace_path=" + trace_path);
  322. paras.push_back("-key_space_dir=" + test_path_);
  323. AnalyzeTrace(paras, output_path, trace_path);
  324. // check the key_stats file
  325. std::vector<std::string> k_stats = {"0 10 0 1 1.000000"};
  326. file_path = output_path + "/test-delete-0-accessed_key_stats.txt";
  327. CheckFileContent(k_stats, file_path, true);
  328. // Check the access count distribution
  329. std::vector<std::string> k_dist = {"access_count: 1 num: 1"};
  330. file_path =
  331. output_path + "/test-delete-0-accessed_key_count_distribution.txt";
  332. CheckFileContent(k_dist, file_path, true);
  333. // Check the trace sequence
  334. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  335. "8", "8", "8", "8", "8", "8",
  336. "8", "8", "0", "6", "7", "0"};
  337. file_path = output_path + "/test-human_readable_trace.txt";
  338. CheckFileContent(k_sequence, file_path, false);
  339. // Check the prefix
  340. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30"};
  341. file_path = output_path + "/test-delete-0-accessed_key_prefix_cut.txt";
  342. CheckFileContent(k_prefix, file_path, true);
  343. // Check the time series
  344. std::vector<std::string> k_series = {"2 1533000630 0"};
  345. file_path = output_path + "/test-delete-0-time_series.txt";
  346. CheckFileContent(k_series, file_path, false);
  347. // Check the accessed key in whole key space
  348. std::vector<std::string> k_whole_access = {"2 1"};
  349. file_path = output_path + "/test-delete-0-whole_key_stats.txt";
  350. CheckFileContent(k_whole_access, file_path, true);
  351. // Check the whole key prefix cut
  352. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  353. "3 0x64", "4 0x65", "5 0x66"};
  354. file_path = output_path + "/test-delete-0-whole_key_prefix_cut.txt";
  355. CheckFileContent(k_whole_prefix, file_path, true);
  356. /*
  357. // Check the overall qps
  358. std::vector<std::string> all_qps = {"0 0 1 0 0 0 0 0 0 1"};
  359. file_path = output_path + "/test-qps_stats.txt";
  360. CheckFileContent(all_qps, file_path, true);
  361. // Check the qps of Delete
  362. std::vector<std::string> get_qps = {"1"};
  363. file_path = output_path + "/test-delete-0-qps_stats.txt";
  364. CheckFileContent(get_qps, file_path, true);
  365. // Check the top k qps prefix cut
  366. std::vector<std::string> top_qps = {"At time: 0 with QPS: 1",
  367. "The prefix: 0x63 Access count: 1"};
  368. file_path = output_path + "/test-delete-0-accessed_top_k_qps_prefix_cut.txt";
  369. CheckFileContent(top_qps, file_path, true);
  370. */
  371. }
  372. // Test analyzing of Merge
  373. TEST_F(TraceAnalyzerTest, Merge) {
  374. std::string trace_path = test_path_ + "/trace";
  375. std::string output_path = test_path_ + "/merge";
  376. std::string file_path;
  377. std::vector<std::string> paras = {
  378. "-analyze_get=false", "-analyze_put=false",
  379. "-analyze_delete=false", "-analyze_merge=true",
  380. "-analyze_single_delete=false", "-analyze_range_delete=false",
  381. "-analyze_iterator=false", "-analyze_multiget=false"};
  382. paras.push_back("-output_dir=" + output_path);
  383. paras.push_back("-trace_path=" + trace_path);
  384. paras.push_back("-key_space_dir=" + test_path_);
  385. AnalyzeTrace(paras, output_path, trace_path);
  386. // check the key_stats file
  387. std::vector<std::string> k_stats = {"0 20 0 1 1.000000"};
  388. file_path = output_path + "/test-merge-0-accessed_key_stats.txt";
  389. CheckFileContent(k_stats, file_path, true);
  390. // Check the access count distribution
  391. std::vector<std::string> k_dist = {"access_count: 1 num: 1"};
  392. file_path = output_path + "/test-merge-0-accessed_key_count_distribution.txt";
  393. CheckFileContent(k_dist, file_path, true);
  394. // Check the trace sequence
  395. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  396. "8", "8", "8", "8", "8", "8",
  397. "8", "8", "0", "6", "7", "0"};
  398. file_path = output_path + "/test-human_readable_trace.txt";
  399. CheckFileContent(k_sequence, file_path, false);
  400. // Check the prefix
  401. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30"};
  402. file_path = output_path + "/test-merge-0-accessed_key_prefix_cut.txt";
  403. CheckFileContent(k_prefix, file_path, true);
  404. // Check the time series
  405. std::vector<std::string> k_series = {"5 1533000630 0"};
  406. file_path = output_path + "/test-merge-0-time_series.txt";
  407. CheckFileContent(k_series, file_path, false);
  408. // Check the accessed key in whole key space
  409. std::vector<std::string> k_whole_access = {"1 1"};
  410. file_path = output_path + "/test-merge-0-whole_key_stats.txt";
  411. CheckFileContent(k_whole_access, file_path, true);
  412. // Check the whole key prefix cut
  413. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  414. "3 0x64", "4 0x65", "5 0x66"};
  415. file_path = output_path + "/test-merge-0-whole_key_prefix_cut.txt";
  416. CheckFileContent(k_whole_prefix, file_path, true);
  417. /*
  418. // Check the overall qps
  419. std::vector<std::string> all_qps = {"0 0 0 0 0 1 0 0 0 1"};
  420. file_path = output_path + "/test-qps_stats.txt";
  421. CheckFileContent(all_qps, file_path, true);
  422. // Check the qps of Merge
  423. std::vector<std::string> get_qps = {"1"};
  424. file_path = output_path + "/test-merge-0-qps_stats.txt";
  425. CheckFileContent(get_qps, file_path, true);
  426. // Check the top k qps prefix cut
  427. std::vector<std::string> top_qps = {"At time: 0 with QPS: 1",
  428. "The prefix: 0x62 Access count: 1"};
  429. file_path = output_path + "/test-merge-0-accessed_top_k_qps_prefix_cut.txt";
  430. CheckFileContent(top_qps, file_path, true);
  431. */
  432. // Check the value size distribution
  433. std::vector<std::string> value_dist = {
  434. "Number_of_value_size_between 0 and 24 is: 1"};
  435. file_path =
  436. output_path + "/test-merge-0-accessed_value_size_distribution.txt";
  437. CheckFileContent(value_dist, file_path, true);
  438. }
  439. // Test analyzing of SingleDelete
  440. TEST_F(TraceAnalyzerTest, SingleDelete) {
  441. std::string trace_path = test_path_ + "/trace";
  442. std::string output_path = test_path_ + "/single_delete";
  443. std::string file_path;
  444. std::vector<std::string> paras = {
  445. "-analyze_get=false", "-analyze_put=false",
  446. "-analyze_delete=false", "-analyze_merge=false",
  447. "-analyze_single_delete=true", "-analyze_range_delete=false",
  448. "-analyze_iterator=false", "-analyze_multiget=false"};
  449. paras.push_back("-output_dir=" + output_path);
  450. paras.push_back("-trace_path=" + trace_path);
  451. paras.push_back("-key_space_dir=" + test_path_);
  452. AnalyzeTrace(paras, output_path, trace_path);
  453. // check the key_stats file
  454. std::vector<std::string> k_stats = {"0 10 0 1 1.000000"};
  455. file_path = output_path + "/test-single_delete-0-accessed_key_stats.txt";
  456. CheckFileContent(k_stats, file_path, true);
  457. // Check the access count distribution
  458. std::vector<std::string> k_dist = {"access_count: 1 num: 1"};
  459. file_path =
  460. output_path + "/test-single_delete-0-accessed_key_count_distribution.txt";
  461. CheckFileContent(k_dist, file_path, true);
  462. // Check the trace sequence
  463. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  464. "8", "8", "8", "8", "8", "8",
  465. "8", "8", "0", "6", "7", "0"};
  466. file_path = output_path + "/test-human_readable_trace.txt";
  467. CheckFileContent(k_sequence, file_path, false);
  468. // Check the prefix
  469. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30"};
  470. file_path = output_path + "/test-single_delete-0-accessed_key_prefix_cut.txt";
  471. CheckFileContent(k_prefix, file_path, true);
  472. // Check the time series
  473. std::vector<std::string> k_series = {"3 1533000630 0"};
  474. file_path = output_path + "/test-single_delete-0-time_series.txt";
  475. CheckFileContent(k_series, file_path, false);
  476. // Check the accessed key in whole key space
  477. std::vector<std::string> k_whole_access = {"3 1"};
  478. file_path = output_path + "/test-single_delete-0-whole_key_stats.txt";
  479. CheckFileContent(k_whole_access, file_path, true);
  480. // Check the whole key prefix cut
  481. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  482. "3 0x64", "4 0x65", "5 0x66"};
  483. file_path = output_path + "/test-single_delete-0-whole_key_prefix_cut.txt";
  484. CheckFileContent(k_whole_prefix, file_path, true);
  485. /*
  486. // Check the overall qps
  487. std::vector<std::string> all_qps = {"0 0 0 1 0 0 0 0 0 1"};
  488. file_path = output_path + "/test-qps_stats.txt";
  489. CheckFileContent(all_qps, file_path, true);
  490. // Check the qps of SingleDelete
  491. std::vector<std::string> get_qps = {"1"};
  492. file_path = output_path + "/test-single_delete-0-qps_stats.txt";
  493. CheckFileContent(get_qps, file_path, true);
  494. // Check the top k qps prefix cut
  495. std::vector<std::string> top_qps = {"At time: 0 with QPS: 1",
  496. "The prefix: 0x64 Access count: 1"};
  497. file_path =
  498. output_path + "/test-single_delete-0-accessed_top_k_qps_prefix_cut.txt";
  499. CheckFileContent(top_qps, file_path, true);
  500. */
  501. }
  502. // Test analyzing of delete
  503. TEST_F(TraceAnalyzerTest, DeleteRange) {
  504. std::string trace_path = test_path_ + "/trace";
  505. std::string output_path = test_path_ + "/range_delete";
  506. std::string file_path;
  507. std::vector<std::string> paras = {
  508. "-analyze_get=false", "-analyze_put=false",
  509. "-analyze_delete=false", "-analyze_merge=false",
  510. "-analyze_single_delete=false", "-analyze_range_delete=true",
  511. "-analyze_iterator=false", "-analyze_multiget=false"};
  512. paras.push_back("-output_dir=" + output_path);
  513. paras.push_back("-trace_path=" + trace_path);
  514. paras.push_back("-key_space_dir=" + test_path_);
  515. AnalyzeTrace(paras, output_path, trace_path);
  516. // check the key_stats file
  517. std::vector<std::string> k_stats = {"0 10 0 1 1.000000", "0 10 1 1 1.000000"};
  518. file_path = output_path + "/test-range_delete-0-accessed_key_stats.txt";
  519. CheckFileContent(k_stats, file_path, true);
  520. // Check the access count distribution
  521. std::vector<std::string> k_dist = {"access_count: 1 num: 2"};
  522. file_path =
  523. output_path + "/test-range_delete-0-accessed_key_count_distribution.txt";
  524. CheckFileContent(k_dist, file_path, true);
  525. // Check the trace sequence
  526. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  527. "8", "8", "8", "8", "8", "8",
  528. "8", "8", "0", "6", "7", "0"};
  529. file_path = output_path + "/test-human_readable_trace.txt";
  530. CheckFileContent(k_sequence, file_path, false);
  531. // Check the prefix
  532. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30",
  533. "1 1 1 1.000000 1.000000 0x65"};
  534. file_path = output_path + "/test-range_delete-0-accessed_key_prefix_cut.txt";
  535. CheckFileContent(k_prefix, file_path, true);
  536. // Check the time series
  537. std::vector<std::string> k_series = {"4 1533000630 0", "4 1533060100 1"};
  538. file_path = output_path + "/test-range_delete-0-time_series.txt";
  539. CheckFileContent(k_series, file_path, false);
  540. // Check the accessed key in whole key space
  541. std::vector<std::string> k_whole_access = {"4 1", "5 1"};
  542. file_path = output_path + "/test-range_delete-0-whole_key_stats.txt";
  543. CheckFileContent(k_whole_access, file_path, true);
  544. // Check the whole key prefix cut
  545. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  546. "3 0x64", "4 0x65", "5 0x66"};
  547. file_path = output_path + "/test-range_delete-0-whole_key_prefix_cut.txt";
  548. CheckFileContent(k_whole_prefix, file_path, true);
  549. /*
  550. // Check the overall qps
  551. std::vector<std::string> all_qps = {"0 0 0 0 2 0 0 0 0 2"};
  552. file_path = output_path + "/test-qps_stats.txt";
  553. CheckFileContent(all_qps, file_path, true);
  554. // Check the qps of DeleteRange
  555. std::vector<std::string> get_qps = {"2"};
  556. file_path = output_path + "/test-range_delete-0-qps_stats.txt";
  557. CheckFileContent(get_qps, file_path, true);
  558. // Check the top k qps prefix cut
  559. std::vector<std::string> top_qps = {"At time: 0 with QPS: 2",
  560. "The prefix: 0x65 Access count: 1",
  561. "The prefix: 0x66 Access count: 1"};
  562. file_path =
  563. output_path + "/test-range_delete-0-accessed_top_k_qps_prefix_cut.txt";
  564. CheckFileContent(top_qps, file_path, true);
  565. */
  566. }
  567. // Test analyzing of Iterator
  568. TEST_F(TraceAnalyzerTest, Iterator) {
  569. std::string trace_path = test_path_ + "/trace";
  570. std::string output_path = test_path_ + "/iterator";
  571. std::string file_path;
  572. std::vector<std::string> paras = {
  573. "-analyze_get=false", "-analyze_put=false",
  574. "-analyze_delete=false", "-analyze_merge=false",
  575. "-analyze_single_delete=false", "-analyze_range_delete=false",
  576. "-analyze_iterator=true", "-analyze_multiget=false"};
  577. paras.push_back("-output_dir=" + output_path);
  578. paras.push_back("-trace_path=" + trace_path);
  579. paras.push_back("-key_space_dir=" + test_path_);
  580. AnalyzeTrace(paras, output_path, trace_path);
  581. // Check the output of Seek
  582. // check the key_stats file
  583. std::vector<std::string> k_stats = {"0 10 0 1 1.000000"};
  584. file_path = output_path + "/test-iterator_Seek-0-accessed_key_stats.txt";
  585. CheckFileContent(k_stats, file_path, true);
  586. // Check the access count distribution
  587. std::vector<std::string> k_dist = {"access_count: 1 num: 1"};
  588. file_path =
  589. output_path + "/test-iterator_Seek-0-accessed_key_count_distribution.txt";
  590. CheckFileContent(k_dist, file_path, true);
  591. // Check the trace sequence
  592. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  593. "8", "8", "8", "8", "8", "8",
  594. "8", "8", "0", "6", "7", "0"};
  595. file_path = output_path + "/test-human_readable_trace.txt";
  596. CheckFileContent(k_sequence, file_path, false);
  597. // Check the prefix
  598. std::vector<std::string> k_prefix = {"0 0 0 0.000000 0.000000 0x30"};
  599. file_path = output_path + "/test-iterator_Seek-0-accessed_key_prefix_cut.txt";
  600. CheckFileContent(k_prefix, file_path, true);
  601. // Check the time series
  602. std::vector<std::string> k_series = {"6 1 0"};
  603. file_path = output_path + "/test-iterator_Seek-0-time_series.txt";
  604. CheckFileContent(k_series, file_path, false);
  605. // Check the accessed key in whole key space
  606. std::vector<std::string> k_whole_access = {"0 1"};
  607. file_path = output_path + "/test-iterator_Seek-0-whole_key_stats.txt";
  608. CheckFileContent(k_whole_access, file_path, true);
  609. // Check the whole key prefix cut
  610. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  611. "3 0x64", "4 0x65", "5 0x66"};
  612. file_path = output_path + "/test-iterator_Seek-0-whole_key_prefix_cut.txt";
  613. CheckFileContent(k_whole_prefix, file_path, true);
  614. /*
  615. // Check the overall qps
  616. std::vector<std::string> all_qps = {"0 0 0 0 0 0 1 1 0 2"};
  617. file_path = output_path + "/test-qps_stats.txt";
  618. CheckFileContent(all_qps, file_path, true);
  619. // Check the qps of Iterator_Seek
  620. std::vector<std::string> get_qps = {"1"};
  621. file_path = output_path + "/test-iterator_Seek-0-qps_stats.txt";
  622. CheckFileContent(get_qps, file_path, true);
  623. // Check the top k qps prefix cut
  624. std::vector<std::string> top_qps = {"At time: 0 with QPS: 1",
  625. "The prefix: 0x61 Access count: 1"};
  626. file_path =
  627. output_path + "/test-iterator_Seek-0-accessed_top_k_qps_prefix_cut.txt";
  628. CheckFileContent(top_qps, file_path, true);
  629. */
  630. // Check the output of SeekForPrev
  631. // check the key_stats file
  632. k_stats = {"0 10 0 1 1.000000"};
  633. file_path =
  634. output_path + "/test-iterator_SeekForPrev-0-accessed_key_stats.txt";
  635. CheckFileContent(k_stats, file_path, true);
  636. // Check the access count distribution
  637. k_dist = {"access_count: 1 num: 1"};
  638. file_path =
  639. output_path +
  640. "/test-iterator_SeekForPrev-0-accessed_key_count_distribution.txt";
  641. CheckFileContent(k_dist, file_path, true);
  642. // Check the prefix
  643. k_prefix = {"0 0 0 0.000000 0.000000 0x30"};
  644. file_path =
  645. output_path + "/test-iterator_SeekForPrev-0-accessed_key_prefix_cut.txt";
  646. CheckFileContent(k_prefix, file_path, true);
  647. // Check the time series
  648. k_series = {"7 0 0"};
  649. file_path = output_path + "/test-iterator_SeekForPrev-0-time_series.txt";
  650. CheckFileContent(k_series, file_path, false);
  651. // Check the accessed key in whole key space
  652. k_whole_access = {"1 1"};
  653. file_path = output_path + "/test-iterator_SeekForPrev-0-whole_key_stats.txt";
  654. CheckFileContent(k_whole_access, file_path, true);
  655. // Check the whole key prefix cut
  656. k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63", "3 0x64", "4 0x65", "5 0x66"};
  657. file_path =
  658. output_path + "/test-iterator_SeekForPrev-0-whole_key_prefix_cut.txt";
  659. CheckFileContent(k_whole_prefix, file_path, true);
  660. /*
  661. // Check the qps of Iterator_SeekForPrev
  662. get_qps = {"1"};
  663. file_path = output_path + "/test-iterator_SeekForPrev-0-qps_stats.txt";
  664. CheckFileContent(get_qps, file_path, true);
  665. // Check the top k qps prefix cut
  666. top_qps = {"At time: 0 with QPS: 1", "The prefix: 0x62 Access count: 1"};
  667. file_path = output_path +
  668. "/test-iterator_SeekForPrev-0-accessed_top_k_qps_prefix_cut.txt";
  669. CheckFileContent(top_qps, file_path, true);
  670. */
  671. }
  672. TEST_F(TraceAnalyzerTest, ExistsPreviousTraceWriteError) {
  673. DB* db_ = nullptr;
  674. Options options;
  675. options.create_if_missing = true;
  676. std::unique_ptr<FaultInjectionTestEnv> fault_env(
  677. new FaultInjectionTestEnv(env_));
  678. const std::string trace_path =
  679. test_path_ + "/previous_trace_write_error_trace";
  680. std::unique_ptr<TraceWriter> trace_writer;
  681. ASSERT_OK(NewFileTraceWriter(fault_env.get(), env_options_, trace_path,
  682. &trace_writer));
  683. ASSERT_OK(DB::Open(options, dbname_, &db_));
  684. ASSERT_OK(db_->StartTrace(TraceOptions(), std::move(trace_writer)));
  685. // Inject write error on the first trace write.
  686. // This trace write is made big enough to actually write to FS for error
  687. // injection.
  688. const std::string kBigKey(1000000, 'k');
  689. const std::string kBigValue(1000000, 'v');
  690. fault_env->SetFilesystemActive(false, Status::IOError("Injected"));
  691. ASSERT_OK(db_->Put(WriteOptions(), kBigKey, kBigValue));
  692. fault_env->SetFilesystemActive(true);
  693. // Without proper handling of the previous trace write error,
  694. // this trace write will continue and crash the db (in DEBUG_LEVEL > 0)
  695. // due to writing to the trace file that has seen error.
  696. ASSERT_OK(db_->Put(WriteOptions(), kBigKey, kBigValue));
  697. // Verify `EndTrace()` returns the previous write trace error if any
  698. Status s = db_->EndTrace();
  699. ASSERT_TRUE(s.IsIncomplete());
  700. ASSERT_TRUE(s.ToString().find("Tracing has seen error") != std::string::npos);
  701. ASSERT_TRUE(s.ToString().find("Injected") != std::string::npos);
  702. delete db_;
  703. ASSERT_OK(DestroyDB(dbname_, options));
  704. }
  705. // Test analyzing of multiget
  706. TEST_F(TraceAnalyzerTest, MultiGet) {
  707. std::string trace_path = test_path_ + "/trace";
  708. std::string output_path = test_path_ + "/multiget";
  709. std::string file_path;
  710. std::vector<std::string> paras = {
  711. "-analyze_get=false", "-analyze_put=false",
  712. "-analyze_delete=false", "-analyze_merge=false",
  713. "-analyze_single_delete=false", "-analyze_range_delete=true",
  714. "-analyze_iterator=false", "-analyze_multiget=true"};
  715. paras.push_back("-output_dir=" + output_path);
  716. paras.push_back("-trace_path=" + trace_path);
  717. paras.push_back("-key_space_dir=" + test_path_);
  718. AnalyzeTrace(paras, output_path, trace_path);
  719. // check the key_stats file
  720. std::vector<std::string> k_stats = {"0 10 0 2 1.000000", "0 10 1 2 1.000000",
  721. "0 10 2 1 1.000000", "0 10 3 2 1.000000",
  722. "0 10 4 2 1.000000"};
  723. file_path = output_path + "/test-multiget-0-accessed_key_stats.txt";
  724. CheckFileContent(k_stats, file_path, true);
  725. // Check the access count distribution
  726. std::vector<std::string> k_dist = {"access_count: 1 num: 1",
  727. "access_count: 2 num: 4"};
  728. file_path =
  729. output_path + "/test-multiget-0-accessed_key_count_distribution.txt";
  730. CheckFileContent(k_dist, file_path, true);
  731. // Check the trace sequence
  732. std::vector<std::string> k_sequence = {"1", "5", "2", "3", "4", "8",
  733. "8", "8", "8", "8", "8", "8",
  734. "8", "8", "0", "6", "7", "0"};
  735. file_path = output_path + "/test-human_readable_trace.txt";
  736. CheckFileContent(k_sequence, file_path, false);
  737. // Check the prefix
  738. std::vector<std::string> k_prefix = {
  739. "0 0 0 0.000000 0.000000 0x30", "1 2 1 2.000000 1.000000 0x61",
  740. "2 2 1 2.000000 1.000000 0x62", "3 1 1 1.000000 1.000000 0x64",
  741. "4 2 1 2.000000 1.000000 0x67"};
  742. file_path = output_path + "/test-multiget-0-accessed_key_prefix_cut.txt";
  743. CheckFileContent(k_prefix, file_path, true);
  744. // Check the time series
  745. std::vector<std::string> k_series = {"8 0 0", "8 0 1", "8 0 2",
  746. "8 0 3", "8 0 4", "8 0 0",
  747. "8 0 1", "8 0 3", "8 0 4"};
  748. file_path = output_path + "/test-multiget-0-time_series.txt";
  749. CheckFileContent(k_series, file_path, false);
  750. // Check the accessed key in whole key space
  751. std::vector<std::string> k_whole_access = {"0 2", "1 2"};
  752. file_path = output_path + "/test-multiget-0-whole_key_stats.txt";
  753. CheckFileContent(k_whole_access, file_path, true);
  754. // Check the whole key prefix cut
  755. std::vector<std::string> k_whole_prefix = {"0 0x61", "1 0x62", "2 0x63",
  756. "3 0x64", "4 0x65", "5 0x66"};
  757. file_path = output_path + "/test-multiget-0-whole_key_prefix_cut.txt";
  758. CheckFileContent(k_whole_prefix, file_path, true);
  759. /*
  760. // Check the overall qps. We have 3 MultiGet queries and it requested 9 keys
  761. // in total
  762. std::vector<std::string> all_qps = {"0 0 0 0 2 0 0 0 9 11"};
  763. file_path = output_path + "/test-qps_stats.txt";
  764. CheckFileContent(all_qps, file_path, true);
  765. // Check the qps of DeleteRange
  766. std::vector<std::string> get_qps = {"9"};
  767. file_path = output_path + "/test-multiget-0-qps_stats.txt";
  768. CheckFileContent(get_qps, file_path, true);
  769. // Check the top k qps prefix cut
  770. std::vector<std::string> top_qps = {
  771. "At time: 0 with QPS: 9", "The prefix: 0x61 Access count: 2",
  772. "The prefix: 0x62 Access count: 2", "The prefix: 0x64 Access count: 1",
  773. "The prefix: 0x67 Access count: 2", "The prefix: 0x68 Access count: 2"};
  774. file_path =
  775. output_path + "/test-multiget-0-accessed_top_k_qps_prefix_cut.txt";
  776. CheckFileContent(top_qps, file_path, true);
  777. */
  778. }
  779. } // namespace ROCKSDB_NAMESPACE
  780. int main(int argc, char** argv) {
  781. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  782. ::testing::InitGoogleTest(&argc, argv);
  783. return RUN_ALL_TESTS();
  784. }
  785. #endif // GFLAG