test_db_bench_runner.py 5.5 KB

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