Skip to content

Instantly share code, notes, and snippets.

@qnkhuat
Last active October 26, 2023 09:44
Show Gist options
  • Save qnkhuat/65d63319fa3d63e52b08c1afe17094f4 to your computer and use it in GitHub Desktop.
Save qnkhuat/65d63319fa3d63e52b08c1afe17094f4 to your computer and use it in GitHub Desktop.
import requests, time, argparse
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=100)
parser.add_argument("--host", type=str, default='http://localhost:3000', help="HOST")
args = parser.parse_args()
ORDER_QS_ID = None
PEOPLE_QS_ID = None
HOST = args.host
COOKIES = {}
CREATE_DASH_TIME = []
GET_DASH_TIME = []
UPDATE_DASHCARDS_TIME = []
UPDATE_DASHBOARD_TIME = []
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
def new_dashboard():
start = time.time()
resp = requests.post(f'{HOST}/api/dashboard', cookies=COOKIES, headers=headers,
json = {
'collection_id': None,
'description': None,
'name': 'new dashboard',
})
CREATE_DASH_TIME.append(time.time() - start)
return resp.json()["id"]
def get_dashboard(id):
start = time.time()
resp = requests.get(f"{HOST}/api/dashboard/{id}", cookies=COOKIES, headers=headers).json()
GET_DASH_TIME.append(time.time() - start)
def sign_in():
json_data = {
'username': 'crowberto@metabase.com',
'password': 'blackjet',
'remember': True,
}
resp = requests.post(f'{HOST}/api/session', headers=headers, json=json_data).json()
global COOKIES
COOKIES = {'metabase.SESSION': resp["id"]}
def update_dashboard(dashboard_id):
start = time.time()
json_data = {
'description': None,
'archived': False,
'collection_position': None,
'param_values': None,
'can_write': True,
'enable_embedding': False,
'collection_id': None,
'show_in_getting_started': False,
'name': 'new dashboard',
'caveats': None,
'collection_authority_level': None,
'creator_id': 3,
'made_public_by_id': None,
'embedding_params': None,
'cache_ttl': None,
'ordered_tabs': [],
'position': None,
'param_fields': None,
'parameters': [],
'auto_apply_filters': True,
'public_uuid': None,
'points_of_interest': None,
}
start = time.time()
resp = requests.put(f'{HOST}/api/dashboard/{dashboard_id}', cookies=COOKIES, headers=headers, json=json_data).json()
UPDATE_DASHBOARD_TIME.append(time.time() - start)
def update_dashboard_cards(dashboard_id):
json_data = {
'cards': [
{
'id': -1,
'card_id': ORDER_QS_ID,
'dashboard_tab_id': None,
'row': 0,
'col': 0,
'size_x': 12,
'size_y': 9,
'series': [],
'visualization_settings': {},
'parameter_mappings': [],
},
{
'id': -2,
'card_id': PEOPLE_QS_ID,
'dashboard_tab_id': None,
'row': 0,
'col': 12,
'size_x': 12,
'size_y': 9,
'series': [],
'visualization_settings': {},
'parameter_mappings': [],
},
{
'id': -3,
'card_id': PEOPLE_QS_ID,
'dashboard_tab_id': None,
'row': 4,
'col': 12,
'size_x': 12,
'size_y': 9,
'series': [],
'visualization_settings': {},
'parameter_mappings': [],
},
{
'id': -4,
'card_id': PEOPLE_QS_ID,
'dashboard_tab_id': None,
'row': 4,
'col': 12,
'size_x': 12,
'size_y': 9,
'series': [],
'visualization_settings': {},
'parameter_mappings': [],
}
],
'ordered_tabs': [],
}
start = time.time()
resp = requests.put(f'{HOST}/api/dashboard/{dashboard_id}/cards', cookies=COOKIES, headers=headers, json=json_data).json()
UPDATE_DASHCARDS_TIME.append(time.time() - start)
return None
def new_question(name, query):
json_data = {
'name': name,
'dataset_query': query,
'display': 'table',
'description': None,
'visualization_settings': {},
'collection_id': None,
'collection_position': None,
'result_metadata': None,
}
resp = requests.post(f'{HOST}/api/card', cookies=COOKIES, headers=headers, json=json_data).json()
return resp["id"]
def setup():
global ORDER_QS_ID
global PEOPLE_QS_ID
ORDER_QS_ID = new_question("ORDERS", {
'database': 1,
'type': 'query',
'query': {
'source-table': 2,
},
})
PEOPLE_QS_ID = new_question("ORDERS", {
'database': 1,
'type': 'query',
'query': {
'source-table': 5,
},
})
def report(n):
print(f"N={n}")
print(f"Avg GET /api/dashboard/:id : {sum(GET_DASH_TIME) / len(GET_DASH_TIME) * 1000}ms")
print(f"Avg POST /api/dashboard : {sum(CREATE_DASH_TIME) / len(CREATE_DASH_TIME) * 1000}ms")
print(f"Avg PUT /api/dashboard/:id : {sum(UPDATE_DASHBOARD_TIME) / len(UPDATE_DASHBOARD_TIME) * 1000}ms")
print(f"Avg PUT /api/dashboard/:id/cards : {sum(UPDATE_DASHCARDS_TIME) / len(UPDATE_DASHCARDS_TIME) * 1000}ms")
def bench(n):
sign_in()
setup()
for _ in range(n):
dash_id = new_dashboard()
get_dashboard(dash_id)
update_dashboard_cards(dash_id)
update_dashboard(dash_id)
report(n)
bench(args.n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment