Skip to content

Commit 519f7b5

Browse files
committed
add with profile block
1 parent bfd2b80 commit 519f7b5

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

template_profiler_panel/panels/template.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727

2828
@wrapt.decorator
2929
def profile_method(wrapped, instance, args, kwargs):
30+
"""
31+
32+
Profile method decorator
33+
Usage:
34+
35+
from template_profiler_panel.panels.template import profile_method
36+
@profile method
37+
def method():
38+
...
39+
"""
3040
start = time()
3141

3242
result = wrapped(*args, **kwargs)
@@ -47,6 +57,34 @@ def profile_method(wrapped, instance, args, kwargs):
4757
return result
4858

4959

60+
class Profile:
61+
"""
62+
Profile with block
63+
Usage:
64+
65+
from template_profiler_panel.panels.template import Profile
66+
with Profile("block name"):
67+
...
68+
"""
69+
def __init__(self, name="Profile with", *args, **kwargs):
70+
self.name = name
71+
72+
def __enter__(self):
73+
self.start = time()
74+
75+
def __exit__(self, type, value, traceback):
76+
self.end = time()
77+
78+
template_rendered.send(
79+
sender=self.__class__,
80+
instance=self.name,
81+
start=self.start,
82+
end=self.end,
83+
processing_timeline=[],
84+
level=1,
85+
)
86+
87+
5088
def get_nodelist_timeline(nodelist, level):
5189
timeline = []
5290
for node in nodelist:

0 commit comments

Comments
 (0)