From f696f23d82c292f095ea87792da49824910cdddd Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 16 Dec 2024 15:49:59 +0530 Subject: [PATCH] bench: top web requests --- .../microbenchmarks/bench_database.py | 2 +- caffeination/microbenchmarks/bench_orm.py | 15 --- .../microbenchmarks/bench_web_requests.py | 96 ++++++++++++++++++- 3 files changed, 93 insertions(+), 20 deletions(-) diff --git a/caffeination/microbenchmarks/bench_database.py b/caffeination/microbenchmarks/bench_database.py index 8dd1437..c78b532 100644 --- a/caffeination/microbenchmarks/bench_database.py +++ b/caffeination/microbenchmarks/bench_database.py @@ -48,7 +48,7 @@ def bench_select_star(): kwargs = [{}, {"as_list": True}, {"as_dict": True}] results = [] for kw in kwargs: - results.append(frappe.db.sql("select * from tabRole limit 10", kw)) + results.append(frappe.db.sql("select * from tabRole order by creation limit 10", kw)) return results diff --git a/caffeination/microbenchmarks/bench_orm.py b/caffeination/microbenchmarks/bench_orm.py index 7e45ed4..4865ed6 100644 --- a/caffeination/microbenchmarks/bench_orm.py +++ b/caffeination/microbenchmarks/bench_orm.py @@ -1,7 +1,6 @@ from functools import lru_cache import frappe -from frappe.desk.reportview import get, get_count def bench_get_doc(): @@ -43,20 +42,6 @@ def bench_get_list(): return frappe.get_list("Role", "*", limit=20, run=0) -def bench_list_view_query(): - frappe.local.form_dict = { - "doctype": "Role", - "fields": '["`tabRole`.`name`","`tabRole`.`owner`","`tabRole`.`creation`","`tabRole`.`modified`","`tabRole`.`modified_by`" ,"`tabRole`.`_user_tags`","`tabRole`.`_comments`","`tabRole`.`_assign`","`tabRole`.`_liked_by`","`tabRole`.`docstatus`","`tabRole`.`idx`","`tabRole`.`disabled`"]', - "filters": "[]", - "order_by": "`tabRole`.creation desc", - "start": "0", - "page_length": "20", - "group_by": "", - "with_comment_count": "1", - } - return get() - - def bench_get_all_with_filters(): return frappe.get_all("Role", {"creation": (">", "2020-01-01 00:00:00")}, "disabled", limit=10, run=0) diff --git a/caffeination/microbenchmarks/bench_web_requests.py b/caffeination/microbenchmarks/bench_web_requests.py index c2d9908..839662d 100644 --- a/caffeination/microbenchmarks/bench_web_requests.py +++ b/caffeination/microbenchmarks/bench_web_requests.py @@ -1,17 +1,105 @@ +import json from functools import lru_cache import frappe from frappe.app import application as _trigger_imports from frappe.utils import get_test_client +from frappe.utils.user import AUTOMATIC_ROLES +TEST_USER = "test@example.com" -def bench_request_overheads(): + +def request(method, path, data=None, auth=False): client = get_test_client() - for _ in range(100): - resp = client.get("/api/method/ping", headers={"X-Frappe-Site-Name": get_site()}) - assert resp.status_code == 200 + headers = {"X-Frappe-Site-Name": get_site()} + if auth: + sid = get_sid() + client.set_cookie("sid", sid) + return client.open(path, headers=headers, method=method, data=data) + + +def bench_request_overheads(): + resp = request("GET", "/api/method/ping") + assert resp.status_code == 200 + + +def bench_request_authed_overheads(): + resp = request("GET", "/api/method/ping", auth=True) + assert resp.status_code == 200 + + +def bench_request_getdoc(): + resp = request( + "POST", + "/api/method/frappe.desk.form.load.getdoc", + data={"doctype": "Role", "name": "Guest"}, + auth=True, + ) + assert resp.status_code == 200 + + +def bench_list_view_count_query(): + resp = request( + "POST", + "/api/method/frappe.desk.reportview.get_count", + data={"doctype": "Role", "filters": "[]", "fields": "[]", "distinct": "false", "limit": "1001"}, + auth=True, + ) + assert resp.status_code == 200 + + +def bench_list_view_query(): + reportview_get_payload = { + "doctype": "Role", + "fields": '["`tabRole`.`name`","`tabRole`.`owner`","`tabRole`.`creation`","`tabRole`.`modified`","`tabRole`.`modified_by`" ,"`tabRole`.`_user_tags`","`tabRole`.`_comments`","`tabRole`.`_assign`","`tabRole`.`_liked_by`","`tabRole`.`docstatus`","`tabRole`.`idx`","`tabRole`.`disabled`"]', + "filters": "[]", + "order_by": "`tabRole`.creation desc", + "start": "0", + "page_length": "20", + "group_by": "", + "with_comment_count": "1", + } + resp = request("POST", "/api/method/frappe.desk.reportview.get", data=reportview_get_payload, auth=True) + assert resp.status_code == 200 @lru_cache def get_site(): return frappe.local.site + + +@lru_cache +def get_sid(): + from frappe.auth import CookieManager, LoginManager + from frappe.utils import set_request + + create_test_user(TEST_USER) + + set_request(path="/") + frappe.local.cookie_manager = CookieManager() + frappe.local.login_manager = LoginManager() + frappe.local.login_manager.login_as(TEST_USER) + frappe.db.commit() + return frappe.session.sid + + +def create_test_user(name): + if frappe.db.exists("User", name): + return + + user = frappe.new_doc("User") + user.email = name + user.first_name = "Frappe" + user.new_password = frappe.local.conf.admin_password + user.send_welcome_email = 0 + user.time_zone = "Asia/Kolkata" + user.flags.ignore_password_policy = True + user.insert(ignore_if_duplicate=True) + + user.reload() + + all_roles = set(frappe.get_all("Role", pluck="name")) + for role in all_roles - set(AUTOMATIC_ROLES): + user.append("roles", {"role": role}) + user.save() + frappe.db.commit()