Skip to content

Commit

Permalink
bench: few more nano benches
Browse files Browse the repository at this point in the history
Covers:
- _dict
- rounding, casting numbers
  • Loading branch information
ankush committed Dec 19, 2024
1 parent ca89fe2 commit b3a09b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions caffeine/microbenchmarks/bench_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import random
import time

import frappe
from frappe.utils import flt
from frappe.utils.caching import redis_cache, request_cache, site_cache
from frappe.utils.data import cint

from caffeine.microbenchmarks.utils import NanoBenchmark

# NOTE: decorated functions themselves are benchmarks, since they aren't wrapped by any other
# function calls we don't need to move them to timeit style statements.
Expand Down Expand Up @@ -46,3 +51,45 @@ def bench_redis_cache_deco_without_local_cache():
def cache_in_redis(num):
time.sleep(0.001)
return num


bench_frappe_dict_getattr = NanoBenchmark("d.x", setup="d=frappe._dict(); d.x=1")
bench_frappe_dict_setattr = NanoBenchmark("d.x = 1", setup="d=frappe._dict();")


bench_flt_typical = NanoBenchmark(
"""flt(x, 2, rounding_method="Banker's Rounding")""",
setup="x = random.uniform(1, 10000)",
globals={"flt": flt, "random": random},
)

bench_flt_no_rounding = NanoBenchmark(
"flt(x)",
setup="x = random.uniform(1, 10000)",
globals={"flt": flt, "random": random},
)

bench_flt_str = NanoBenchmark(
"flt(x, 2)",
setup="x = str(random.uniform(1, 10000))",
globals={"flt": flt, "random": random},
)

bench_cint_on_float = NanoBenchmark(
"""cint(x)""",
setup="x = random.uniform(1, 10000)",
globals={"cint": cint, "random": random},
)

bench_cint_on_string = NanoBenchmark(
"""cint(x)""",
setup="x = str(random.randint(1, 10000))",
globals={"cint": cint, "random": random},
)


bench_cint_on_int = NanoBenchmark(
"""cint(x)""",
setup="x = random.randint(1, 10000)",
globals={"cint": cint, "random": random},
)
2 changes: 2 additions & 0 deletions caffeine/microbenchmarks/run_benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/env python3

import inspect
import random
from types import FunctionType

import frappe
Expand Down Expand Up @@ -64,6 +65,7 @@ def setup(site):
frappe.init(site)
assert frappe.conf.allow_tests
frappe.connect()
random.seed(42) # Ensure consistent results


def teardown(site):
Expand Down

0 comments on commit b3a09b8

Please sign in to comment.