From 634e490cff3b12b0a25b740552065a472777745c Mon Sep 17 00:00:00 2001 From: Daoud Clarke Date: Sun, 11 Apr 2021 15:10:02 +0100 Subject: [PATCH] Add a script for performance testing --- make_curl.py | 29 +++++++++++++++++++++++++++++ performance.py | 10 +++++----- 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 make_curl.py diff --git a/make_curl.py b/make_curl.py new file mode 100644 index 0000000..f7e7ed5 --- /dev/null +++ b/make_curl.py @@ -0,0 +1,29 @@ +""" +Make a curl script for testing performance +""" +import os +from itertools import islice +from urllib.parse import quote + +from paths import DATA_DIR +from wiki import get_wiki_titles_and_urls + +URL_TEMPLATE = "http://localhost:8000/complete?q={}" +CURL_FILE = os.path.join(DATA_DIR, "urls.curl") + + +def get_urls(): + titles_and_urls = get_wiki_titles_and_urls() + for title, url in islice(titles_and_urls, 100): + query = quote(title.lower()) + yield URL_TEMPLATE.format(query) + + +def run(): + with open(CURL_FILE, 'wt') as output_file: + for url in get_urls(): + output_file.write(f'url="{url}"\n') + + +if __name__ == '__main__': + run() diff --git a/performance.py b/performance.py index a16e2f7..9ab5f2e 100644 --- a/performance.py +++ b/performance.py @@ -21,15 +21,15 @@ NUM_PAGES = 500 def query_test(): titles_and_urls = get_wiki_titles_and_urls() - # client = TestClient(app) + client = TestClient(app) start = datetime.now() hits = 0 for title, url in islice(titles_and_urls, NUM_PAGES): - # result = client.get('/complete', params={'q': title}) - # assert result.status_code == 200 - # data = result.content.decode('utf8') - data = json.dumps(complete(title)) + result = client.get('/complete', params={'q': title}) + assert result.status_code == 200 + data = result.content.decode('utf8') + # data = json.dumps(complete(title)) if url in data: hits += 1