From 9af5fcc6ece98777d15fc6c15e8661228871276b Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 16 Dec 2024 14:19:46 +0530 Subject: [PATCH] test: more DB/ORM/QB benchmarks --- .../microbenchmarks/bench_database.py | 12 ++++++ caffeination/microbenchmarks/bench_orm.py | 22 ++++++++++ caffeination/microbenchmarks/bench_qb.py | 41 +++++++++++++++++++ .../microbenchmarks/run_benchmarks.py | 10 ++++- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 caffeination/microbenchmarks/bench_qb.py diff --git a/caffeination/microbenchmarks/bench_database.py b/caffeination/microbenchmarks/bench_database.py index ff45081..3e0a0fd 100644 --- a/caffeination/microbenchmarks/bench_database.py +++ b/caffeination/microbenchmarks/bench_database.py @@ -9,6 +9,14 @@ def bench_get_value_simple(): status.append(frappe.db.get_value("Role", role, "disabled")) +def bench_get_value_with_dict_filters(): + return frappe.db.get_value("Role", {"creation": (">", "2020-01-01 00:00:00")}, "disabled") + + +def bench_get_value_with_list_filters(): + return frappe.db.get_value("Role", ["creation", ">", "2020-01-01 00:00:00"], "*") + + def bench_get_cached_value_simple(): status = [] for _ in range(10): @@ -37,6 +45,10 @@ def bench_select_star(): return results +def bench_sql_select_many_rows(): + return frappe.db.sql("select * from `tabDocField` order by creation limit 1000") + + @lru_cache def get_all_roles(): return frappe.get_all("Role", order_by="creation asc", limit=10, pluck="name") diff --git a/caffeination/microbenchmarks/bench_orm.py b/caffeination/microbenchmarks/bench_orm.py index 94eff19..4865ed6 100644 --- a/caffeination/microbenchmarks/bench_orm.py +++ b/caffeination/microbenchmarks/bench_orm.py @@ -34,6 +34,28 @@ def bench_get_local_cached_doc(): return docs +def bench_get_all(): + return frappe.get_all("DocField", "*", limit=1, run=0) + + +def bench_get_list(): + return frappe.get_list("Role", "*", limit=20, run=0) + + +def bench_get_all_with_filters(): + return frappe.get_all("Role", {"creation": (">", "2020-01-01 00:00:00")}, "disabled", limit=10, run=0) + + +def bench_get_all_with_many_fields(): + return frappe.get_all( + "Role", + {"creation": (">", "2020-01-01 00:00:00")}, + ["disabled", "name", "creation", "modified"], + limit=10, + run=0, + ) + + @lru_cache def get_all_roles(): return frappe.get_all("Role", order_by="creation asc", limit=10, pluck="name") diff --git a/caffeination/microbenchmarks/bench_qb.py b/caffeination/microbenchmarks/bench_qb.py new file mode 100644 index 0000000..add2ac8 --- /dev/null +++ b/caffeination/microbenchmarks/bench_qb.py @@ -0,0 +1,41 @@ +import frappe + + +def bench_qb_select_star(): + table = frappe.qb.DocType("Role") + return frappe.qb.from_(table).select("*").limit(20).run(run=0) + + +def bench_qb_select_star_multiple_fields(): + table = frappe.qb.DocType("Role") + return frappe.qb.from_(table).select(table.name, table.creation, table.modified).limit(20).run(run=0) + + +def bench_qb_get_query(): + return frappe.qb.get_query( + "Role", + filters={"creation": (">", "2020-01-01 00:00:00")}, + fields="disabled", + limit=10, + order_by="creation asc", + ).run(run=0) + + +def bench_qb_get_query_multiple_fields(): + return frappe.qb.get_query( + "Role", + filters={"creation": (">", "2020-01-01 00:00:00")}, + fields=["disabled", "name", "creation", "modified"], + limit=10, + order_by="creation asc", + ).run(run=0) + + +def bench_qb_simple_get_query(): + return frappe.qb.get_query( + "Role", + filters={"name": "Guest"}, + fields="*", + limit=1, + order_by="creation asc", + ).run(run=0) diff --git a/caffeination/microbenchmarks/run_benchmarks.py b/caffeination/microbenchmarks/run_benchmarks.py index ba9f23a..b978856 100755 --- a/caffeination/microbenchmarks/run_benchmarks.py +++ b/caffeination/microbenchmarks/run_benchmarks.py @@ -11,6 +11,7 @@ bench_background_jobs, bench_database, bench_orm, + bench_qb, bench_redis, bench_web_requests, ) @@ -54,7 +55,14 @@ def teardown(site): def discover_benchmarks(benchmark_filter): - benchmark_modules = [bench_orm, bench_database, bench_redis, bench_background_jobs, bench_web_requests] + benchmark_modules = [ + bench_orm, + bench_database, + bench_redis, + bench_background_jobs, + bench_web_requests, + bench_qb, + ] benchmarks = [] for module in benchmark_modules: