The Artima Developer Community
Sponsored Link

Python Buzz Forum
YADI, part 3: timedtest, a decorator for time performance tests

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Tarek Ziadé

Posts: 56
Nickname: tarek
Registered: Sep, 2005

Tarek Ziadé is R&D developer for Nuxeo Corp.
YADI, part 3: timedtest, a decorator for time performance tests Posted: Sep 29, 2005 9:53 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Tarek Ziadé.
Original Post: YADI, part 3: timedtest, a decorator for time performance tests
Feed Title: Tarek Ziadé
Feed URL: http://blogs.nuxeo.com/sections/blogs/tarek_ziade/exportrss
Feed Description: Feed about Python, Zope and all related technos
Latest Python Buzz Posts
Latest Python Buzz Posts by Tarek Ziadé
Latest Posts From Tarek Ziadé

Advertisement
Grig Gheorghiu has released a pretty cool tool, called pyUnitPerf. It's a
port of Mike Clark's JUnitPerf.

This tool runs your tests cases, and measure the time they take to run. It
can pop a Failure when the tests lasts too long. It also has a Load runner,
but nothing comparable to the amazing funkload tool

Thats quite useful in some cases, when your test fixture does not make the
code run slower, like it happens in ZopeTestCase.

So since pyUnitPerf associates a max time with a TestCase class, and since
test fixture can slow down the tests, it makes it really hard to use it, to
measure a distinct test method.

So I have taken back the idea and put it in a decorator that I can use on
all test methods I want to control, performances wise.   

The timedtest decorator


import unittest
import time

# might be changed to allow tolerance
# if tests are run on other boxes
TOLERANCE = 0.05

class DurationError(AssertionError): pass

def timedtest(max_time, tolerance=TOLERANCE):
    """ timedtest decorator
    decorates the test method with a timer
    when the time spent by the test exceeds
    max_time in seconds, an Assertion error is thrown.
    """
    def _timedtest(function):
        def wrapper(*args, **kw):
            start_time = time.time()
            try:
                function(*args, **kw)
            finally:
                total_time = time.time() - start_time
                if total_time > max_time + tolerance:
                    raise DurationError(('Test was too long (%.2f s)'
                                           % total_time))
        return wrapper

    return _timedtest
    

Usage


It's quite easy to use, just decorate a TestCase method with it, giving the max time allowed for the test, in seconds:    

class MyTestCase(unittest.TestCase):

    def __init__(self, name):
        unittest.TestCase.__init__(self, name)

    @timedtest(2)
    def test_timecritical(self):
        time.sleep(1.5)

    @timedtest(1)
    def test_timecritical2(self):
        time.sleep(1.5)

if __name__ == '__main__':
    suite = unittest.makeSuite(MyTestCase)
    unittest.TextTestRunner().run(suite)

Now this can help much to make sure some critical code does not get too
  slow.
  

[tziade@Tarek Desktop]$ python timedtest.py
.F
======================================================================
FAIL: test_timecritical2 (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "timedtest.py", line 28, in wrapper
    raise DurationError(('Test was too long (%.2f s)'
DurationError: Test was too long (1.50 s)

----------------------------------------------------------------------
Ran 2 tests in 2.999s

 

Read: YADI, part 3: timedtest, a decorator for time performance tests

Topic: ShedSkin Previous Topic   Next Topic Topic: American phone card

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use