~loic.molinari/+junk/time-to-first-frame

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
#!/usr/bin/env python

import sys, subprocess, os, tempfile, math
import matplotlib.pyplot as plot
from matplotlib import rcParams

PROGRAMS = [
    { 'command': ['./src/qt/qt'],                                   'label': 'Qt OpenGL' },
    { 'command': ['./src/quick/quick', 'src/quick/QuickMain.qml'],  'label': 'Qt Quick'  },
    { 'command': ['./src/quick/quick', 'src/quick/UbuntuMain.qml'], 'label': 'Qt Ubuntu' }
]
PLOT_FONT_NAME = 'Ubuntu'
PLOT_FONT_SIZE = 12
EXECUTIONS_COUNT = 100

# Gets average, standard deviation, min, max.
def getStats(values):
    sum = 0.0
    min = sys.maxsize
    max = 0
    for i in values:
        if i < min:
            min = i
        if i > max:
            max = i
        sum += i
    avg = sum / len(values)
    var = 0.0
    for i in values:
        var += (i - avg) * (i - avg)
    var /= len(values)
    return (avg, math.sqrt(var), min, max)

def main(args):
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = [PLOT_FONT_NAME]
    rcParams['font.size'] = PLOT_FONT_SIZE
    fig, axis = plot.subplots()

    for program in PROGRAMS:
        values = []
        for i in range(0, EXECUTIONS_COUNT):
            try:
                p = subprocess.Popen(program['command'], bufsize=4096, stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE, close_fds=True)
                p.wait()
                for line in p.stdout.readlines():
                    if line.startswith('TTFF: '):
                        value = [t(s) for t,s in zip((long,), (line.split('TTFF: ')[1],))][0]
                        values.append(value * 0.000001)
                        break
                else:
                    print 'Error: Can\'t retrieve value.'
                    sys.exit(1)
            except:
                print 'Error: Can\'t spawn ' + program['command'][0]
                sys.exit(1)
        (avg, stdev, min, max) = getStats(values)
        label = program['label'] + ' (avg=' + ('%.2f' % avg) + ', stdev=' + ('%.2f' % stdev) + ')'
        base_line, = axis.plot(range(1, EXECUTIONS_COUNT + 1), values, '-', label=label)
        axis.plot((1, EXECUTIONS_COUNT), (avg, avg), '--', color=base_line.get_color(), alpha=0.5)

    axis.grid()
    axis.legend(loc=0)
    axis.set_xlim(0, EXECUTIONS_COUNT + 1)
    axis.set_ylim(0)
    plot.title('Time to first frame')
    plot.xlabel('Executions')
    plot.ylabel('Time (ms)')
    plot.show()

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))