|
| 1 | +import datetime |
| 2 | +from unittest import TestCase, mock |
| 3 | +from plan_monitor import config |
| 4 | +from plan_monitor.detect import calculate_plan_age_stats, is_established_plan, \ |
| 5 | + get_query_plan_hashes_under_investigation |
| 6 | + |
| 7 | +def get_time_diff_from_ms(start_time: datetime, seconds_to_subtract: int) -> int: |
| 8 | + ts = start_time - datetime.timedelta(seconds=seconds_to_subtract) |
| 9 | + return ts.timestamp() * 1000 |
| 10 | + |
| 11 | +class CalculatePlanAge(TestCase): |
| 12 | + |
| 13 | + def test_calculate_plan_age(self): |
| 14 | + plan_stats = { |
| 15 | + "creation_time": 3234325, |
| 16 | + "last_execution_time": 324242, |
| 17 | + "worst_statement_query_plan_hash": "23424252" |
| 18 | + } |
| 19 | + dt = datetime.datetime.now() |
| 20 | + stats_time = int(dt.strftime("%Y%m%d%H%M%S")) |
| 21 | + plan_age_seconds, last_execution_time_seconds = calculate_plan_age_stats(plan_stats, stats_time) |
| 22 | + expected_plan_age = (stats_time - plan_stats['creation_time']) / 1000 |
| 23 | + exected_last_execution_age = (stats_time - plan_stats['last_execution_time']) / 1000 |
| 24 | + self.assertEqual(expected_plan_age, plan_age_seconds) |
| 25 | + self.assertEqual(exected_last_execution_age, last_execution_time_seconds) |
| 26 | + |
| 27 | +class IsEstablishedPlan(TestCase): |
| 28 | + |
| 29 | + # plan is established because plan age is sufficiently old |
| 30 | + @mock.patch('plan_monitor.config') |
| 31 | + def test_is_established_plan_plan_age(self, conf): |
| 32 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 33 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 34 | + dt = datetime.datetime.now() |
| 35 | + stats_time = dt.timestamp() * 1000 |
| 36 | + established_create_ms = get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)) |
| 37 | + established_last_execution_ms = get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)) |
| 38 | + plan_stats = { |
| 39 | + "creation_time": established_create_ms, |
| 40 | + "last_execution_time": established_last_execution_ms, |
| 41 | + "worst_statement_query_plan_hash": "23424252" |
| 42 | + } |
| 43 | + |
| 44 | + plan_age_seconds, last_execution_time_seconds = calculate_plan_age_stats(plan_stats, stats_time) |
| 45 | + is_established = is_established_plan(plan_age_seconds, last_execution_time_seconds) |
| 46 | + self.assertTrue(is_established) |
| 47 | + |
| 48 | + # plan is established because plan execution age is sufficiently old |
| 49 | + @mock.patch('plan_monitor.config') |
| 50 | + def test_is_established_plan_execution_age(self, conf): |
| 51 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 52 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 53 | + dt = datetime.datetime.now() |
| 54 | + stats_time = dt.timestamp() * 1000 |
| 55 | + established_create_ms = get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS - 1)) |
| 56 | + established_last_execution_ms = get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS + 1)) |
| 57 | + |
| 58 | + plan_stats = { |
| 59 | + "creation_time": established_create_ms, |
| 60 | + "last_execution_time": established_last_execution_ms, |
| 61 | + "worst_statement_query_plan_hash": "23424252" |
| 62 | + } |
| 63 | + |
| 64 | + plan_age_seconds, last_execution_time_seconds = calculate_plan_age_stats(plan_stats, stats_time) |
| 65 | + is_established = is_established_plan(plan_age_seconds, last_execution_time_seconds) |
| 66 | + self.assertTrue(is_established) |
| 67 | + |
| 68 | + # plan is established because plan both execution age and plan age are sufficiently old |
| 69 | + @mock.patch('plan_monitor.config') |
| 70 | + def test_is_established_plan_both(self, conf): |
| 71 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 72 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 73 | + dt = datetime.datetime.now() |
| 74 | + stats_time = dt.timestamp() * 1000 |
| 75 | + established_create_ms = get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)) |
| 76 | + established_last_execution_ms = get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS + 1)) |
| 77 | + plan_stats = { |
| 78 | + "creation_time": established_create_ms, |
| 79 | + "last_execution_time": established_last_execution_ms, |
| 80 | + "worst_statement_query_plan_hash": "23424252" |
| 81 | + } |
| 82 | + |
| 83 | + plan_age_seconds, last_execution_time_seconds = calculate_plan_age_stats(plan_stats, stats_time) |
| 84 | + is_established = is_established_plan(plan_age_seconds, last_execution_time_seconds) |
| 85 | + self.assertTrue(is_established) |
| 86 | + |
| 87 | + # plan is not established |
| 88 | + @mock.patch('plan_monitor.config') |
| 89 | + def test_is_not_established_plan(self, conf): |
| 90 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 91 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 92 | + dt = datetime.datetime.now() |
| 93 | + stats_time = dt.timestamp() * 1000 |
| 94 | + established_create_ms = get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS - 1)) |
| 95 | + established_last_execution_ms = get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)) |
| 96 | + plan_stats = { |
| 97 | + "creation_time": established_create_ms, |
| 98 | + "last_execution_time": established_last_execution_ms, |
| 99 | + "worst_statement_query_plan_hash": "23424252" |
| 100 | + } |
| 101 | + |
| 102 | + plan_age_seconds, last_execution_time_seconds = calculate_plan_age_stats(plan_stats, stats_time) |
| 103 | + is_established = is_established_plan(plan_age_seconds, last_execution_time_seconds) |
| 104 | + self.assertFalse(is_established) |
| 105 | + |
| 106 | +class GetActiveQueryPlanHashes(TestCase): |
| 107 | + |
| 108 | + # an established query plan hash matches an unestablished plan |
| 109 | + @mock.patch('plan_monitor.config') |
| 110 | + def test_get_query_plan_hash_under_investigation(self, conf): |
| 111 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 112 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 113 | + duplicate_query_plan_hash = '2FCDCA2278D3D2A3' |
| 114 | + dt = datetime.datetime.now() |
| 115 | + stats_time = dt.timestamp() * 1000 |
| 116 | + plans = { |
| 117 | + "plan-one-duplicate-qp-hash-not-established": { |
| 118 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS - 1)), |
| 119 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)), |
| 120 | + "worst_statement_query_plan_hash": duplicate_query_plan_hash |
| 121 | + }, |
| 122 | + "not-a-match-established": { |
| 123 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)), |
| 124 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)), |
| 125 | + "worst_statement_query_plan_hash": "not-a-query-plan-hash-match" |
| 126 | + }, |
| 127 | + "plan-two-duplicate-qp-hash-is-established": { |
| 128 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 2)), |
| 129 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS + 2)), |
| 130 | + "worst_statement_query_plan_hash": duplicate_query_plan_hash |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + qp_hashes_under_investigation = get_query_plan_hashes_under_investigation(plans, stats_time) |
| 135 | + self.assertEqual(len(qp_hashes_under_investigation), 1) |
| 136 | + self.assertEqual(qp_hashes_under_investigation.pop(), duplicate_query_plan_hash) |
| 137 | + |
| 138 | + # returns a query hash plan even if neither duplicate is established |
| 139 | + @mock.patch('plan_monitor.config') |
| 140 | + def test_get_query_plan_hash_under_investigation_returns_qp_hash(self, conf): |
| 141 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 142 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 143 | + duplicate_query_plan_hash = '2FCDCA2278D3D2A3' |
| 144 | + dt = datetime.datetime.now() |
| 145 | + stats_time = dt.timestamp() * 1000 |
| 146 | + plans = { |
| 147 | + "plan-one-duplicate-qp-hash-not-established": { |
| 148 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS - 1)), |
| 149 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)), |
| 150 | + "worst_statement_query_plan_hash": duplicate_query_plan_hash |
| 151 | + }, |
| 152 | + "not-a-match-established": { |
| 153 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)), |
| 154 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)), |
| 155 | + "worst_statement_query_plan_hash": "not-a-query-plan-hash-match" |
| 156 | + }, |
| 157 | + "plan-two-duplicate-qp-hash-not-established-either": { |
| 158 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS - 1)), |
| 159 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)), |
| 160 | + "worst_statement_query_plan_hash": duplicate_query_plan_hash |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + qp_hashes_under_investigation = get_query_plan_hashes_under_investigation(plans, stats_time) |
| 165 | + self.assertEqual(len(qp_hashes_under_investigation), 1) |
| 166 | + self.assertEqual(qp_hashes_under_investigation.pop(), duplicate_query_plan_hash) |
| 167 | + |
| 168 | + # does not return a query plan hash if duplicates are both established |
| 169 | + @mock.patch('plan_monitor.config') |
| 170 | + def test_get_query_plan_hash_under_investigation_doesnt_return_established_plans(self, conf): |
| 171 | + conf.MAX_AGE_OF_LAST_EXECUTION_SECONDS.return_value = 5 |
| 172 | + conf.MAX_NEW_PLAN_AGE_SECONDS.return_value = 3 |
| 173 | + duplicate_query_plan_hash = '2FCDCA2278D3D2A3' |
| 174 | + dt = datetime.datetime.now() |
| 175 | + stats_time = dt.timestamp() * 1000 |
| 176 | + plans = { |
| 177 | + "plan-one-duplicate-qp-hash-established": { |
| 178 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)), |
| 179 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS + 1)), |
| 180 | + "worst_statement_query_plan_hash": duplicate_query_plan_hash |
| 181 | + }, |
| 182 | + "not-a-match-established": { |
| 183 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)), |
| 184 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS - 1)), |
| 185 | + "worst_statement_query_plan_hash": "not-a-query-plan-hash-match" |
| 186 | + }, |
| 187 | + "plan-two-duplicate-qp-hash-established-either": { |
| 188 | + "creation_time": get_time_diff_from_ms(dt, (config.MAX_NEW_PLAN_AGE_SECONDS + 1)), |
| 189 | + "last_execution_time": get_time_diff_from_ms(dt, (config.MAX_AGE_OF_LAST_EXECUTION_SECONDS + 1)), |
| 190 | + "worst_statement_query_plan_hash": duplicate_query_plan_hash |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + qp_hashes_under_investigation = get_query_plan_hashes_under_investigation(plans, stats_time) |
| 195 | + self.assertEqual(len(qp_hashes_under_investigation), 0) |
| 196 | + |
| 197 | + |
0 commit comments