Skip to content

Commit

Permalink
fix black
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic182 committed Aug 14, 2024
1 parent 59f920e commit 3eb023d
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions tests/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

try:
import uvloop

uvloop.install()
except ImportError:
pass
Expand All @@ -38,18 +39,22 @@ async def app(scope, receive, send):
"""Simple ASGI app."""
assert scope["type"] == "http"
res = b"foo"
await send({
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
[b"content-length", b"%d" % len(res)],
],
})
await send({
"type": "http.response.body",
"body": res,
})
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
[b"content-length", b"%d" % len(res)],
],
}
)
await send(
{
"type": "http.response.body",
"body": res,
}
)


async def start_dummy_server(port):
Expand All @@ -69,7 +74,9 @@ async def timeit_coro(func, *args, repeat=1000, **kwargs):

async def performance_aiohttp(url, concurrency):
"""Test aiohttp performance."""
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit=concurrency)) as session:
async with aiohttp.ClientSession(
connector=aiohttp.TCPConnector(limit=concurrency)
) as session:
return await timeit_coro(session.get, url)


Expand Down Expand Up @@ -111,22 +118,41 @@ async def do_tests(url):
results["aiohttp"] = await performance_aiohttp(url, concurrency)
results["aiosonic"] = await performance_aiosonic(url, concurrency)
results["requests"] = timeit_requests(url, concurrency)
results["aiosonic_cyclic"] = await performance_aiosonic(url, concurrency, pool_cls=CyclicQueuePool)
results["aiosonic_cyclic"] = await performance_aiosonic(
url, concurrency, pool_cls=CyclicQueuePool
)

try:
results["httpx"] = await performance_httpx(url, concurrency)
except Exception as exc:
# results["httpx_error"] = str(exc)
print(f"httpx encountered an error: {exc}")

print(json.dumps({k: f"1000 requests in {v:.2f} ms" for k, v in results.items() if not k.endswith('_error')}, indent=4))
print(
json.dumps(
{
k: f"1000 requests in {v:.2f} ms"
for k, v in results.items()
if not k.endswith("_error")
},
indent=4,
)
)

if "httpx" in results:
print(f"aiosonic is {((results['httpx'] / results['aiosonic']) - 1) * 100:.2f}% faster than httpx")
print(
f"aiosonic is {((results['httpx'] / results['aiosonic']) - 1) * 100:.2f}% faster than httpx"
)

print(f"aiosonic is {((results['aiohttp'] / results['aiosonic']) - 1) * 100:.2f}% faster than aiohttp")
print(f"aiosonic is {((results['requests'] / results['aiosonic']) - 1) * 100:.2f}% faster than requests")
print(f"aiosonic is {((results['aiosonic_cyclic'] / results['aiosonic']) - 1) * 100:.2f}% faster than aiosonic cyclic")
print(
f"aiosonic is {((results['aiohttp'] / results['aiosonic']) - 1) * 100:.2f}% faster than aiohttp"
)
print(
f"aiosonic is {((results['requests'] / results['aiosonic']) - 1) * 100:.2f}% faster than requests"
)
print(
f"aiosonic is {((results['aiosonic_cyclic'] / results['aiosonic']) - 1) * 100:.2f}% faster than aiosonic cyclic"
)

return results

Expand Down Expand Up @@ -161,15 +187,14 @@ def main():

try:
res = asyncio.run(do_tests(url))

# Check if any results are valid and proceed
fastest_client = sorted(res.items(), key=lambda x: x[1])[0][0]
assert "aiosonic" in fastest_client

finally:
process.terminate()



if __name__ == "__main__":
main()

0 comments on commit 3eb023d

Please sign in to comment.