From 2a7654bf9f541b39714303f27309fd38cfaa8534 Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Tue, 23 Aug 2022 21:43:02 +0000 Subject: [PATCH] Add helper annotation for profiling functions --- server/entertainment_decider/profiling.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 server/entertainment_decider/profiling.py diff --git a/server/entertainment_decider/profiling.py b/server/entertainment_decider/profiling.py new file mode 100644 index 0000000..2ec92f6 --- /dev/null +++ b/server/entertainment_decider/profiling.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +import cProfile +from functools import wraps +import pstats + + +LIST_MAX_FUNCTION_COUNT = 40 + + +def profile(fun): + @wraps(fun) + def _fun(*args, **kwargs): + with cProfile.Profile() as pf: + ret = fun(*args, **kwargs) + pstats.Stats(pf).sort_stats(pstats.SortKey.CUMULATIVE).print_stats( + "entertainment_decider", + LIST_MAX_FUNCTION_COUNT, + ) + return ret + + return _fun