Skip to content

Commit

Permalink
bench: top web requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Dec 16, 2024
1 parent da54f7a commit adcef93
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
2 changes: 1 addition & 1 deletion caffeination/microbenchmarks/bench_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 0 additions & 15 deletions caffeination/microbenchmarks/bench_orm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from functools import lru_cache

import frappe
from frappe.desk.reportview import get, get_count


def bench_get_doc():
Expand Down Expand Up @@ -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)

Expand Down
96 changes: 92 additions & 4 deletions caffeination/microbenchmarks/bench_web_requests.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"

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()

0 comments on commit adcef93

Please sign in to comment.