File: _util.py

package info (click to toggle)
python-invoke 1.4.1%2Bds-0.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,704 kB
  • sloc: python: 11,377; makefile: 18; sh: 12
file content (57 lines) | stat: -rw-r--r-- 1,481 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from contextlib import contextmanager
from resource import getrusage, RUSAGE_SELF
import sys
import time

from six import wraps

from pytest import skip


def current_cpu_usage():
    rusage = getrusage(RUSAGE_SELF)
    return rusage.ru_utime + rusage.ru_stime


@contextmanager
def assert_cpu_usage(lt, verbose=False):
    """
    Execute wrapped block, asserting CPU utilization was less than ``lt``%.

    :param float lt: CPU use percentage above which failure will occur.
    :param bool verbose: Whether to print out the calculated percentage.
    """
    start_usage = current_cpu_usage()
    start_time = time.time()
    yield
    end_usage = current_cpu_usage()
    end_time = time.time()

    usage_diff = end_usage - start_usage
    time_diff = end_time - start_time

    if time_diff == 0:  # Apparently possible!
        time_diff = 0.000001

    percentage = (usage_diff / time_diff) * 100.0

    if verbose:
        print("Used {0:.2}% CPU over {1:.2}s".format(percentage, time_diff))

    assert percentage < lt


def only_utf8(f):
    """
    Decorator causing tests to skip if local shell pipes aren't UTF-8.
    """
    # TODO: use actual test selection labels or whatever nose has
    @wraps(f)
    def inner(*args, **kwargs):
        if getattr(sys.stdout, "encoding", None) == "UTF-8":
            return f(*args, **kwargs)
        # TODO: could remove this so they show green, but figure yellow is more
        # appropriate
        skip()

    return inner