-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathtest_stats.py
More file actions
170 lines (164 loc) · 6.03 KB
/
test_stats.py
File metadata and controls
170 lines (164 loc) · 6.03 KB
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
from datetime import datetime, timezone
from pathlib import Path
from textwrap import dedent
import pytest
from freezegun import freeze_time
from dstack._internal.proxy.gateway.schemas.stats import Stat
from dstack._internal.proxy.gateway.services.stats import StatsCollector
@pytest.mark.asyncio
@freeze_time(datetime(2024, 12, 6, 12, 10, tzinfo=timezone.utc))
@pytest.mark.parametrize(
("access_log", "expected_result"),
[
pytest.param(
dedent(
"""
2024-12-06T12:08:00+00:00 srv-0.gtw.test 200 0.100 1
2024-12-06T12:08:00+00:00 srv-1.gtw.test 200 1.100 1
2024-12-06T12:09:15+00:00 srv-0.gtw.test 200 0.200 1
2024-12-06T12:09:15+00:00 srv-1.gtw.test 200 1.200 1
2024-12-06T12:09:45+00:00 srv-0.gtw.test 200 0.300 1
"""
),
{
"srv-0.gtw.test": {
30: Stat(requests=1, request_time=0.3),
60: Stat(requests=2, request_time=0.25),
300: Stat(requests=3, request_time=0.2),
},
"srv-1.gtw.test": {
30: Stat(requests=0, request_time=0.0),
60: Stat(requests=1, request_time=1.2),
300: Stat(requests=2, request_time=1.15),
},
},
id="multiple-services",
),
pytest.param(
dedent(
"""
2024-12-06T12:08:00+00:00 srv.gtw.test 200 0.100 1
2024-12-06T12:08:00+00:00 srv.gtw.test 200 0.200 1
2024-12-06T12:08:00+00:00 srv.gtw.test 200 0.300 1
2024-12-06T12:08:01+00:00 srv.gtw.test 200 0.400 1
2024-12-06T12:08:01+00:00 srv.gtw.test 200 0.500 1
"""
),
{
"srv.gtw.test": {
30: Stat(requests=0, request_time=0.0),
60: Stat(requests=0, request_time=0.0),
300: Stat(requests=5, request_time=0.3),
},
},
id="multiple-entries-per-second",
),
pytest.param(
dedent(
"""
2024-12-06T12:04:50+00:00 srv.gtw.test 200 0.400 1
2024-12-06T12:08:00+00:00 srv.gtw.test 200 0.300 1
2024-12-06T12:09:15+00:00 srv.gtw.test 200 0.200 1
2024-12-06T12:09:45+00:00 srv.gtw.test 200 0.100 1
"""
),
{
"srv.gtw.test": {
30: Stat(requests=1, request_time=0.1),
60: Stat(requests=2, request_time=0.15),
300: Stat(requests=3, request_time=0.2),
},
},
id="ignores-out-of-window",
),
pytest.param(
dedent(
"""
2024-12-06T12:08:01+00:00 srv.gtw.test 200 0.100 1
2024-12-06T12:08:02+00:00 srv.gtw.test 200 0.200 0
2024-12-06T12:08:03+00:00 srv.gtw.test 200 0.300 1
"""
),
{
"srv.gtw.test": {
30: Stat(requests=0, request_time=0.0),
60: Stat(requests=0, request_time=0.0),
300: Stat(requests=2, request_time=0.2),
},
},
id="ignores-replica-not-hit",
),
pytest.param(
dedent(
"""
2024-12-06T12:08:01+00:00 srv.gtw.test 200 0.100
2024-12-06T12:08:02+00:00 srv.gtw.test 303 0.200
2024-12-06T12:08:03+00:00 srv.gtw.test 401 0.300
2024-12-06T12:08:04+00:00 srv.gtw.test 502 0.400
2024-12-06T12:08:05+00:00 srv.gtw.test 403 0.500
2024-12-06T12:08:06+00:00 srv.gtw.test 404 0.600
"""
),
{
"srv.gtw.test": {
30: Stat(requests=0, request_time=0.0),
60: Stat(requests=0, request_time=0.0),
300: Stat(requests=4, request_time=0.25),
},
},
id="ignores-irrelevant-statuses-in-legacy-pre-0.19.11-log",
),
pytest.param(
"",
{},
id="empty-log",
),
],
)
async def test_collect_stats(access_log: str, expected_result: dict, tmp_path: Path) -> None:
access_log_path = tmp_path / "dstack.access.log"
access_log_path.write_text(access_log.lstrip())
collector = StatsCollector(access_log_path)
result = await collector.collect()
assert result == expected_result
@pytest.mark.asyncio
@freeze_time(datetime(2024, 12, 6, 12, 10, tzinfo=timezone.utc))
async def test_collect_stats_after_log_update(tmp_path: Path) -> None:
access_log_path = tmp_path / "dstack.access.log"
collector = StatsCollector(access_log_path)
first_chunk = dedent(
"""
2024-12-06T12:09:15+00:00 srv.gtw.test 200 0.100
2024-12-06T12:09:20+00:00 srv.gtw.test 200 0.200
"""
).lstrip()
second_chunk = dedent(
"""
2024-12-06T12:09:40+00:00 srv.gtw.test 200 0.300
2024-12-06T12:09:45+00:00 srv.gtw.test 200 0.400
2024-12-06T12:09:50+00:00 srv.gtw.test 200 0.500
"""
).lstrip()
first_chunk_stats = {
"srv.gtw.test": {
30: Stat(requests=0, request_time=0.0),
60: Stat(requests=2, request_time=0.15),
300: Stat(requests=2, request_time=0.15),
},
}
both_chunks_stats = {
"srv.gtw.test": {
30: Stat(requests=3, request_time=0.4),
60: Stat(requests=5, request_time=0.3),
300: Stat(requests=5, request_time=0.3),
},
}
with open(access_log_path, "w") as f:
f.write(first_chunk)
f.flush()
result = await collector.collect()
assert result == first_chunk_stats
f.write(second_chunk)
f.flush()
result = await collector.collect()
assert result == both_chunks_stats