-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_metrics.py
176 lines (157 loc) · 7.71 KB
/
task_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Provides statistical metrics calculation functions for task performance analysis."""
import numpy as np
import streamlit as st
from task_loads import sql_query
from tables import Tasks
from logger import logger
def format_number(number: int):
return number
# return f"{number:,}"
def report_number(sql_string: str, index: int):
"""Calculate statistical metrics from SQL query results.
Args:
sql_string: SQL query to execute
index: Column index to analyze from results
Returns:
dict: Statistical metrics including percentiles, avg, min, max
"""
try:
# logger.info(sql_string)
res = sql_query(sql_string)
if request_count := [int(item[index]) for item in res]:
return {
"P50": format_number(int(np.percentile(request_count, 50))),
"P90": format_number(int(np.percentile(request_count, 90))),
"P99": format_number(int(np.percentile(request_count, 99))),
"P999": format_number(int(np.percentile(request_count, 99.9))),
"Avg": format_number(int(np.mean(request_count))),
"Min": format_number(min(request_count)),
"Max": format_number(max(request_count)),
}
except Exception as e:
logger.error(e)
st.error(e)
return {
"P50": None,
"P90": None,
"P99": None,
"P999": None,
"Avg": None,
"Min": None,
"Max": None,
}
def task_metrics(task: Tasks):
"""Generate performance metrics for a given task.
Args:
task: Task object containing execution details
Returns:
dict: Collection of performance metrics
"""
stream = bool(task.stream)
chunks = f"chunks_{task.id}"
requests = f"requests_{task.id}"
if stream:
return {
"Concurrency": report_number(
f"SELECT COUNT(DISTINCT thread_num) AS request_count FROM {requests}",
0,
),
"Threads Per Sec": report_number(
f"SELECT ROUND((created_at / 1000)) AS timestamp_seconds, COUNT(DISTINCT thread_num) AS request_count FROM {chunks} GROUP BY timestamp_seconds ORDER BY timestamp_seconds",
1,
),
"Threads Per Minute": report_number(
f"SELECT ROUND((created_at / 60000)) AS timestamp_seconds, COUNT(DISTINCT thread_num) AS request_count FROM {chunks} GROUP BY timestamp_seconds ORDER BY timestamp_seconds",
1,
),
"Requests Per Sec": report_number(
f"SELECT ROUND((created_at / 1000)) AS timestamp_seconds, COUNT(DISTINCT id) AS request_count FROM {requests} WHERE success = 1 GROUP BY timestamp_seconds ORDER BY timestamp_seconds",
1,
),
"Request Per Minute": report_number(
f"SELECT ROUND((created_at / 60000)) AS timestamp_seconds, COUNT(DISTINCT id) AS request_count FROM {requests} WHERE success = 1 GROUP BY timestamp_seconds ORDER BY timestamp_seconds",
1,
),
"Input Token Per Sec": report_number(
f"SELECT ROUND((start_req_time / 1000)) AS timestamp_seconds, sum(input_token_count) AS input_token_count FROM {requests} WHERE success = 1 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Input Token Per Minute": report_number(
f"SELECT ROUND((start_req_time / 60000)) AS timestamp_seconds, sum(input_token_count) AS input_token_count FROM {requests} WHERE success = 1 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Output Token Per Sec": report_number(
f"SELECT ROUND((created_at / 1000)) AS timestamp_seconds, sum(token_len) AS token_count FROM {chunks} WHERE token_len>0 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Output Token Per Minute": report_number(
f"SELECT ROUND((created_at / 60000)) AS timestamp_seconds, sum(token_len) AS token_count FROM {chunks} WHERE token_len>0 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Output Characters Per Sec": report_number(
f"SELECT ROUND((created_at / 1000)) AS timestamp_seconds, sum(characters_len) AS characters_count FROM {chunks} WHERE characters_len>0 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Output Characters Per Minute": report_number(
f"SELECT ROUND((created_at / 60000)) AS timestamp_seconds, sum(characters_len) AS characters_count FROM {chunks} WHERE characters_len>0 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Time To First Token (TTFT) Per Request": report_number(
f"SELECT first_token_latency_ms FROM {requests} WHERE first_token_latency_ms is not null;",
0,
),
"Time Between Tokens (TBT) Per Request": report_number(
f"SELECT last_token_latency_ms FROM {requests} WHERE last_token_latency_ms is not null;",
0,
),
"Request Latency Per Request": report_number(
f"SELECT request_latency_ms FROM {requests} WHERE request_latency_ms is not null;",
0,
),
"Input Token Per Request": report_number(
f"SELECT input_token_count FROM {requests} WHERE success = 1 and input_token_count is not null;",
0,
),
"Chunks Per Request": report_number(
f"SELECT chunks_count FROM {requests} WHERE success = 1 and chunks_count is not null;",
0,
),
"Output Token Per Request": report_number(
f"SELECT output_token_count FROM {requests} WHERE success = 1 and output_token_count is not null;",
0,
),
}
return {
"Concurrency": report_number(
f"SELECT ROUND((start_req_time / 1000)) AS timestamp_seconds, COUNT(DISTINCT thread_num) AS request_count FROM {requests} WHERE start_req_time is not null GROUP BY timestamp_seconds ORDER BY timestamp_seconds",
1,
),
"Request Per Sec": report_number(
f"SELECT ROUND((start_req_time / 1000)) AS timestamp_seconds, COUNT(DISTINCT id) AS request_count FROM {requests} WHERE start_req_time is not null GROUP BY timestamp_seconds ORDER BY timestamp_seconds",
1,
),
"Output Token Per Sec": report_number(
f"SELECT ROUND((start_req_time / 1000)) AS timestamp_seconds, sum(output_token_count) AS token_count FROM {requests} WHERE start_req_time is not null and output_token_count > 0 GROUP BY timestamp_seconds ORDER BY timestamp_seconds;",
1,
),
"Time To First Token (TTFT)": report_number(
f"SELECT first_token_latency_ms FROM {requests} WHERE first_token_latency_ms is not null;",
0,
),
"Time Between Tokens (TBT)": report_number(
f"SELECT last_token_latency_ms FROM {requests} WHERE last_token_latency_ms is not null;",
0,
),
"Request Latency": report_number(
f"SELECT request_latency_ms FROM {requests} WHERE request_latency_ms is not null;",
0,
),
"Chunks Count": report_number(
f"SELECT chunks_count FROM {requests} WHERE success = 1 and chunks_count is not null;",
0,
),
"Output Token Count": report_number(
f"SELECT output_token_count FROM {requests} WHERE success = 1 and output_token_count is not null;",
0,
),
}