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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
import unittest
import yappi
import utils
import logging
import asyncio
import tempfile
try:
import aiofiles
HAS_AIOFILES = True
except ImportError:
HAS_AIOFILES = False
try:
from aiohttp import web
import aiohttp
HAS_AIOHTTP = True
except ImportError:
HAS_AIOHTTP = False
try:
import httpx
HAS_HTTPX = True
except ImportError:
HAS_HTTPX = False
try:
from quart import Quart, jsonify
HAS_QUART = True
except ImportError:
HAS_QUART = False
# A test file containing advanced asyncio tests which have to be verified
# manually.
class Manual(utils.AergiaUnitTestCase):
@unittest.skipIf(not HAS_AIOHTTP,
"Skipping 'test_fetch' because aiohttp "
"could not be imported.")
def test_fetch(self):
async def handle(request):
return web.Response(text="Hello, world")
def get_runner():
app = web.Application()
app.router.add_get('/', handle)
return web.AppRunner(app)
async def start_server():
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()
async def stop_server():
await runner.cleanup()
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def query(urls):
tasks = [fetch(url) for url in urls]
return await asyncio.gather(*tasks)
async def main():
await start_server()
# wait for server to start
await asyncio.sleep(1)
urls = ['http://localhost:8080'] * 25
await query(urls)
await stop_server()
print('')
runner = get_runner()
yappi.start()
asyncio.run(main())
yappi.stop()
self.yappi_print_traceable_results(yappi.get_func_stats())
runner = get_runner()
self.Aergia.start()
asyncio.run(main())
self.Aergia.print_samples()
self.Aergia.stop()
print('')
@unittest.skipIf(not HAS_AIOHTTP,
"Skipping 'test_fetch_with_burn' because aiohttp "
"could not be imported.")
def test_fetch_with_burn(self):
async def handle(request):
utils.burn_cpu(0.05)
return web.Response(text="Hello, world")
def get_runner():
app = web.Application()
app.router.add_get('/', handle)
return web.AppRunner(app)
async def start_server():
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()
async def stop_server():
await runner.cleanup()
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def query(urls):
tasks = [fetch(url) for url in urls]
return await asyncio.gather(*tasks)
async def main():
await start_server()
# wait for server to start
await asyncio.sleep(1)
urls = ['http://localhost:8080'] * 150
await query(urls)
await stop_server()
print('')
runner = get_runner()
yappi.start()
asyncio.run(main())
yappi.stop()
self.yappi_print_traceable_results(yappi.get_func_stats())
runner = get_runner()
self.Aergia.start()
asyncio.run(main())
self.Aergia.print_samples()
self.Aergia.stop()
print('')
@unittest.skipIf(not HAS_HTTPX or not HAS_QUART,
"Skipping 'test_quart' because quart or httpx "
"could not be imported.")
def test_quart(self):
# https://quart-latest-doc-gen.readthedocs.io/en/latest/how_to_guides/logging.html
# official solutions don't actually disable the logger. :(
logging.disable(logging.WARN)
app = Quart(__name__)
@app.route('/', methods=['GET'])
async def fetch():
return jsonify({"message": "Hello, World!"})
async def query(url):
async with httpx.AsyncClient() as c:
t = [c.get(f'{url}/data') for _ in range(200)]
return await asyncio.gather(*t)
async def main():
asyncio.create_task(app.run_task(port=5000))
url = "http:/localhost:5000"
await query(url)
await app.shutdown()
print('')
yappi.start()
asyncio.run(main())
yappi.stop()
self.yappi_print_traceable_results(yappi.get_func_stats())
self.Aergia.start()
asyncio.run(main())
self.Aergia.print_samples()
self.Aergia.stop()
print('')
@unittest.skipIf(not HAS_HTTPX or not HAS_QUART,
"Skipping 'test_quart_with_burn' because quart or httpx "
"could not be imported.")
def test_quart_with_burn(self):
# https://quart-latest-doc-gen.readthedocs.io/en/latest/how_to_guides/logging.html
# official solutions don't actually disable the logger. :(
logging.disable(logging.WARN)
app = Quart(__name__)
@app.route('/', methods=['GET'])
async def fetch():
return jsonify({"message": "Hello, World!"})
async def query(url):
async with httpx.AsyncClient() as c:
utils.burn_cpu(0.05)
t = [c.get(f'{url}/data') for _ in range(200)]
return await asyncio.gather(*t)
async def main():
asyncio.create_task(app.run_task(port=5000))
url = "http:/localhost:5000"
await query(url)
await app.shutdown()
print('')
yappi.start()
asyncio.run(main())
yappi.stop()
self.yappi_print_traceable_results(yappi.get_func_stats())
self.Aergia.start()
asyncio.run(main())
self.Aergia.print_samples()
self.Aergia.stop()
print('')
@unittest.skipIf(not HAS_AIOFILES,
"Skipping 'test_file' because aiofiles "
"could not be imported.")
def test_file(self):
async def write_file(filename, content):
async with aiofiles.open(filename, mode='w') as f:
await f.write(content)
async def read_file(filename):
async with aiofiles.open(filename, mode='r') as f:
content = await f.read()
return content
async def read_lots(filename, iterations):
tasks = [read_file(filename) for _ in range(iterations)]
await asyncio.gather(*tasks)
async def main():
# Create a temp file (/tmp)
with tempfile.NamedTemporaryFile(delete=True, suffix='.txt') as temp_file:
filename = temp_file.name
content = 'Hello, World!'
# write the file
await write_file(filename, content)
# read the file 200 times
await read_lots(filename, 200)
print('')
yappi.start()
asyncio.run(main())
yappi.stop()
self.yappi_print_traceable_results(yappi.get_func_stats())
self.Aergia.start()
asyncio.run(main())
self.Aergia.print_samples()
self.Aergia.stop()
print('')
@unittest.skipIf(not HAS_AIOFILES,
"Skipping 'test_file_with_burn' because aiofiles "
"could not be imported.")
def test_file_with_burn(self):
async def write_file(filename, content):
async with aiofiles.open(filename, mode='w') as f:
await f.write(content)
async def read_file(filename):
utils.burn_cpu(0.05)
async with aiofiles.open(filename, mode='r') as f:
content = await f.read()
return content
async def read_lots(filename, iterations):
tasks = [read_file(filename) for _ in range(iterations)]
await asyncio.gather(*tasks)
async def main():
# Create a temp file (/tmp)
with tempfile.NamedTemporaryFile(delete=False, suffix='.txt') as temp_file:
filename = temp_file.name
content = 'Hello, World!'
# write the file
await write_file(filename, content)
# read the file 200 times
await read_lots(filename, 200)
print('')
yappi.start()
asyncio.run(main())
yappi.stop()
self.yappi_print_traceable_results(yappi.get_func_stats())
self.Aergia.start()
asyncio.run(main())
self.Aergia.print_samples()
self.Aergia.stop()
print('')
|