-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: few basic microbenchmarks and structure around it
- Loading branch information
Showing
7 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from functools import lru_cache | ||
|
||
import frappe | ||
|
||
|
||
def bench_get_value_simple(): | ||
status = [] | ||
for role in get_all_roles(): | ||
status.append(frappe.db.get_value("Role", role, "disabled")) | ||
|
||
|
||
def bench_get_cached_value_simple(): | ||
status = [] | ||
for _ in range(10): | ||
for role in get_all_roles(): | ||
status.append(frappe.db.get_value("Role", role, "disabled", cache=True)) | ||
|
||
|
||
def bench_empty_transaction_cycling(): | ||
frappe.db.rollback() | ||
frappe.db.commit() | ||
|
||
|
||
def bench_get_single_value(): | ||
country = frappe.db.get_single_value("System Settings", "country", cache=False) | ||
# Requires Casting | ||
telemetry = frappe.db.get_single_value("System Settings", "enable_telemetry", cache=False) | ||
return country, telemetry | ||
|
||
|
||
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)) | ||
|
||
return results | ||
|
||
|
||
@lru_cache | ||
def get_all_roles(): | ||
return frappe.get_all("Role", order_by="creation asc", limit=10, pluck="name") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from functools import lru_cache | ||
|
||
import frappe | ||
|
||
|
||
def bench_get_doc(): | ||
return [frappe.get_doc("Role", r) for r in get_all_roles()] | ||
|
||
|
||
def bench_get_user(): | ||
"""Complex version of get_doc - involves child documents to init""" | ||
guest = frappe.get_doc("User", "Guest") | ||
admin = frappe.get_doc("User", "Administrator") | ||
return guest, admin | ||
|
||
|
||
def bench_get_cached_doc(): | ||
docs = [] | ||
for role in get_all_roles(): | ||
doctype = "Role" | ||
|
||
docs.append(frappe.get_cached_doc(doctype, role)) | ||
|
||
# Clear "local" cache to avoid testing basically nothing. | ||
frappe.local.cache.clear() | ||
return docs | ||
|
||
|
||
def bench_get_local_cached_doc(): | ||
docs = [] | ||
for role in get_all_roles(): | ||
doctype = "Role" | ||
docs.append(frappe.get_cached_doc(doctype, role)) | ||
return docs | ||
|
||
|
||
@lru_cache | ||
def get_all_roles(): | ||
return frappe.get_all("Role", order_by="creation asc", limit=10, pluck="name") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from functools import lru_cache | ||
|
||
import frappe | ||
|
||
|
||
def bench_make_key(): | ||
keys = [] | ||
for dt in get_all_doctypes(): | ||
keys.append(frappe.cache.make_key(dt)) | ||
return keys | ||
|
||
|
||
def bench_set_value(): | ||
for dt in get_all_doctypes(): | ||
key = f"_test_set_value:{dt}" | ||
frappe.cache.set_value(key, cached_get_doc(dt), expires_in_sec=30) | ||
assert frappe.cache.exists(key) | ||
assert frappe.cache.get_value(key).name == dt | ||
frappe.local.cache.clear() | ||
|
||
|
||
@lru_cache | ||
def get_all_doctypes(): | ||
return frappe.get_all("DocType", order_by="creation asc", limit=100, pluck="name") | ||
|
||
|
||
@lru_cache | ||
def cached_get_doc(doctype): | ||
return frappe.get_doc("DocType", doctype) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/env python3 | ||
|
||
import inspect | ||
import os | ||
from types import FunctionType | ||
|
||
import frappe | ||
import pyperf | ||
|
||
from caffeination.microbenchmarks import bench_database, bench_orm, bench_redis | ||
|
||
BENCHMARK_PREFIX = "bench_" | ||
BENCHMARK_SITE = os.environ.get("FRAPPE_BENCHMARK_SITE") or "bench.localhost" | ||
|
||
|
||
def run_microbenchmarks(): | ||
benchmarks = discover_benchmarks() | ||
|
||
frappe.init(BENCHMARK_SITE) | ||
frappe.connect() | ||
|
||
runner = pyperf.Runner() | ||
for name, func in benchmarks: | ||
runner.bench_func(name, func) | ||
|
||
frappe.destroy() | ||
|
||
|
||
def discover_benchmarks(): | ||
benchmark_modules = [ | ||
bench_orm, | ||
bench_database, | ||
bench_redis, | ||
] | ||
|
||
benchmarks = [] | ||
for module in benchmark_modules: | ||
module_name = module.__name__.split(".")[-1] | ||
for fn_name, fn in inspect.getmembers(module, predicate=lambda x: isinstance(x, FunctionType)): | ||
if fn_name.startswith(BENCHMARK_PREFIX): | ||
unique_name = f"{module_name}_{fn.__name__.removeprefix(BENCHMARK_PREFIX)}" | ||
benchmarks.append((unique_name, fn)) | ||
|
||
return sorted(benchmarks, key=lambda x: x[0]) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_microbenchmarks() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters