Skip to content

Commit 58180a2

Browse files
committed
add benchmarks for testing sync vs async performances
- `async.py` tests dbapi - `dbapi2_async.py` tests dbapi2
1 parent 18397e8 commit 58180a2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

async.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import os
3+
from time import perf_counter
4+
5+
from libsql_client import create_client
6+
7+
async def insert(client, t: str, s: str):
8+
return await client.execute(f"INSERT INTO {t} VALUES ('{s}')")
9+
10+
async def sync_demo(client):
11+
initial = perf_counter()
12+
for i in range(20):
13+
await insert(client, "sync", f"{i}")
14+
print(f"Sync code took {perf_counter() - initial} seconds to run.")
15+
16+
async def async_demo(client):
17+
initial = perf_counter()
18+
requests = [insert(client, "async", f"{i}") for i in range(20)]
19+
await asyncio.gather(*requests)
20+
print(f"Async code took {perf_counter() - initial} seconds to run.")
21+
22+
async def main():
23+
url = os.getenv("TURSO_URL", "")
24+
async with create_client(url) as client:
25+
await client.execute("CREATE TABLE IF NOT EXISTS sync (name TEXT)")
26+
await client.execute("CREATE TABLE IF NOT EXISTS async (name TEXT)")
27+
28+
await sync_demo(client)
29+
await async_demo(client)
30+
31+
32+
33+
asyncio.run(main())

dbapi2_async.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import os
3+
from libsql_client.dbapi2 import connect
4+
from time import perf_counter
5+
6+
async def insert(conn, t: str, s: str):
7+
return await conn.execute(f"INSERT INTO {t} VALUES ('{s}')")
8+
9+
async def sync_demo(conn):
10+
initial = perf_counter()
11+
for i in range(20):
12+
await insert(conn, "sync", f"{i}")
13+
await conn.commit()
14+
print(f"Sync code took {perf_counter() - initial} seconds to run.")
15+
16+
async def async_demo(conn):
17+
initial = perf_counter()
18+
requests = [insert(conn, "async", f"{i}") for i in range(20)]
19+
await asyncio.gather(*requests)
20+
await conn.commit()
21+
print(f"Async code took {perf_counter() - initial} seconds to run.")
22+
23+
async def main():
24+
url = os.getenv("TURSO_URL", "")
25+
conn = connect(url, uri=True)
26+
27+
await conn.execute("CREATE TABLE IF NOT EXISTS dbapi2_sync (t TEXT)")
28+
await conn.execute("CREATE TABLE IF NOT EXISTS dbapi2_async (t TEXT)")
29+
30+
await sync_demo(conn)
31+
await async_demo(conn)
32+
33+
asyncio.run(main())

0 commit comments

Comments
 (0)