| 1 |
# Copyright Amethyst Reese |
| 2 |
# Licensed under the MIT license |
| 3 |
|
| 4 |
""" |
| 5 |
Simple perf tests for aiosqlite and the asyncio run loop. |
| 6 |
""" |
| 7 |
import string |
| 8 |
import tempfile |
| 9 |
import time |
| 10 |
|
| 11 |
from unittest import IsolatedAsyncioTestCase as TestCase |
| 12 |
|
| 13 |
import aiosqlite |
| 14 |
from .smoke import setup_logger |
| 15 |
|
| 16 |
TEST_DB = ":memory:" |
| 17 |
TARGET = 2.0 |
| 18 |
RESULTS = {} |
| 19 |
|
| 20 |
|
| 21 |
def timed(fn, name=None): |
| 22 |
""" |
| 23 |
Decorator for perf testing a block of async code. |
| 24 |
|
| 25 |
Expects the wrapped function to return an async generator. |
| 26 |
The generator should do setup, then yield when ready to start perf testing. |
| 27 |
The decorator will then pump the generator repeatedly until the target |
| 28 |
time has been reached, then close the generator and print perf results. |
| 29 |
""" |
| 30 |
|
| 31 |
name = name or fn.__name__ |
| 32 |
|
| 33 |
async def wrapper(*args, **kwargs): |
| 34 |
gen = fn(*args, **kwargs) |
| 35 |
|
| 36 |
await gen.asend(None) |
| 37 |
count = 0 |
| 38 |
before = time.time() |
| 39 |
|
| 40 |
while True: |
| 41 |
count += 1 |
| 42 |
value = time.time() - before < TARGET |
| 43 |
try: |
| 44 |
if value: |
| 45 |
await gen.asend(value) |
| 46 |
else: |
| 47 |
await gen.aclose() |
| 48 |
break |
| 49 |
|
| 50 |
except StopAsyncIteration: |
| 51 |
break |
| 52 |
|
| 53 |
except Exception as e: |
| 54 |
print(f"exception occurred: {e}") |
| 55 |
return |
| 56 |
|
| 57 |
duration = time.time() - before |
| 58 |
|
| 59 |
RESULTS[name] = (count, duration) |
| 60 |
|
| 61 |
return wrapper |
| 62 |
|
| 63 |
|
| 64 |
class PerfTest(TestCase): |
| 65 |
@classmethod |
| 66 |
def setUpClass(cls): |
| 67 |
print(f"Running perf tests for at least {TARGET:.1f}s each...") |
| 68 |
setup_logger() |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def tearDownClass(cls): |
| 72 |
print(f"\n{'Perf Test':<25} Iterations Duration {'Rate':>11}") |
| 73 |
for name in sorted(RESULTS): |
| 74 |
count, duration = RESULTS[name] |
| 75 |
rate = count / duration |
| 76 |
name = name.replace("test_", "") |
| 77 |
print(f"{name:<25} {count:>10} {duration:>7.1f}s {rate:>9.1f}/s") |
| 78 |
|
| 79 |
@timed |
| 80 |
async def test_connection_memory(self): |
| 81 |
while True: |
| 82 |
yield |
| 83 |
async with aiosqlite.connect(TEST_DB): |
| 84 |
pass |
| 85 |
|
| 86 |
@timed |
| 87 |
async def test_connection_file(self): |
| 88 |
with tempfile.NamedTemporaryFile(delete=False) as tf: |
| 89 |
path = tf.name |
| 90 |
tf.close() |
| 91 |
|
| 92 |
async with aiosqlite.connect(path) as db: |
| 93 |
await db.execute( |
| 94 |
"create table perf (i integer primary key asc, k integer)" |
| 95 |
) |
| 96 |
await db.execute("insert into perf (k) values (2), (3)") |
| 97 |
await db.commit() |
| 98 |
|
| 99 |
while True: |
| 100 |
yield |
| 101 |
async with aiosqlite.connect(path): |
| 102 |
pass |
| 103 |
|
| 104 |
@timed |
| 105 |
async def test_atomics(self): |
| 106 |
async with aiosqlite.connect(TEST_DB) as db: |
| 107 |
await db.execute("create table perf (i integer primary key asc, k integer)") |
| 108 |
await db.execute("insert into perf (k) values (2), (3)") |
| 109 |
await db.commit() |
| 110 |
|
| 111 |
while True: |
| 112 |
yield |
| 113 |
async with db.execute("select last_insert_rowid()") as cursor: |
| 114 |
await cursor.fetchone() |
| 115 |
|
| 116 |
@timed |
| 117 |
async def test_inserts(self): |
| 118 |
async with aiosqlite.connect(TEST_DB) as db: |
| 119 |
await db.execute("create table perf (i integer primary key asc, k integer)") |
| 120 |
await db.commit() |
| 121 |
|
| 122 |
while True: |
| 123 |
yield |
| 124 |
await db.execute("insert into perf (k) values (1), (2), (3)") |
| 125 |
await db.commit() |
| 126 |
|
| 127 |
@timed |
| 128 |
async def test_insert_ids(self): |
| 129 |
async with aiosqlite.connect(TEST_DB) as db: |
| 130 |
await db.execute("create table perf (i integer primary key asc, k integer)") |
| 131 |
await db.commit() |
| 132 |
|
| 133 |
while True: |
| 134 |
yield |
| 135 |
cursor = await db.execute("insert into perf (k) values (1)") |
| 136 |
await cursor.execute("select last_insert_rowid()") |
| 137 |
await cursor.fetchone() |
| 138 |
await db.commit() |
| 139 |
|
| 140 |
@timed |
| 141 |
async def test_insert_macro_ids(self): |
| 142 |
async with aiosqlite.connect(TEST_DB) as db: |
| 143 |
await db.execute("create table perf (i integer primary key asc, k integer)") |
| 144 |
await db.commit() |
| 145 |
|
| 146 |
while True: |
| 147 |
yield |
| 148 |
await db.execute_insert("insert into perf (k) values (1)") |
| 149 |
await db.commit() |
| 150 |
|
| 151 |
@timed |
| 152 |
async def test_select(self): |
| 153 |
async with aiosqlite.connect(TEST_DB) as db: |
| 154 |
await db.execute("create table perf (i integer primary key asc, k integer)") |
| 155 |
for i in range(100): |
| 156 |
await db.execute("insert into perf (k) values (%d)" % (i,)) |
| 157 |
await db.commit() |
| 158 |
|
| 159 |
while True: |
| 160 |
yield |
| 161 |
cursor = await db.execute("select i, k from perf") |
| 162 |
assert len(await cursor.fetchall()) == 100 |
| 163 |
|
| 164 |
@timed |
| 165 |
async def test_select_macro(self): |
| 166 |
async with aiosqlite.connect(TEST_DB) as db: |
| 167 |
await db.execute("create table perf (i integer primary key asc, k integer)") |
| 168 |
for i in range(100): |
| 169 |
await db.execute("insert into perf (k) values (%d)" % (i,)) |
| 170 |
await db.commit() |
| 171 |
|
| 172 |
while True: |
| 173 |
yield |
| 174 |
assert len(await db.execute_fetchall("select i, k from perf")) == 100 |
| 175 |
|
| 176 |
async def test_iterable_cursor_perf(self): |
| 177 |
async with aiosqlite.connect(TEST_DB) as db: |
| 178 |
await db.execute( |
| 179 |
"create table ic_perf (" |
| 180 |
"i integer primary key asc, k integer, a integer, b integer, c char(16))" |
| 181 |
) |
| 182 |
for batch in range(128): # add 128k rows |
| 183 |
r_start = batch * 1024 |
| 184 |
await db.executemany( |
| 185 |
"insert into ic_perf (k, a, b, c) values(?, 1, 2, ?)", |
| 186 |
[ |
| 187 |
*[ |
| 188 |
(i, string.ascii_lowercase) |
| 189 |
for i in range(r_start, r_start + 1024) |
| 190 |
] |
| 191 |
], |
| 192 |
) |
| 193 |
await db.commit() |
| 194 |
|
| 195 |
async def test_perf(chunk_size: int): |
| 196 |
while True: |
| 197 |
async with db.execute("SELECT * FROM ic_perf") as cursor: |
| 198 |
cursor.iter_chunk_size = chunk_size |
| 199 |
async for _ in cursor: |
| 200 |
yield |
| 201 |
|
| 202 |
for chunk_size in [2**i for i in range(4, 11)]: |
| 203 |
await timed(test_perf, f"iterable_cursor @ {chunk_size}")(chunk_size) |