Created
July 16, 2020 23:42
-
-
Save qxcv/179bd71aa6c32b5a03d3eb1310e2643a to your computer and use it in GitHub Desktop.
Sort the times produced by pytest --durations=0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
"""Some helpful functions for neatly displaying the output of `pytest | |
--durations=0`.""" | |
import itertools | |
import re | |
import sys | |
def parse_lines(text): | |
results = [] | |
for line in text.splitlines(): | |
match = re.match( | |
r'^(?P<time>\d+.\d+)s (?P<call_setup>call|setup)\s+' | |
r'(?P<test_name>.*?)(\[(?P<params>.*)\])?\s*$', line) | |
if match: | |
match_dict = match.groupdict() | |
new_dict = dict( | |
time=float(match_dict['time']), | |
ident=( | |
match_dict['test_name'], | |
match_dict['call_setup'] + '::' + (match_dict['params'] or ''))) | |
results.append(new_dict) | |
return results | |
def sort_into_groups(results): | |
key = lambda d: d['ident'][0] | |
sorted_results = sorted(results, key=key) | |
group_iter = itertools.groupby(sorted_results, key) | |
results = [] | |
for group_ident, group in group_iter: | |
group_list = list(group) | |
group_time = sum(d['time'] for d in group_list) | |
sub_idents = [(d['time'], d['ident'][1]) for d in group_list] | |
results.append((group_time, group_ident, sub_idents)) | |
return sorted(results, reverse=True) | |
def print_sorted_results(sorted_results): | |
for group_time, group_ident, group in sorted_results: | |
print('\n% 7.2f %s' % (group_time, group_ident)) | |
for elem_time, elem_name in group: | |
print(' % 4.2f %s' % (elem_time, elem_name)) | |
data = sys.stdin.read() | |
results = parse_lines(data) | |
sorted_results = sort_into_groups(results) | |
print_sorted_results(sorted_results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment