test_db_bench_runner.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import os
  6. import unittest
  7. from advisor.db_bench_runner import DBBenchRunner
  8. from advisor.db_log_parser import DataSource, NO_COL_FAMILY
  9. from advisor.db_options_parser import DatabaseOptions
  10. class TestDBBenchRunnerMethods(unittest.TestCase):
  11. def setUp(self):
  12. self.pos_args = [
  13. "./../../db_bench",
  14. "overwrite",
  15. "use_existing_db=true",
  16. "duration=10",
  17. ]
  18. self.bench_runner = DBBenchRunner(self.pos_args)
  19. this_path = os.path.abspath(os.path.dirname(__file__))
  20. options_path = os.path.join(this_path, "input_files/OPTIONS-000005")
  21. self.db_options = DatabaseOptions(options_path)
  22. def test_setup(self):
  23. self.assertEqual(self.bench_runner.db_bench_binary, self.pos_args[0])
  24. self.assertEqual(self.bench_runner.benchmark, self.pos_args[1])
  25. self.assertSetEqual(
  26. set(self.bench_runner.db_bench_args), set(self.pos_args[2:])
  27. )
  28. def test_get_info_log_file_name(self):
  29. log_file_name = DBBenchRunner.get_info_log_file_name(None, "random_path")
  30. self.assertEqual(log_file_name, "LOG")
  31. log_file_name = DBBenchRunner.get_info_log_file_name(
  32. "/dev/shm/", "/tmp/rocksdbtest-155919/dbbench/"
  33. )
  34. self.assertEqual(log_file_name, "tmp_rocksdbtest-155919_dbbench_LOG")
  35. def test_get_opt_args_str(self):
  36. misc_opt_dict = {"bloom_bits": 2, "empty_opt": None, "rate_limiter": 3}
  37. optional_args_str = DBBenchRunner.get_opt_args_str(misc_opt_dict)
  38. self.assertEqual(optional_args_str, " --bloom_bits=2 --rate_limiter=3")
  39. def test_get_log_options(self):
  40. db_path = "/tmp/rocksdb-155919/dbbench"
  41. # when db_log_dir is present in the db_options
  42. update_dict = {
  43. "DBOptions.db_log_dir": {NO_COL_FAMILY: "/dev/shm"},
  44. "DBOptions.stats_dump_period_sec": {NO_COL_FAMILY: "20"},
  45. }
  46. self.db_options.update_options(update_dict)
  47. log_file_prefix, stats_freq = self.bench_runner.get_log_options(
  48. self.db_options, db_path
  49. )
  50. self.assertEqual(log_file_prefix, "/dev/shm/tmp_rocksdb-155919_dbbench_LOG")
  51. self.assertEqual(stats_freq, 20)
  52. update_dict = {
  53. "DBOptions.db_log_dir": {NO_COL_FAMILY: None},
  54. "DBOptions.stats_dump_period_sec": {NO_COL_FAMILY: "30"},
  55. }
  56. self.db_options.update_options(update_dict)
  57. log_file_prefix, stats_freq = self.bench_runner.get_log_options(
  58. self.db_options, db_path
  59. )
  60. self.assertEqual(log_file_prefix, "/tmp/rocksdb-155919/dbbench/LOG")
  61. self.assertEqual(stats_freq, 30)
  62. def test_build_experiment_command(self):
  63. # add some misc_options to db_options
  64. update_dict = {
  65. "bloom_bits": {NO_COL_FAMILY: 2},
  66. "rate_limiter_bytes_per_sec": {NO_COL_FAMILY: 128000000},
  67. }
  68. self.db_options.update_options(update_dict)
  69. db_path = "/dev/shm"
  70. experiment_command = self.bench_runner._build_experiment_command(
  71. self.db_options, db_path
  72. )
  73. opt_args_str = DBBenchRunner.get_opt_args_str(
  74. self.db_options.get_misc_options()
  75. )
  76. opt_args_str += " --options_file=" + self.db_options.generate_options_config(
  77. "12345"
  78. )
  79. for arg in self.pos_args[2:]:
  80. opt_args_str += " --" + arg
  81. expected_command = (
  82. self.pos_args[0]
  83. + " --benchmarks="
  84. + self.pos_args[1]
  85. + " --statistics --perf_level=3 --db="
  86. + db_path
  87. + opt_args_str
  88. )
  89. self.assertEqual(experiment_command, expected_command)
  90. class TestDBBenchRunner(unittest.TestCase):
  91. def setUp(self):
  92. # Note: the db_bench binary should be present in the rocksdb/ directory
  93. self.pos_args = [
  94. "./../../db_bench",
  95. "overwrite",
  96. "use_existing_db=true",
  97. "duration=20",
  98. ]
  99. self.bench_runner = DBBenchRunner(self.pos_args)
  100. this_path = os.path.abspath(os.path.dirname(__file__))
  101. options_path = os.path.join(this_path, "input_files/OPTIONS-000005")
  102. self.db_options = DatabaseOptions(options_path)
  103. def test_experiment_output(self):
  104. update_dict = {"bloom_bits": {NO_COL_FAMILY: 2}}
  105. self.db_options.update_options(update_dict)
  106. db_path = "/dev/shm"
  107. data_sources, throughput = self.bench_runner.run_experiment(
  108. self.db_options, db_path
  109. )
  110. self.assertEqual(
  111. data_sources[DataSource.Type.DB_OPTIONS][0].type, DataSource.Type.DB_OPTIONS
  112. )
  113. self.assertEqual(data_sources[DataSource.Type.LOG][0].type, DataSource.Type.LOG)
  114. self.assertEqual(len(data_sources[DataSource.Type.TIME_SERIES]), 2)
  115. self.assertEqual(
  116. data_sources[DataSource.Type.TIME_SERIES][0].type,
  117. DataSource.Type.TIME_SERIES,
  118. )
  119. self.assertEqual(
  120. data_sources[DataSource.Type.TIME_SERIES][1].type,
  121. DataSource.Type.TIME_SERIES,
  122. )
  123. self.assertEqual(data_sources[DataSource.Type.TIME_SERIES][1].stats_freq_sec, 0)
  124. if __name__ == "__main__":
  125. unittest.main()