lbry-sdk/scripts/time_to_first_byte.py

113 lines
3 KiB
Python
Raw Normal View History

2018-12-12 07:43:17 +01:00
import argparse
2018-12-19 20:46:47 +01:00
import asyncio
import aiohttp
2018-12-11 19:04:32 +01:00
import keyring
import time
2018-12-19 20:46:47 +01:00
from aiohttp import ClientConnectorError
from lbrynet import conf
from lbrynet.extras.daemon.auth.client import UnAuthAPIClient
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
def kill_loop():
loop = asyncio.get_event_loop()
loop.stop()
# loop.close()
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
def extract_uris(response):
uris = list()
for key in response:
for value in response[key]:
uris.append(value)
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
return uris
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
async def get_frontpage_uris():
kr = keyring.get_keyring()
c = kr.get_preferred_collection()
lbry_keyring = None
for col in c.get_all_items():
if col.get_label() == "LBRY/auth_token":
lbry_keyring = col
break
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
if lbry_keyring is None:
print("An auth token is needed to fetch the front page uris")
print("To generate the auth token, run the LBRY app at least once")
print("Then run the script again")
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
lbry_keyring = lbry_keyring.get_secret().decode("ascii")
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
session = aiohttp.ClientSession()
response = await session.get("https://api.lbry.io/file/list_homepage?auth_token={}".format(lbry_keyring))
if response.status != 200:
print("API returned non 200 code!!")
await session.close()
kill_loop()
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
body = await response.json()
await session.close()
uris = extract_uris(body['data']['Uris'])
return uris
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
async def main():
uris = await get_frontpage_uris()
api = await UnAuthAPIClient.from_url(conf.settings.get_api_connection_string())
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
try:
await api.status()
except (ClientConnectorError, ConnectionError):
await api.session.close()
kill_loop()
print("Could not connect to daemon. Are you sure it's running?")
return 1
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
results = dict()
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
# uris = ["what", "holi", "aweqwfq"]
_sum = 0
downloaded = len(uris)
2018-12-11 19:04:32 +01:00
for uri in uris:
start = time.time()
2018-12-19 20:46:47 +01:00
resp = await api.call("get", {"uri": uri})
2018-12-11 19:04:32 +01:00
end = time.time()
2018-12-19 20:46:47 +01:00
await api.call("file_delete", {"delete_from_download_dir": True,
"delete_all": True,
"claim_name": uri
})
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
time_taken = end - start
results[uri] = time_taken
_sum += time_taken
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
if resp.get('error'):
results[uri] = "Could not download"
downloaded -= 1
_sum -= time_taken
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
print(results[uri], uri)
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
avg = _sum / downloaded
print()
print("Average time taken:", avg)
print("Downloaded {} Not Downloaded {}".format(downloaded, len(uris) - downloaded))
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
await api.session.close()
2018-12-11 19:04:32 +01:00
if __name__ == "__main__":
2018-12-12 07:43:17 +01:00
parser = argparse.ArgumentParser()
2018-12-19 20:46:47 +01:00
parser.add_argument("--data_dir")
parser.add_argument("--wallet_dir")
parser.add_argument("--download_directory")
2018-12-12 07:43:17 +01:00
args = parser.parse_args()
2018-12-19 20:46:47 +01:00
conf.initialize_settings(data_dir=args.data_dir, wallet_dir=args.wallet_dir, download_dir=args.download_directory)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())