#! /usr/bin/env python import sys import requests import time import random my_arids = [ 176, 31, 857, 524, 545, 578, 865, 1033, 620, 629, 642, 989, 4265, 3597, 2820, 19793, 574, 56592, 37233, 44693, 32050, 825, 1160, 18950, 15645, 22399, 61628, 66321, 84978, 84979, 54140, 69200, 115479, 69658, ] NUM_ITERATIONS = 2 class Tester: def __init__(self, token): self.token = token def make_authorised_request(self, method, url, params=None, **kwargs): kwargs.setdefault("allow_redirects", True) headers = { "Authorization": "Bearer2 {}".format(self.token), "Content-Type": "application/json", "Accept": "*/*" } return requests.request(method, 'https://api2.whtlst.in' + url, params=params, headers=headers, **kwargs) def cache_percent(self, percent): self.make_authorised_request('delete', '/api/artist_manual_cache', json={'source': 'a', 'ids':my_arids}) if percent > 0: if percent >= 1: arids_to_cache = my_arids else: num_arids = int(len(my_arids) * percent) arids_to_cache = random.choices(my_arids, k=num_arids) self.make_authorised_request('post', '/api/artist_manual_cache', json={'source': 'a', 'ids':arids_to_cache}) def do_whitelist_calls(self, use_cache=True, use_stream=False): elapsed_time = 0 for i in range(NUM_ITERATIONS): start_time = time.time() res = self.make_authorised_request('get', '/api/my_whitelist', params={'use_cache': 1} if use_cache else {}, stream=use_stream) elapsed_time += (time.time() - start_time) print(i, "done", res.status_code, elapsed_time) data = res.json() arids = ["{}:{}".format(a['artist']['query_key'], a['artist'].get('cache_status')) for a in data['artists']] # print(len(arids)) # print(", ".join(arids)) time.sleep(2) return (elapsed_time) / float(NUM_ITERATIONS) * 1000.0 if __name__ == "__main__": print("Testing perf ") try: token = sys.argv[1] except: print( "Supply a JWT. To get it, login to web.whtlst.in, and copy it from your " "localstore ...accessToken" ) sys.exit(5) print("\nStarting...\n\n10 repeated calls at 0%, 10%, 25%, 75%, and 100% cached") tester = Tester(token) import pdb; pdb.set_trace() tester.cache_percent(0) average_millis = tester.do_whitelist_calls(use_cache=False) print("--- 0%, {}".format(average_millis)) average_millis = tester.do_whitelist_calls(use_cache=True) print("--- 0% CACHED, {}".format(average_millis)) tester.cache_percent(1.0) average_millis = tester.do_whitelist_calls(use_cache=True) print("--- 100%, {}".format(average_millis)) average_millis = tester.do_whitelist_calls(use_cache=True, use_stream=True) print("--- 100% STREAM, {}".format(average_millis))