From 7bdef3146b4ff43e20a046a4d469011dfa3c1e1b Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 23 Apr 2019 15:40:10 -0300 Subject: [PATCH] fix ttfb --- lbrynet/extras/cli.py | 8 ++++---- lbrynet/extras/daemon/client.py | 27 +++------------------------ scripts/time_to_first_byte.py | 9 +++++---- 3 files changed, 12 insertions(+), 32 deletions(-) diff --git a/lbrynet/extras/cli.py b/lbrynet/extras/cli.py index ba256e285..50ac2f04f 100644 --- a/lbrynet/extras/cli.py +++ b/lbrynet/extras/cli.py @@ -25,7 +25,7 @@ def display(data): print(json.dumps(data, indent=2)) -async def execute_command(conf, method, params): +async def execute_command(conf, method, params, callback=display): async with aiohttp.ClientSession() as session: try: message = {'method': method, 'params': params} @@ -33,12 +33,12 @@ async def execute_command(conf, method, params): try: data = await resp.json() if 'result' in data: - display(data['result']) + return callback(data['result']) elif 'error' in data: if 'message' in data['error']: - display(data['error']['message']) + return callback(data['error']['message']) else: - display(data['error']) + return callback(data['error']) except Exception as e: log.exception('Could not process response from server:', exc_info=e) except aiohttp.ClientConnectionError: diff --git a/lbrynet/extras/daemon/client.py b/lbrynet/extras/daemon/client.py index a9796d12f..8cac31482 100644 --- a/lbrynet/extras/daemon/client.py +++ b/lbrynet/extras/daemon/client.py @@ -1,27 +1,6 @@ -import aiohttp -import logging from lbrynet.conf import Config - -log = logging.getLogger(__name__) -USER_AGENT = "AuthServiceProxy/0.1" -TWISTED_SECURE_SESSION = "TWISTED_SECURE_SESSION" -TWISTED_SESSION = "TWISTED_SESSION" -LBRY_SECRET = "LBRY_SECRET" -HTTP_TIMEOUT = 30 +from lbrynet.extras.cli import execute_command -class JSONRPCException(Exception): - def __init__(self, rpc_error): - super().__init__() - self.error = rpc_error - - -async def daemon_rpc(conf: Config, method: str, *args, **kwargs): - async with aiohttp.ClientSession() as session: - message = {'method': method, 'params': [args, kwargs]} - async with session.get(conf.api_connection_url, json=message) as resp: - data = await resp.json() - if 'result' in data: - return data['result'] - elif 'error' in data: - raise JSONRPCException(data['error']) +def daemon_rpc(conf: Config, method: str, **kwargs): + return execute_command(conf, method, kwargs, callback=lambda data: data) diff --git a/scripts/time_to_first_byte.py b/scripts/time_to_first_byte.py index 712c6b22c..755e1ba50 100644 --- a/scripts/time_to_first_byte.py +++ b/scripts/time_to_first_byte.py @@ -90,15 +90,16 @@ async def main(uris=None, allow_fees=False): print("**********************************************") resolvable = [] - for name in uris: - resolved = await daemon_rpc(conf, 'resolve', name) + async def __resolve(name): + resolved = await daemon_rpc(conf, 'resolve', urls=[name]) if 'error' not in resolved.get(name, {}): - if ("fee" not in resolved[name]['claim']['value']['stream']) or allow_fees: + if ("fee" not in resolved[name]['claim']['value']) or allow_fees: resolvable.append(name) else: print(f"{name} has a fee, skipping it") else: print(f"failed to resolve {name}: {resolved[name]['error']}") + await asyncio.gather(*(__resolve(name) for name in uris)) print(f"attempting to download {len(resolvable)}/{len(uris)} frontpage streams") first_byte_times = [] @@ -113,7 +114,7 @@ async def main(uris=None, allow_fees=False): for i, uri in enumerate(resolvable): start = time.time() try: - await daemon_rpc(conf, 'get', uri) + await daemon_rpc(conf, 'get', uri=uri) first_byte = time.time() first_byte_times.append(first_byte - start) print(f"{i + 1}/{len(resolvable)} - {first_byte - start} {uri}")