1
1
import asyncio
2
2
import logging
3
- import time
4
- from contextlib import contextmanager
5
3
from datetime import datetime , timedelta , timezone
6
4
7
5
import pytest
10
8
log = logging .getLogger (__name__ )
11
9
12
10
13
- @contextmanager
14
- def timer ():
15
- start = time .perf_counter ()
16
- yield lambda : time .perf_counter () - start
17
-
18
-
19
11
@pytest .fixture (scope = "function" )
20
12
def agents (session_local , host , models ):
21
13
agent_ids = []
@@ -67,9 +59,9 @@ async def _create_checkins(session_local, models, agent_ids):
67
59
)
68
60
69
61
70
- agent_count = 10
71
- time_delta = 5 # 17280 checkins per agent per day
72
- days_back = 7
62
+ agent_count = 2
63
+ time_delta = 20 # 4320 checkins per agent per day
64
+ days_back = 3
73
65
end_time = datetime (2023 , 1 , 8 , tzinfo = timezone .utc )
74
66
start_time = end_time - timedelta (days = days_back )
75
67
@@ -88,51 +80,6 @@ async def _create_checkin(session_local, models, agent_id):
88
80
db_2 .add_all (checkins )
89
81
90
82
91
- @pytest .mark .slow
92
- def test_database_performance_checkins (models , host , agents , session_local ):
93
- # logging.basicConfig()
94
- # logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
95
- # logging.getLogger("sqlalchemy.engine").propagate = True
96
- # print(query.statement.compile(compile_kwargs={"literal_binds": True}))
97
-
98
- with session_local () as db :
99
- asyncio .run (_create_checkins (session_local , models , agents ))
100
-
101
- with timer () as t :
102
- checkins = db .query (models .AgentCheckIn ).count ()
103
- assert checkins >= (agent_count * 17280 * days_back )
104
- log .info (f"Time to query { checkins } checkins count: { t ():0.4f} seconds" )
105
-
106
- with timer () as t :
107
- agents = db .query (models .Agent ).count ()
108
- assert agents >= agent_count
109
- log .info (f"Time to query { agents } agents count: { t ():0.4f} seconds" )
110
- assert t () < 1
111
-
112
- with timer () as t :
113
- query = db .query (models .Agent )
114
- query .all ()
115
- log .info (f"Time to query { agents } agents: { t ():0.4f} seconds" )
116
- assert t () < 1
117
-
118
- with timer () as t :
119
- query = db .query (models .AgentCheckIn ).limit (50000 )
120
- query .all ()
121
- log .info (f"Time to query { checkins } checkins: { t ():0.4f} seconds" )
122
- assert t () < 6 # noqa: PLR2004
123
-
124
- agents = db .query (models .Agent ).all ()
125
-
126
- with timer () as t :
127
- for a in agents :
128
- name = a .name
129
- lastseen_time = a .lastseen_time
130
- stale = a .stale
131
- log .info (f"{ name } - { lastseen_time } - { stale } " )
132
- log .info (f"Time to query { agents } agents' dynamic fields: { t ():0.4f} seconds" )
133
- assert t () < 0.1 * agent_count
134
-
135
-
136
83
def test_get_agent_checkins_agent_not_found (client , admin_auth_header ):
137
84
response = client .get ("/api/v2/agents/XYZ123/checkins" , headers = admin_auth_header )
138
85
@@ -153,7 +100,7 @@ def test_get_agent_checkins_with_limit_and_page(
153
100
checkin_count = 10
154
101
assert response .status_code == status .HTTP_200_OK
155
102
assert len (response .json ()["records" ]) == checkin_count
156
- assert response .json ()["total" ] > days_back * 17280
103
+ assert response .json ()["total" ] > days_back * 4320
157
104
assert response .json ()["page" ] == 1
158
105
159
106
page1 = response .json ()["records" ]
@@ -166,7 +113,7 @@ def test_get_agent_checkins_with_limit_and_page(
166
113
page_count = 2
167
114
assert response .status_code == status .HTTP_200_OK
168
115
assert len (response .json ()["records" ]) == checkin_count
169
- assert response .json ()["total" ] > days_back * 17280
116
+ assert response .json ()["total" ] > days_back * 4320
170
117
assert response .json ()["page" ] == page_count
171
118
172
119
page2 = response .json ()["records" ]
@@ -178,18 +125,17 @@ def test_get_agent_checkins_with_limit_and_page(
178
125
def test_get_agent_checkins_multiple_agents (
179
126
client , admin_auth_header , agents , session_local , models
180
127
):
181
- with_checkins = agents [:3 ]
182
- asyncio .run (_create_checkins (session_local , models , with_checkins ))
128
+ asyncio .run (_create_checkins (session_local , models , agents ))
183
129
184
130
response = client .get (
185
131
"/api/v2/agents/checkins" ,
186
132
headers = admin_auth_header ,
187
- params = {"agents" : with_checkins [: 2 ] , "limit" : 400000 },
133
+ params = {"agents" : agents , "limit" : 400000 },
188
134
)
189
135
190
136
assert response .status_code == status .HTTP_200_OK
191
- assert len (response .json ()["records" ]) == days_back * 17280 * 2
192
- assert {r ["agent_id" ] for r in response .json ()["records" ]} == set (with_checkins [: 2 ] )
137
+ assert len (response .json ()["records" ]) == days_back * 4320 * agent_count
138
+ assert {r ["agent_id" ] for r in response .json ()["records" ]} == set (agents )
193
139
194
140
195
141
@pytest .mark .slow
@@ -199,7 +145,7 @@ def test_agent_checkins_aggregate(
199
145
if empire_config .database .use == "sqlite" :
200
146
pytest .skip ("sqlite not supported for checkin aggregation" )
201
147
202
- asyncio .run (_create_checkins (session_local , models , agents [: 3 ] ))
148
+ asyncio .run (_create_checkins (session_local , models , agents ))
203
149
204
150
response = client .get (
205
151
"/api/v2/agents/checkins/aggregate" ,
@@ -209,7 +155,7 @@ def test_agent_checkins_aggregate(
209
155
assert response .status_code == status .HTTP_200_OK
210
156
assert response .elapsed .total_seconds () < 5 # noqa: PLR2004
211
157
assert response .json ()["bucket_size" ] == "day"
212
- assert response .json ()["records" ][1 ]["count" ] == 17280 * 3
158
+ assert response .json ()["records" ][1 ]["count" ] == 4320 * agent_count
213
159
214
160
response = client .get (
215
161
"/api/v2/agents/checkins/aggregate" ,
@@ -220,7 +166,7 @@ def test_agent_checkins_aggregate(
220
166
assert response .status_code == status .HTTP_200_OK
221
167
assert response .elapsed .total_seconds () < 5 # noqa: PLR2004
222
168
assert response .json ()["bucket_size" ] == "hour"
223
- assert response .json ()["records" ][1 ]["count" ] == 720 * 3
169
+ assert response .json ()["records" ][1 ]["count" ] == 180 * agent_count
224
170
225
171
response = client .get (
226
172
"/api/v2/agents/checkins/aggregate" ,
@@ -231,7 +177,7 @@ def test_agent_checkins_aggregate(
231
177
assert response .status_code == status .HTTP_200_OK
232
178
assert response .elapsed .total_seconds () < 5 # noqa: PLR2004
233
179
assert response .json ()["bucket_size" ] == "minute"
234
- assert response .json ()["records" ][1 ]["count" ] == 12 * 3
180
+ assert response .json ()["records" ][1 ]["count" ] == 3 * agent_count
235
181
236
182
response = client .get (
237
183
"/api/v2/agents/checkins/aggregate" ,
@@ -246,7 +192,7 @@ def test_agent_checkins_aggregate(
246
192
assert response .status_code == status .HTTP_200_OK
247
193
assert response .elapsed .total_seconds () < 5 # noqa: PLR2004
248
194
assert response .json ()["bucket_size" ] == "second"
249
- assert response .json ()["records" ][1 ]["count" ] == 1 * 3
195
+ assert response .json ()["records" ][1 ]["count" ] == 1 * agent_count
250
196
251
197
# Test start date and end date
252
198
response = client .get (
0 commit comments