Squish/Using a Decorator to Profile Tests: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
==Use a decorator to profile tests==
[[Category:Tools::Squish]]


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.
== 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:
In order to do this you will need to run your main function through the python profiling package cProfile. For example:
<code>def main():<br /> pass
import cProfile<br />profile = cProfile.Profile&amp;amp;#40;&amp;#41;<br />profile.runcall(main)<br /></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:


This technique also has the advantage of saving the code to profile in one single place. Also, you could turn it off or depending on an environment variable, you could report it in any way your particular setup requires.
<code>import decorator
 
</code>decorator.profile<br />def main():<br /> pass


===Categories:===
#in the decorator file<br />def profile&amp;amp;#40;func&amp;amp;#41;:<br /> def inner():<br /> import cProfile<br /> p = cProfile.Profile&amp;amp;#40;&amp;#41;<br /> p.runcall(func)<br /> #do something to report the result


* [[:Category:Tools|Tools]]
return inner<br /><code>
** [[:Category:Tools::Squish|Squish]]

Revision as of 06:13, 24 February 2015


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():<br /> pass

import cProfile<br />profile = cProfile.Profile&amp;amp;#40;&amp;#41;<br />profile.runcall(main)<br />

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

  1. in the decorator file
    def profile&amp;#40;func&amp;#41;:
    def inner():
    import cProfile
    p = cProfile.Profile&amp;#40;&#41;
    p.runcall(func)
    #do something to report the result

return inner