Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: response time worst in input #319

Merged
merged 8 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/caret_analyze/record/records_service/response_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def columns(self) -> list[str]:
return self._columns


# NOTE: Rename ResponseMap after refactoring
class ResponseMapAll:

def __init__(
Expand Down Expand Up @@ -259,7 +260,6 @@ def __init__(
else:
idx = self._start_timestamps.index(start_ts)
if end_ts < self._end_timestamps[idx]:
self._start_timestamps[idx] = start_ts
self._end_timestamps[idx] = end_ts

def to_all_records(self) -> RecordsInterface:
Expand All @@ -274,6 +274,28 @@ def to_all_records(self) -> RecordsInterface:

return records

def to_worst_in_input_records(self) -> RecordsInterface:
end_timestamps: list[int] = []
start_timestamps: list[int] = []
for start_ts, end_ts in zip(self._start_timestamps, self._end_timestamps):
if end_ts not in end_timestamps:
start_timestamps.append(start_ts)
end_timestamps.append(end_ts)
else:
idx = end_timestamps.index(end_ts)
if start_ts < start_timestamps[idx]:
start_timestamps[idx] = start_ts

records = self._create_empty_records()
for start_ts, end_ts in zip(start_timestamps, end_timestamps):
record = {
self._start_column: start_ts,
'response_time': end_ts - start_ts
}
records.append(record)
keita1523 marked this conversation as resolved.
Show resolved Hide resolved

return records

def _create_empty_records(
self
) -> RecordsInterface:
Expand Down Expand Up @@ -351,6 +373,9 @@ def __init__(
def to_all_records(self) -> RecordsInterface:
return self._response_map_all.to_all_records()

def to_worst_in_input_records(self) -> RecordsInterface:
return self._response_map_all.to_worst_in_input_records()

def to_stacked_bar(self) -> RecordsInterface:
"""
Calculate records for stacked bar.
Expand Down
104 changes: 104 additions & 0 deletions src/test/record/records_service/test_response_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,107 @@ def test_to_worst_case_timeseries(self):
{'column0_min': 20, 'response_time': 25}
]
assert to_dict(response_time) == expect


class TestResponseTimeWorstInInput:

def test_empty_case(self):
records_raw = []
columns = [ColumnValue('start'), ColumnValue('end')]
records = create_records(records_raw, columns)

response_time = ResponseTime(records)

expect_raw = []
result = to_dict(response_time.to_worst_in_input_records())
assert result == expect_raw

def test_two_column_default_case(self):
records_raw = [
{'start': 0, 'end': 2},
{'start': 3, 'end': 4},
{'start': 11, 'end': 12}
]
columns = [ColumnValue('start'), ColumnValue('end')]
records = create_records(records_raw, columns)

response_time = ResponseTime(records)

expect_raw = [
{'start': 0, 'response_time': 2},
{'start': 3, 'response_time': 1},
{'start': 11, 'response_time': 1}
]
result = to_dict(response_time.to_worst_in_input_records())
assert result == expect_raw

def test_three_column_default_case(self):
records_raw = [
{'start': 0, 'middle': 1, 'end': 2},
{'start': 3, 'middle': 4, 'end': 6},
{'start': 11, 'middle': 13, 'end': 16}
]
columns = [ColumnValue('start'), ColumnValue('middle'), ColumnValue('end')]
records = create_records(records_raw, columns)

response_time = ResponseTime(records)

expect_raw = [
{'start': 0, 'response_time': 2},
{'start': 3, 'response_time': 3},
{'start': 11, 'response_time': 5}
]
result = to_dict(response_time.to_worst_in_input_records())
assert result == expect_raw

def test_single_input_multi_output_case(self):
records_raw = [
{'start': 0, 'middle': 4, 'end': 5},
{'start': 0, 'middle': 4, 'end': 6},
{'start': 0, 'middle': 12, 'end': 13}
]
columns = [ColumnValue('start'), ColumnValue('middle'), ColumnValue('end')]
records = create_records(records_raw, columns)

response_time = ResponseTime(records)

expect_raw = [
{'start': 0, 'response_time': 5}
]
result = to_dict(response_time.to_worst_in_input_records())
assert result == expect_raw

def test_multi_input_single_output_case(self):
records_raw = [
{'start': 0, 'middle': 4, 'end': 13},
{'start': 1, 'middle': 4, 'end': 13},
{'start': 5, 'middle': 12, 'end': 13}
]
columns = [ColumnValue('start'), ColumnValue('middle'), ColumnValue('end')]
records = create_records(records_raw, columns)

response_time = ResponseTime(records)

expect_raw = [
{'start': 0, 'response_time': 13}
]
result = to_dict(response_time.to_worst_in_input_records())
assert result == expect_raw

def test_drop_case(self):
records_raw = [
{'start': 0, 'middle': 4, 'end': 8},
{'start': 1, 'middle': 4},
{'start': 5, 'middle': 12, 'end': 13}
]
columns = [ColumnValue('start'), ColumnValue('middle'), ColumnValue('end')]
records = create_records(records_raw, columns)

response_time = ResponseTime(records)

expect_raw = [
{'start': 0, 'response_time': 8},
{'start': 1, 'response_time': 12}
]
result = to_dict(response_time.to_worst_in_input_records())
assert result == expect_raw
Loading