~vila/uci-engine/config

« back to all changes in this revision

Viewing changes to bin/time_sorted_tests.py

  • Committer: Vincent Ladeuil
  • Date: 2014-04-10 19:09:02 UTC
  • mfrom: (239.1.187 uci-engine)
  • Revision ID: vila+ci@canonical.com-20140410190902-0zwky6u3aqtjrhrd
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import os
 
3
import sys
 
4
import re
 
5
import subprocess
 
6
 
 
7
 
 
8
# test_name ... OK (0.1 secs)
 
9
pattern = re.compile('^(.*) \.\.\. .*\((.*) secs\)$')
 
10
 
 
11
 
 
12
def gen_timings(output):
 
13
    '''Filter out the test names and timing data into tuples.'''
 
14
    for line in output.split('\n'):
 
15
        try:
 
16
            test_name, seconds = re.match(pattern, line).groups()
 
17
        except AttributeError:
 
18
            continue
 
19
        seconds = float(seconds)
 
20
        yield test_name, seconds
 
21
 
 
22
 
 
23
def sort_timings(timings):
 
24
    '''Sort the timings based on the second value in the tuple, the time in
 
25
    seconds.'''
 
26
    return sorted(timings, cmp=lambda x, y: cmp(x[1], y[1]))
 
27
 
 
28
 
 
29
def run_tests(args, test_command=['./run-tests']):
 
30
    '''Call ./run-tests with any arguments passed to this program. Return
 
31
    stderr'''
 
32
    cmd = test_command + args
 
33
    # Drop stderr. The text output from tests is not needed here.
 
34
    with open('/dev/null', 'a') as devnull:
 
35
        kwargs = {'stdout': subprocess.PIPE, 'stderr': devnull}
 
36
        p = subprocess.Popen(cmd, **kwargs)
 
37
    try:
 
38
        return p.communicate()[0]
 
39
    except KeyboardInterrupt:
 
40
        # Clean up the subprocess when we've interrupted this script.
 
41
        if p.pid:
 
42
            # Terminate.
 
43
            os.kill(p.pid, 15)
 
44
        return None
 
45
 
 
46
 
 
47
def print_timings(timings):
 
48
    for test_name, seconds in timings:
 
49
        print '%s: %s' % (test_name, seconds)
 
50
 
 
51
 
 
52
def print_total_timing(timings):
 
53
    total = sum([x[1] for x in timings])
 
54
    output = 'Total test time: %.2f seconds' % total
 
55
    print
 
56
    print '-' * len(output)
 
57
    print output
 
58
 
 
59
 
 
60
if __name__ == '__main__':
 
61
    output = run_tests(sys.argv[1:])
 
62
    if output is None:
 
63
        sys.exit(0)
 
64
    timings = gen_timings(output)
 
65
    sorted_timings = sort_timings(timings)
 
66
    print_timings(sorted_timings)
 
67
    print_total_timing(sorted_timings)