Debian Codegraph @

scripts/python/performance_replay.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 '''
4 Replays a Flightgear recording and shows framerate statistics.
5
6 Usage:
7 -f <fgfs>
8 Name of Flightear executable/script, e.g.: -f run_fgfs.sh
9 -i <tape>
10 Name of recording, for use with --load-tape.
11 '''
12
13 import math
14 import sys
15 import time
16
17 import recordreplay
18
19
20 def average_stddev(items):
21 '''
22 Returns (average, stddev).
23 '''
24 total = 0
25 total_sq = 0
26 for item in items:
27 total += item
28 total_sq += item*item
29 n = len(items)
30 average = total / n
31 variance = total_sq / n - (total / n)**2
32 stddev = math.sqrt(variance)
33 return average, stddev
34
35
36 if __name__ == '__main__':
37 fgfs = None
38 tape = None
39 args = iter(sys.argv[1:])
40 while 1:
41 try:
42 arg = next(args)
43 except StopIteration:
44 break
45 if arg in ('-h', '--help'):
46 print(__doc__)
47 elif arg == '-f':
48 fgfs = next(args)
49 elif arg == '-i':
50 tape = next(args)
51 else:
52 raise Exception(f'Unrecognised arg: {arg}')
53
54 if not fgfs:
55 raise Exception(f'Specify fgfs executable/run-script with -f')
56 if not tape:
57 raise Exception(f'Specify tape to replay with -i')
58
59 fg = recordreplay.Fg( None,
60 f'{fgfs}'
61 f' --load-tape={tape}'
62 f' --timeofday=noon'
63 f' --prop:bool:/sim/replay/log-frame-times=true'
64 f' --prop:bool:/sim/replay/replay-main-view=true'
65 f' --prop:bool:/sim/replay/replay-main-window-size=true'
66 f' --prop:bool:/sim/replay/looped=false'
67 )
68
69 fg.waitfor('/sim/fdm-initialized', 1, timeout=45)
70 fg.waitfor('/sim/replay/replay-state', 1)
71 fg.waitfor('/sim/replay/replay-state-eof', 1, timeout=600)
72
73 print(f'Reading frame-time statistics...')
74 t = time.time()
75 items = fg.fg.ls('/sim/replay/log-frame-times')
76 t = time.time() - t
77 print(f'fg.fg.ls took t={t}')
78
79 fg.close()
80
81 dts = []
82 for item in items:
83 if item.name == 'dt':
84 dts.append(float(item.value))
85
86 def statistics_text(dts):
87 dt_average, dt_stddev = average_stddev(dts)
88 t_total = sum(dts)
89 return f'n={len(dts)} dt_average={dt_average} dt_stddev={dt_stddev} t_total={t_total} fps={len(dts)/t_total}'
90
91 print(f'-' * 40)
92 print(f'')
93 print(f'Overall frame time (dt) statistics:')
94 print(f' {statistics_text(dts)}')
95
96 print(f'Ignoring first 4 frames:')
97 print(f' {statistics_text(dts[4:])}')
98
99 print(f'')
100 print(f'Raw frame times:')
101 dts_text = ' '.join(map(lambda dt: f'{dt:.4f}', dts))
102 print(f'dts: {dts_text}')