File: decorators.py

package info (click to toggle)
python-enocean 0.60.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: python: 2,030; xml: 1,464; makefile: 8; sh: 1
file content (38 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- encoding: utf-8 -*-
from __future__ import print_function, unicode_literals, division
import time
import functools
from os import environ


def timing(rounds=1, limit=None):
    '''
    Wrapper to implement simple timing of tests.
    Allows running multiple rounds to calculate average time.
    Limit (in milliseconds) can be set to assert, if (average) duration is too high.
    '''
    def decorator(method):
        @functools.wraps(method)
        def f():
            if rounds == 1:
                start = time.time()
                method()
                duration = time.time() - start
            else:
                start = time.time()
                for i in range(rounds):
                    method()
                duration = (time.time() - start) / rounds
            # Use milliseconds for duration counter
            duration = duration * 1e3

            print('Test "%s.%s" took %.06f ms.' % (method.__module__, method.__name__, duration))
            if limit is not None:
                assert limit > duration, 'Timing failure: %.06f > %.06f' % (duration, limit)

        # Run tests with timings, only if WITH_TIMINGS environment variable is set.
        # This is because tests with multiple rounds can take long to process.
        if environ.get('WITH_TIMINGS', None) == '1':
            return f
        return method
    return decorator