def test_call_set_update_increments_call_count_and_duration(): callset = NPlusOneCallSet() callset.update(SQL, 3, 0.11) callset.update(SQL, 2, 0.07) item = callset.find_for(SQL) assert item.call_count == 5 assert item.call_duration == 0.18
def test_call_set_finds_or_creates(): sql = "SELECT * from test" callset = NPlusOneCallSet() assert (0 == len(callset.items)) item = callset.find_for(sql) assert (1 == len(callset.items)) assert (item.sql_string == sql)
def test_call_set_updates(): sql = "SELECT * from test" callset = NPlusOneCallSet() callset.update(sql, 3, 4) item = callset.find_for(sql) assert (3 == item.call_count) assert (4 == item.call_duration)
def test_call_set_should_capture_backtrace_only_once(): callset = NPlusOneCallSet() callset.update(SQL, CALL_COUNT_THRESHOLD, DURATION_THRESHOLD) assert callset.should_capture_backtrace(SQL) assert not callset.should_capture_backtrace(SQL) callset.update(SQL, CALL_COUNT_THRESHOLD // 2, DURATION_THRESHOLD / 2) assert not callset.should_capture_backtrace(SQL)
def test_call_set_update_no_longer_increments_call_count_and_duration_after_capture( ): callset = NPlusOneCallSet() callset.update(SQL, CALL_COUNT_THRESHOLD, DURATION_THRESHOLD) callset.should_capture_backtrace(SQL) callset.update(SQL, CALL_COUNT_THRESHOLD // 2, DURATION_THRESHOLD // 2) item = callset.find_for(SQL) assert item.call_count == CALL_COUNT_THRESHOLD assert item.call_duration == DURATION_THRESHOLD
def test_no_capture_if_already_captured(): sql = "SELECT * from test" callset = NPlusOneCallSet() callset.update(sql, NPlusOneCallSetItem.CALL_COUNT_THRESHOLD, NPlusOneCallSetItem.DURATION_THRESHOLD) assert (callset.should_capture_backtrace(sql) is True) # Subsequent calls should return False assert (callset.should_capture_backtrace(sql) is False) callset.update(sql, 1, 1) assert (callset.should_capture_backtrace(sql) is False)
def test_should_capture_backtrace(): sql = "SELECT * from test" callset = NPlusOneCallSet() callset.update(sql, NPlusOneCallSetItem.CALL_COUNT_THRESHOLD - 1, NPlusOneCallSetItem.DURATION_THRESHOLD - 1) assert (callset.should_capture_backtrace(sql) is False) callset.update(sql, 1, 1) assert (callset.should_capture_backtrace(sql) is True)
def __init__(self, *args, **kwargs): self.req_id = "req-" + str(uuid4()) self.start_time = kwargs.get("start_time", datetime.utcnow()) self.end_time = kwargs.get("end_time", None) self.active_spans = kwargs.get("active_spans", []) self.complete_spans = kwargs.get("complete_spans", []) self.tags = kwargs.get("tags", {}) self.real_request = kwargs.get("real_request", False) self.memory_start = kwargs.get("memory_start", Memory.rss_in_mb()) self.callset = NPlusOneCallSet() logger.debug("Starting request: %s", self.req_id)
def __init__(self, *args, **kwargs): self.req_id = 'req-' + str(uuid4()) self.start_time = kwargs.get('start_time', datetime.utcnow()) self.end_time = kwargs.get('end_time', None) self.active_spans = kwargs.get('active_spans', []) self.complete_spans = kwargs.get('complete_spans', []) self.tags = kwargs.get('tags', {}) self.real_request = kwargs.get('real_request', False) self.memory_start = kwargs.get('memory_start', Memory.rss_in_mb()) self.callset = NPlusOneCallSet() logger.debug('Starting request: %s', self.req_id)
def __init__(self): self.request_id = "req-" + str(uuid4()) self.start_time = dt.datetime.utcnow() self.end_time = None self.active_spans = [] self.complete_spans = [] self.tags = {} self.is_real_request = False self._memory_start = get_rss_in_mb() self.callset = NPlusOneCallSet() logger.debug("Starting request: %s", self.request_id)
def test_is_past_duration_threshold(): sql = "SELECT * from test" callset = NPlusOneCallSet() callset.update(sql, 0, NPlusOneCallSetItem.DURATION_THRESHOLD - 1) item = callset.find_for(sql) assert (item.is_past_duration_threshold() is False) callset.update(sql, 0, 1) assert (item.is_past_duration_threshold() is True) # Assert again to test the attribute check in the function assert (item.is_past_duration_threshold() is True)
def test_call_set_find_for_creates_new_item(): callset = NPlusOneCallSet() assert len(callset.items) == 0 item = callset.find_for(SQL) assert len(callset.items) == 1 assert item.sql_string == SQL
def test_call_set_find_for_returns_existing_item(): callset = NPlusOneCallSet() item1 = callset.find_for(SQL) item2 = callset.find_for(SQL) assert len(callset.items) == 1 assert item1 is item2