Squish/Using a Decorator to Profile Tests: Difference between revisions
< Squish
Jump to navigation
Jump to search
No edit summary |
AutoSpider (talk | contribs) (Move [[Category::Tools::Squish]] -> [[Category::Squish]]) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category | [[Category:Squish]] | ||
== Use a decorator to profile tests == | == Use a decorator to profile tests == | ||
Line 7: | Line 7: | ||
In order to do this you will need to run your main function through the python profiling package cProfile. For example: | In order to do this you will need to run your main function through the python profiling package cProfile. For example: | ||
<code>def main(): | <code>def main(): | ||
pass | |||
import cProfile | import cProfile | ||
profile = cProfile.Profile() | |||
profile.runcall(main) | |||
</code> | |||
Depending on your particular configuration, the previous code might lead you to run the test twice. To avoid the repetition, wrap your main function with a decorator instead: | Depending on your particular configuration, the previous code might lead you to run the test twice. To avoid the repetition, wrap your main function with a decorator instead: | ||
Line 15: | Line 19: | ||
<code>import decorator | <code>import decorator | ||
</code>decorator.profile | </code>decorator.profile | ||
def main(): | |||
pass | |||
#in the decorator file | #in the decorator file | ||
def profile(func): | |||
def inner(): | |||
import cProfile | |||
p = cProfile.Profile() | |||
p.runcall(func) | |||
#do something to report the result | |||
return inner | return inner | ||
<code> |
Latest revision as of 08:38, 25 November 2017
Use a decorator to profile tests
If your Squish tests are very intensive, or you have hundreds of them, you can run them trough a profiler from time to time. This will enable you to see where you're spending most of your time and will help optimize your efforts.
In order to do this you will need to run your main function through the python profiling package cProfile. For example:
def main():
pass
import cProfile
profile = cProfile.Profile()
profile.runcall(main)
Depending on your particular configuration, the previous code might lead you to run the test twice. To avoid the repetition, wrap your main function with a decorator instead:
import decorator
decorator.profile
def main():
pass
- in the decorator file
def profile(func):
def inner(): import cProfile p = cProfile.Profile() p.runcall(func) #do something to report the result
return inner