test_db_stats_fetcher.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 time
  7. import unittest
  8. from unittest.mock import MagicMock
  9. from advisor.db_stats_fetcher import DatabasePerfContext, LogStatsParser
  10. from advisor.db_timeseries_parser import NO_ENTITY
  11. from advisor.rule_parser import Condition, TimeSeriesCondition
  12. class TestLogStatsParser(unittest.TestCase):
  13. def setUp(self):
  14. this_path = os.path.abspath(os.path.dirname(__file__))
  15. stats_file = os.path.join(this_path, "input_files/log_stats_parser_keys_ts")
  16. # populate the keys_ts dictionary of LogStatsParser
  17. self.stats_dict = {NO_ENTITY: {}}
  18. with open(stats_file) as fp:
  19. for line in fp:
  20. stat_name = line.split(":")[0].strip()
  21. self.stats_dict[NO_ENTITY][stat_name] = {}
  22. token_list = line.split(":")[1].strip().split(",")
  23. for token in token_list:
  24. timestamp = int(token.split()[0])
  25. value = float(token.split()[1])
  26. self.stats_dict[NO_ENTITY][stat_name][timestamp] = value
  27. self.log_stats_parser = LogStatsParser("dummy_log_file", 20)
  28. self.log_stats_parser.keys_ts = self.stats_dict
  29. def test_check_and_trigger_conditions_bursty(self):
  30. # mock fetch_timeseries() because 'keys_ts' has been pre-populated
  31. self.log_stats_parser.fetch_timeseries = MagicMock()
  32. # condition: bursty
  33. cond1 = Condition("cond-1")
  34. cond1 = TimeSeriesCondition.create(cond1)
  35. cond1.set_parameter("keys", "rocksdb.db.get.micros.p50")
  36. cond1.set_parameter("behavior", "bursty")
  37. cond1.set_parameter("window_sec", 40)
  38. cond1.set_parameter("rate_threshold", 0)
  39. self.log_stats_parser.check_and_trigger_conditions([cond1])
  40. expected_cond_trigger = {NO_ENTITY: {1530896440: 0.9767546362322214}}
  41. self.assertDictEqual(expected_cond_trigger, cond1.get_trigger())
  42. # ensure that fetch_timeseries() was called once
  43. self.log_stats_parser.fetch_timeseries.assert_called_once()
  44. def test_check_and_trigger_conditions_eval_agg(self):
  45. # mock fetch_timeseries() because 'keys_ts' has been pre-populated
  46. self.log_stats_parser.fetch_timeseries = MagicMock()
  47. # condition: evaluate_expression
  48. cond1 = Condition("cond-1")
  49. cond1 = TimeSeriesCondition.create(cond1)
  50. cond1.set_parameter("keys", "rocksdb.db.get.micros.p50")
  51. cond1.set_parameter("behavior", "evaluate_expression")
  52. keys = ["rocksdb.manifest.file.sync.micros.p99", "rocksdb.db.get.micros.p50"]
  53. cond1.set_parameter("keys", keys)
  54. cond1.set_parameter("aggregation_op", "latest")
  55. # condition evaluates to FALSE
  56. cond1.set_parameter("evaluate", "keys[0]-(keys[1]*100)>200")
  57. self.log_stats_parser.check_and_trigger_conditions([cond1])
  58. expected_cond_trigger = {NO_ENTITY: [1792.0, 15.9638]}
  59. self.assertIsNone(cond1.get_trigger())
  60. # condition evaluates to TRUE
  61. cond1.set_parameter("evaluate", "keys[0]-(keys[1]*100)<200")
  62. self.log_stats_parser.check_and_trigger_conditions([cond1])
  63. expected_cond_trigger = {NO_ENTITY: [1792.0, 15.9638]}
  64. self.assertDictEqual(expected_cond_trigger, cond1.get_trigger())
  65. # ensure that fetch_timeseries() was called
  66. self.log_stats_parser.fetch_timeseries.assert_called()
  67. def test_check_and_trigger_conditions_eval(self):
  68. # mock fetch_timeseries() because 'keys_ts' has been pre-populated
  69. self.log_stats_parser.fetch_timeseries = MagicMock()
  70. # condition: evaluate_expression
  71. cond1 = Condition("cond-1")
  72. cond1 = TimeSeriesCondition.create(cond1)
  73. cond1.set_parameter("keys", "rocksdb.db.get.micros.p50")
  74. cond1.set_parameter("behavior", "evaluate_expression")
  75. keys = ["rocksdb.manifest.file.sync.micros.p99", "rocksdb.db.get.micros.p50"]
  76. cond1.set_parameter("keys", keys)
  77. cond1.set_parameter("evaluate", "keys[0]-(keys[1]*100)>500")
  78. self.log_stats_parser.check_and_trigger_conditions([cond1])
  79. expected_trigger = {
  80. NO_ENTITY: {
  81. 1530896414: [9938.0, 16.31508],
  82. 1530896440: [9938.0, 16.346602],
  83. 1530896466: [9938.0, 16.284669],
  84. 1530896492: [9938.0, 16.16005],
  85. }
  86. }
  87. self.assertDictEqual(expected_trigger, cond1.get_trigger())
  88. self.log_stats_parser.fetch_timeseries.assert_called_once()
  89. class TestDatabasePerfContext(unittest.TestCase):
  90. def test_unaccumulate_metrics(self):
  91. perf_dict = {
  92. "user_key_comparison_count": 675903942,
  93. "block_cache_hit_count": 830086,
  94. }
  95. timestamp = int(time.time())
  96. perf_ts = {}
  97. for key in perf_dict:
  98. perf_ts[key] = {}
  99. start_val = perf_dict[key]
  100. for ix in range(5):
  101. perf_ts[key][timestamp + (ix * 10)] = start_val + (2 * ix * ix)
  102. db_perf_context = DatabasePerfContext(perf_ts, 10, True)
  103. timestamps = [timestamp + (ix * 10) for ix in range(1, 5, 1)]
  104. values = [val for val in range(2, 15, 4)]
  105. inner_dict = {timestamps[ix]: values[ix] for ix in range(4)}
  106. expected_keys_ts = {
  107. NO_ENTITY: {
  108. "user_key_comparison_count": inner_dict,
  109. "block_cache_hit_count": inner_dict,
  110. }
  111. }
  112. self.assertDictEqual(expected_keys_ts, db_perf_context.keys_ts)