Fix performance issue for users with many orders

This commit is contained in:
2026-05-24 12:50:52 +02:00
parent 7856123bc0
commit 3591e0f33a
6 changed files with 136 additions and 22 deletions

View File

@@ -30,3 +30,78 @@ def get_jinja_renderer() -> Jinja2Templates:
renderer.env.globals["production_mode"] = "APS_PRODUCTION_MODE" in os.environ
return renderer
def timer_context(name: str):
import time
class Timer:
def __enter__(self):
self.start = time.perf_counter()
def __exit__(self, exc_type, exc_val, exc_tb):
end = time.perf_counter()
print(f"{name} took {end - self.start:.4f} seconds")
return Timer()
import cProfile, pstats, io
from contextlib import contextmanager
@contextmanager
def profile(top_n: int = 20, sort_by: str = "cumulative", min_time: float = 0.001):
pr = cProfile.Profile()
pr.enable()
try:
yield
finally:
pr.disable()
s = io.StringIO()
ps = pstats.Stats(pr, stream=s).strip_dirs()
ps.sort_stats(sort_by)
ps.print_stats()
rows = []
for line in s.getvalue().splitlines():
parts = line.split(None, 5)
if len(parts) < 6:
continue
try:
cum = float(parts[3])
if cum >= min_time:
rows.append(
(
int(parts[0].split("/")[0]),
float(parts[1]),
cum,
float(parts[4]),
parts[5].strip(),
)
)
except (ValueError, IndexError):
continue
rows = rows[:top_n]
if not rows:
print("[profiler] nothing exceeded min_time threshold")
return
w = 55
print(f"\n── profile ({sort_by}, ≥{min_time}s) ──────────────────────────────")
print(f"{'calls':>7} {'tot':>8} {'cum':>8} {'percall':>8} {'function':<{w}}")
print("" * (7 + 8 + 8 + 8 + w + 6))
for calls, tot, cum, percall, loc in rows:
c = (
f"\033[91m{cum:8.4f}\033[0m"
if cum > 1
else f"\033[93m{cum:8.4f}\033[0m" if cum > 0.1 else f"{cum:8.4f}"
)
print(
f"{calls:>7} {tot:8.4f} {c} {percall:8.4f} {loc[-w:] if len(loc) > w else loc:<{w}}"
)
print(f"" * (7 + 8 + 8 + 8 + w + 6))
print(
f" {len(rows)} functions | tot accounted: {sum(r[1] for r in rows):.4f}s\n"
)