Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / build / android / surface_stats.py
blob9cff25c04cc761ef8590a98b3f1e6a2e93253a20
1 #!/usr/bin/env python
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Command line tool for continuously printing Android graphics surface
8 statistics on the console.
9 """
11 import collections
12 import optparse
13 import sys
14 import time
16 from pylib import android_commands, surface_stats_collector
17 from pylib.utils import run_tests_helper
20 _FIELD_FORMAT = {
21 'jank_count (janks)': '%d',
22 'max_frame_delay (vsyncs)': '%d',
23 'avg_surface_fps (fps)': '%.2f',
24 'frame_lengths (vsyncs)': '%.3f',
25 'refresh_period (seconds)': '%.6f',
29 def _MergeResults(results, fields):
30 merged_results = collections.defaultdict(list)
31 for result in results:
32 if fields != ['all'] and not result.name in fields:
33 continue
34 name = '%s (%s)' % (result.name, result.unit)
35 if isinstance(result.value, list):
36 value = result.value
37 else:
38 value = [result.value]
39 merged_results[name] += value
40 for name, values in merged_results.iteritems():
41 merged_results[name] = sum(values) / float(len(values))
42 return merged_results
45 def _GetTerminalHeight():
46 try:
47 import fcntl, termios, struct
48 except ImportError:
49 return 0, 0
50 height, _, _, _ = struct.unpack('HHHH',
51 fcntl.ioctl(0, termios.TIOCGWINSZ,
52 struct.pack('HHHH', 0, 0, 0, 0)))
53 return height
56 def _PrintColumnTitles(results):
57 for name in results.keys():
58 print '%s ' % name,
59 print
60 for name in results.keys():
61 print '%s ' % ('-' * len(name)),
62 print
65 def _PrintResults(results):
66 for name, value in results.iteritems():
67 value = _FIELD_FORMAT.get(name, '%s') % value
68 print value.rjust(len(name)) + ' ',
69 print
72 def main(argv):
73 parser = optparse.OptionParser(usage='Usage: %prog [options]',
74 description=__doc__)
75 parser.add_option('-v',
76 '--verbose',
77 dest='verbose_count',
78 default=0,
79 action='count',
80 help='Verbose level (multiple times for more)')
81 parser.add_option('--device',
82 help='Serial number of device we should use.')
83 parser.add_option('-f',
84 '--fields',
85 dest='fields',
86 default='jank_count,max_frame_delay,avg_surface_fps,'
87 'frame_lengths',
88 help='Comma separated list of fields to display or "all".')
89 parser.add_option('-d',
90 '--delay',
91 dest='delay',
92 default=1,
93 type='float',
94 help='Time in seconds to sleep between updates.')
96 options, args = parser.parse_args(argv)
97 run_tests_helper.SetLogLevel(options.verbose_count)
99 adb = android_commands.AndroidCommands(options.device)
100 collector = surface_stats_collector.SurfaceStatsCollector(adb)
101 collector.DisableWarningAboutEmptyData()
103 fields = options.fields.split(',')
104 row_count = None
106 try:
107 collector.Start()
108 while True:
109 time.sleep(options.delay)
110 results = collector.SampleResults()
111 results = _MergeResults(results, fields)
113 if not results:
114 continue
116 terminal_height = _GetTerminalHeight()
117 if row_count is None or (terminal_height and
118 row_count >= terminal_height - 3):
119 _PrintColumnTitles(results)
120 row_count = 0
122 _PrintResults(results)
123 row_count += 1
124 except KeyboardInterrupt:
125 sys.exit(0)
126 finally:
127 collector.Stop()
130 if __name__ == '__main__':
131 main(sys.argv)