/usr/lib/python3/dist-packages/nosetimer/plugin.py is in python3-nose-timer 0.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import re
import operator
import os
from time import time
import logging
from nose.plugins.base import Plugin
log = logging.getLogger('nose.plugin.timer')
class TimerPlugin(Plugin):
"""This plugin provides test timings."""
name = 'timer'
score = 1
COLORS = {
'ok': '\033[92m',
'warning': '\033[93m',
'error': '\033[91m',
'default': '\033[0m'
}
time_format = re.compile(r'^(?P<time>\d+)(?P<units>s|ms)?$')
def _timeTaken(self):
if hasattr(self, '_timer'):
taken = time() - self._timer
else:
# test died before it ran (probably error in setup())
# or success/failure added before test started probably
# due to custom TestResult munging
taken = 0.0
return taken
def _parse_time(self, value):
"""Parse string time representation to get number of milliseconds.
Raises ValueError for invalid format
"""
try:
# Default time unit is second, we should convert it to milliseconds
return int(value) * 1000
except ValueError:
# Try to parse if we are unlucky to cast value into int
m = self.time_format.match(value)
if not m:
raise ValueError("Could not parse time represented by "
"'{t}'".format(t=value))
time = int(m.group('time'))
if m.group('units') != 'ms':
time *= 1000
return time
def configure(self, options, config):
"""Configures the test timer plugin."""
super(TimerPlugin, self).configure(options, config)
self.config = config
self.timer_top_n = int(options.timer_top_n)
self.timer_ok = self._parse_time(options.timer_ok)
self.timer_warning = self._parse_time(options.timer_warning)
self.timer_verbose = options.timer_verbose
self._timed_tests = {}
def startTest(self, test):
"""Initializes a timer before starting a test."""
self._timer = time()
def afterTest(self, test):
"""Called after the test has been run and the result recorded (after
stopTest)."""
if self.timer_verbose:
try:
log.info(self._timed_tests[test.id()])
except KeyError:
pass
def report(self, stream):
"""Report the test times."""
if not self.enabled:
return
d = sorted(self._timed_tests.items(),
key=operator.itemgetter(1),
reverse=True)
for i, (test, time_taken) in enumerate(d):
if i < self.timer_top_n or self.timer_top_n == -1:
stream.writeln(self._format_report(test, time_taken))
def _format_report(self, test, time_taken):
# Time taken is stores as seconds, we should convert it to milliseconds
# to be able to compare with timer settings.
taken_ms = time_taken * 1000
if taken_ms <= self.timer_ok:
color = "default"
elif taken_ms <= self.timer_warning:
color = "warning"
else:
color = "error"
return "%s%s: %0.4fs%s" % \
(self.COLORS[color], test, time_taken, self.COLORS["default"])
def _register_time(self, test):
self._timed_tests[test.id()] = self._timeTaken()
def addError(self, test, err, capt=None):
"""Called when a test raises an uncaught exception."""
self._register_time(test)
def addFailure(self, test, err, capt=None, tb_info=None):
"""Called when a test fails."""
self._register_time(test)
def addSuccess(self, test, capt=None):
"""Called when a test passes."""
self._register_time(test)
def addOptions(self, parser, env=os.environ):
"""Called to allow plugin to register command-line options with the
parser.
"""
super(TimerPlugin, self).addOptions(parser, env)
_help = ("When the timer plugin is enabled, only show the N tests "
"that consume more time. The default, -1, shows all tests.")
parser.add_option("--timer-top-n", action="store", default="-1",
dest="timer_top_n", help=_help)
_time_unit_help = "Default time unit is a second, but you can set " \
"it explicitly (e.g. 1s, 500ms)."
_ok_help = ("Normal execution time. Such test will be highlight "
"green. {units}".format(units=_time_unit_help))
parser.add_option("--timer-ok", action="store", default=1,
dest="timer_ok",
help=_ok_help)
_warning_help = ("Warning about execution time to highlight slow "
"tests in yellow. Tests which take more time will "
"be highlighted red. {units}".format(
units=_time_unit_help))
parser.add_option("--timer-warning", action="store", default=3,
dest="timer_warning",
help=_warning_help)
_verbose_help = ("Print execution time after each test.")
parser.add_option("--timer-verbose", action="store_true",
dest="timer_verbose", help=_verbose_help)
|