2019-07-16 18:26:28 +02:00
|
|
|
import uvloop, asyncio, time, sys, logging
|
2019-07-12 01:20:58 +02:00
|
|
|
from concurrent.futures import ProcessPoolExecutor
|
2019-07-13 06:35:25 +02:00
|
|
|
from lbry.wallet.server.db import reader
|
2019-07-19 17:26:50 +02:00
|
|
|
from lbry.wallet.server.metrics import calculate_avg_percentiles
|
2019-07-12 01:20:58 +02:00
|
|
|
|
|
|
|
|
2019-07-19 01:21:48 +02:00
|
|
|
db_path = '../../../lbryconf/wallet-server/claims.db'
|
2019-07-16 18:26:28 +02:00
|
|
|
default_query_timout = 0.25
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.addHandler(logging.StreamHandler())
|
2019-07-12 01:20:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def run_times(executor, iterations, show=True):
|
2019-07-13 06:35:25 +02:00
|
|
|
start = time.perf_counter()
|
|
|
|
timings = await asyncio.gather(*(asyncio.get_running_loop().run_in_executor(
|
|
|
|
executor, reader.search_to_bytes, {
|
|
|
|
'no_totals': True,
|
|
|
|
'offset': 0,
|
|
|
|
'limit': 20,
|
|
|
|
'any_tags': [
|
2019-07-19 17:26:50 +02:00
|
|
|
'ufos', 'city fix'
|
2019-07-13 06:35:25 +02:00
|
|
|
],
|
|
|
|
'not_tags': [
|
2019-07-19 17:26:50 +02:00
|
|
|
'porn', 'mature', 'xxx', 'nsfw'
|
|
|
|
],
|
|
|
|
'order_by': [
|
|
|
|
'release_time'
|
2019-07-13 06:35:25 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
) for _ in range(iterations)))
|
2019-07-19 01:21:48 +02:00
|
|
|
timings = [r[1]['execute_query'][0]['total'] for r in timings]
|
2019-07-13 06:35:25 +02:00
|
|
|
total = int((time.perf_counter() - start) * 100)
|
2019-07-12 01:20:58 +02:00
|
|
|
if show:
|
2019-07-12 02:10:28 +02:00
|
|
|
avg = sum(timings)/len(timings)
|
2019-07-13 06:35:25 +02:00
|
|
|
print(f"{iterations:4}: {total}ms total concurrent, {len(timings)*avg*1000:.3f}s total sequential (avg*runs)")
|
|
|
|
print(f" {total/len(timings):.1f}ms/query concurrent (total/runs)")
|
|
|
|
print(f" {avg:.1f}ms/query actual average (sum(queries)/runs)")
|
2019-07-19 17:26:50 +02:00
|
|
|
stats = calculate_avg_percentiles(timings)
|
|
|
|
print(f" min: {stats[1]}, 5%: {stats[2]}, 25%: {stats[3]}, 50%: {stats[4]}, 75%: {stats[5]}, 95%: {stats[6]}, max: {stats[7]}")
|
2019-07-12 02:10:28 +02:00
|
|
|
sys.stdout.write(' sample:')
|
|
|
|
for i, t in zip(range(10), timings[::-1]):
|
2019-07-13 06:35:25 +02:00
|
|
|
sys.stdout.write(f' {t}ms')
|
2019-07-12 02:10:28 +02:00
|
|
|
print(' ...\n' if len(timings) > 10 else '\n')
|
2019-07-12 01:20:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2019-07-16 18:26:28 +02:00
|
|
|
executor = ProcessPoolExecutor(
|
2019-07-19 17:26:50 +02:00
|
|
|
4, initializer=reader.initializer, initargs=(log, db_path, 'mainnet', 1.0, True)
|
2019-07-16 18:26:28 +02:00
|
|
|
)
|
2019-07-19 17:26:50 +02:00
|
|
|
#await run_times(executor, 4, show=False)
|
|
|
|
#await run_times(executor, 1)
|
2019-07-12 02:10:28 +02:00
|
|
|
await run_times(executor, 2**3)
|
|
|
|
await run_times(executor, 2**5)
|
|
|
|
await run_times(executor, 2**7)
|
2019-07-19 17:26:50 +02:00
|
|
|
#await run_times(executor, 2**9)
|
2019-07-19 01:21:48 +02:00
|
|
|
#await run_times(executor, 2**11)
|
|
|
|
#await run_times(executor, 2**13)
|
2019-07-12 01:20:58 +02:00
|
|
|
executor.shutdown(True)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-07-12 02:10:28 +02:00
|
|
|
uvloop.install()
|
2019-07-12 01:20:58 +02:00
|
|
|
asyncio.run(main())
|