sqlite performance script measures sql time inside each process individually

This commit is contained in:
Lex Berezhny 2019-07-11 20:10:28 -04:00
parent b81c801f2d
commit c5aeac6898

View file

@ -1,4 +1,4 @@
import asyncio, sqlite3, time
import uvloop, asyncio, sqlite3, time, sys
from concurrent.futures import ProcessPoolExecutor
from contextvars import ContextVar
@ -14,7 +14,7 @@ def init():
def reader():
conn = db.get()
for _ in range(1):
start = time.time()
conn.execute("""
SELECT
claimtrie.claim_hash as is_controlling,
@ -45,28 +45,39 @@ def reader():
ORDER BY claim.height DESC, claim.normalized ASC
LIMIT 20 OFFSET 100
""").fetchall()
elapsed = time.time() - start
return elapsed
async def run_times(executor, iterations, show=True):
start = time.time()
await asyncio.gather(*(asyncio.get_running_loop().run_in_executor(executor, reader) for _ in range(iterations)))
elapsed = time.time() - start
timings = await asyncio.gather(*(
asyncio.get_running_loop().run_in_executor(executor, reader) for _ in range(iterations)
))
total = time.time() - start
if show:
print(f"{iterations:3}: {elapsed:.5f}ms total, {elapsed/iterations:.5f}ms/query")
avg = sum(timings)/len(timings)
print(f"{iterations:4}: {total:.5f}ms total concurrent, {len(timings)*avg:.5f}ms total sequential (avg*runs)")
print(f" {total/len(timings):.5f}ms/query concurrent (total/runs)")
print(f" {avg:.5f}ms/query actual average (sum(queries)/runs)")
sys.stdout.write(' sample:')
for i, t in zip(range(10), timings[::-1]):
sys.stdout.write(f' {t:.5f}ms')
print(' ...\n' if len(timings) > 10 else '\n')
async def main():
executor = ProcessPoolExecutor(4, initializer=init)
await run_times(executor, 4, show=False)
await run_times(executor, 1)
await run_times(executor, 4)
await run_times(executor, 8)
await run_times(executor, 16)
await run_times(executor, 32)
await run_times(executor, 64)
await run_times(executor, 128)
await run_times(executor, 256)
await run_times(executor, 2**3)
await run_times(executor, 2**5)
await run_times(executor, 2**7)
await run_times(executor, 2**9)
await run_times(executor, 2**11)
await run_times(executor, 2**13)
executor.shutdown(True)
if __name__ == '__main__':
uvloop.install()
asyncio.run(main())