lbry-sdk/scripts/time_to_first_byte.py

93 lines
2.7 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 time
2018-12-19 20:46:47 +01:00
from aiohttp import ClientConnectorError
from lbrynet import conf
2019-01-11 21:01:56 +01:00
from lbrynet.schema.uri import parse_lbry_uri
from lbrynet.extras.daemon.DaemonConsole import LBRYAPIClient
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():
session = aiohttp.ClientSession()
2019-01-11 21:01:56 +01:00
try:
response = await session.get("https://api.lbry.io/file/list_homepage", timeout=10.0)
if response.status != 200:
print("API returned non 200 code!!")
return
body = await response.json()
await session.close()
uris = extract_uris(body['data']['Uris'])
return uris
finally:
2018-12-19 20:46:47 +01:00
await session.close()
2018-12-11 19:04:32 +01:00
2018-12-19 20:46:47 +01:00
async def main():
uris = await get_frontpage_uris()
2019-01-11 21:01:56 +01:00
print("got %i uris" % len(uris))
api = await LBRYAPIClient.get_client()
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()
print("Could not connect to daemon. Are you sure it's running?")
2019-01-11 21:01:56 +01:00
return
2018-12-11 19:04:32 +01:00
2019-01-11 21:01:56 +01:00
first_byte_times = []
2018-12-11 19:04:32 +01:00
for uri in uris:
2019-01-11 21:01:56 +01:00
await api.call(
"file_delete", {
"delete_from_download_dir": True,
"delete_all": True,
"claim_name": parse_lbry_uri(uri).name
}
)
for i, uri in enumerate(uris):
2018-12-11 19:04:32 +01:00
start = time.time()
2019-01-11 21:01:56 +01:00
try:
await api.call("get", {"uri": uri})
first_byte = time.time()
first_byte_times.append(first_byte - start)
print(f"{i + 1}/{len(uris)} - {first_byte - start} {uri}")
except:
print(f"{i + 1}/{len(uris)} - timed out in {time.time() - start} {uri}")
await api.call(
"file_delete", {
"delete_from_download_dir": True,
"claim_name": parse_lbry_uri(uri).name
}
)
avg = sum(first_byte_times) / len(first_byte_times)
2018-12-19 20:46:47 +01:00
print()
2019-01-11 21:01:56 +01:00
print(f"Average time to first byte: {avg} ({len(first_byte_times)} streams)")
print(f"Started {len(first_byte_times)} Timed out {len(uris) - len(first_byte_times)}")
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()
2019-01-11 21:01:56 +01:00
parser.add_argument("--data_dir", default=None)
parser.add_argument("--wallet_dir", default=None)
parser.add_argument("--download_directory", default=None)
2018-12-12 07:43:17 +01:00
args = parser.parse_args()
2019-01-11 21:01:56 +01:00
conf.initialize_settings(
data_dir=args.data_dir, wallet_dir=args.wallet_dir, download_dir=args.download_directory
)
asyncio.run(main())