Skip to content

Commit

Permalink
fix: migrate smaller benchmarks to NanoBenchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Dec 19, 2024
1 parent 69003c6 commit aba3a90
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
2 changes: 2 additions & 0 deletions caffeine/microbenchmarks/bench_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import frappe

from caffeine.microbenchmarks.utils import NanoBenchmark


def bench_get_value_simple():
status = []
Expand Down
23 changes: 11 additions & 12 deletions caffeine/microbenchmarks/bench_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import frappe

from caffeine.microbenchmarks.utils import NanoBenchmark


def bench_get_doc():
return [frappe.get_doc("Role", r) for r in get_all_roles()]
Expand Down Expand Up @@ -34,26 +36,23 @@ 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)
bench_get_all = NanoBenchmark('frappe.get_all("DocField", "*", limit=1, run=0)')

bench_get_list = NanoBenchmark('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)

bench_get_all_with_filters = NanoBenchmark(
'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(
bench_get_all_with_many_fields = NanoBenchmark(
"""frappe.get_all(
"Role",
{"creation": (">", "2020-01-01 00:00:00")},
["disabled", "name", "creation", "modified"],
limit=10,
run=0,
)
run=0)"""
)


@lru_cache
Expand Down
53 changes: 30 additions & 23 deletions caffeine/microbenchmarks/bench_qb.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
import textwrap

import frappe

from caffeine.microbenchmarks.utils import NanoBenchmark

def bench_qb_select_star():
table = frappe.qb.DocType("Role")
return frappe.qb.from_(table).select("*").limit(20).run(run=0)
bench_qb_select_star = NanoBenchmark(
'frappe.qb.from_(table).select("*").limit(20).run(run=0)',
setup='table = frappe.qb.DocType("Role")',
)


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)
bench_qb_select_multiple_fields = NanoBenchmark(
"frappe.qb.from_(table).select(table.name, table.creation, table.modified).limit(20).run(run=0)",
setup='table = frappe.qb.DocType("Role")',
)


def bench_qb_get_query():
return frappe.qb.get_query(
bench_qb_get_query = NanoBenchmark(
"""frappe.qb.get_query(
"Role",
filters={"creation": (">", "2020-01-01 00:00:00")},
fields="disabled",
limit=10,
order_by="creation asc",
).run(run=0)

).run(run=0)"""
)

def bench_qb_get_query_multiple_fields():
return frappe.qb.get_query(
bench_qb_get_query_multiple_fields = NanoBenchmark(
"""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)
).run(run=0)"""
)


bench_qb_simple_get_query = NanoBenchmark(
"""frappe.qb.get_query(
"Role",
filters={"name": "Guest"},
fields="*",
limit=1,
order_by="creation asc",
).run(run=0)"""
)
2 changes: 1 addition & 1 deletion caffeine/microbenchmarks/bench_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from caffeine.microbenchmarks.bench_orm import get_all_roles
from caffeine.microbenchmarks.utils import NanoBenchmark

bench_make_key = NanoBenchmark(statement="frappe.cache.make_key('a')")
bench_make_key = NanoBenchmark("frappe.cache.make_key('a')")


def bench_redis_get_set_delete_cycle():
Expand Down
5 changes: 5 additions & 0 deletions caffeine/microbenchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

@dataclass
class NanoBenchmark:
"""This class can be used to represent benchmarks that measure sub-milisecond operations.
These measurement are affected by function call overhead, so instead directly executing the
statement avoids it."""

statement: str
setup: str = "pass"
teardown: str = "pass"
Expand Down

0 comments on commit aba3a90

Please sign in to comment.