OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Runs the Monkey tests on one or more devices.""" | 6 """Runs the Monkey tests on one or more devices.""" |
7 import logging | 7 import logging |
8 import optparse | 8 import optparse |
9 import time | 9 import time |
10 import random | |
frankf
2012/09/11 17:36:51
not alphabetical. Please run gpylint.
| |
10 | 11 |
11 from pylib import android_commands | 12 from pylib import android_commands |
12 from pylib import python_test_base | 13 from pylib import python_test_base |
13 from pylib import python_test_caller | 14 from pylib import python_test_caller |
14 from pylib import python_test_sharder | 15 from pylib import python_test_sharder |
15 from pylib import test_options_parser | 16 from pylib import test_options_parser |
16 from pylib import test_result | 17 from pylib import test_result |
17 | 18 |
18 | 19 |
19 class MonkeyTest(python_test_base.PythonTestBase): | 20 class MonkeyTest(python_test_base.PythonTestBase): |
20 def __init__(self, test_name, options): | 21 def __init__(self, test_name, options): |
21 self.options = options | 22 self.options = options |
22 super(MonkeyTest, self).__init__(test_name) | 23 super(MonkeyTest, self).__init__(test_name) |
23 | 24 |
24 def testMonkey(self): | 25 def testMonkey(self): |
25 start_ms = int(time.time()) * 1000 | 26 start_ms = int(time.time()) * 1000 |
26 | 27 |
27 # Launch and wait for Chrome to launch. | 28 # Launch and wait for Chrome to launch. |
28 self.adb.StartActivity(self.options['package_name'], | 29 self.adb.StartActivity(self.options.package_name, |
29 self.options.pop('activity_name'), | 30 self.options.activity_name, |
30 wait_for_completion=True, | 31 wait_for_completion=True, |
31 action='android.intent.action.MAIN') | 32 action='android.intent.action.MAIN') |
32 | 33 |
33 # Chrome crashes are not always caught by Monkey test runner. | 34 # Chrome crashes are not always caught by Monkey test runner. |
34 # Verify Chrome has the same PID before and after the test. | 35 # Verify Chrome has the same PID before and after the test. |
35 before_pids = self.adb.ExtractPid(self.options['package_name']) | 36 before_pids = self.adb.ExtractPid(self.options.package_name) |
36 | 37 |
37 # Run the test. | 38 # Run the test. |
38 output = '' | 39 output = '' |
39 duration_ms = 0 | 40 duration_ms = 0 |
40 if before_pids: | 41 if before_pids: |
41 output = '\n'.join(self.adb.RunMonkey(**self.options)) | 42 output = '\n'.join(RunMonkey(self.adb, self.options)) |
42 duration_ms = int(time.time()) * 1000 - start_ms | 43 duration_ms = int(time.time()) * 1000 - start_ms |
43 after_pids = self.adb.ExtractPid(self.options['package_name']) | 44 after_pids = self.adb.ExtractPid(self.options.package_name) |
44 | 45 |
45 crashed = (not before_pids or not after_pids | 46 crashed = (not before_pids or not after_pids |
46 or after_pids[0] != before_pids[0]) | 47 or after_pids[0] != before_pids[0]) |
47 result = test_result.SingleTestResult(self.qualified_name, start_ms, | 48 result = test_result.SingleTestResult(self.qualified_name, start_ms, |
48 duration_ms, log=output) | 49 duration_ms, log=output) |
49 results = test_result.TestResults() | 50 results = test_result.TestResults() |
50 | 51 |
51 if 'Monkey finished' in output and not crashed: | 52 if 'Monkey finished' in output and not crashed: |
52 results.ok = [result] | 53 results.ok = [result] |
53 else: | 54 else: |
54 results.crashed = [result] | 55 results.crashed = [result] |
55 | 56 |
56 return results | 57 return results |
57 | 58 |
58 | 59 |
59 def DispatchPythonTests(options): | 60 def DispatchPythonTests(options): |
60 """Dispatches the Monkey tests, sharding it if there multiple devices.""" | 61 """Dispatches the Monkey tests, sharding it if there multiple devices.""" |
61 logger = logging.getLogger() | 62 logger = logging.getLogger() |
62 logger.setLevel(logging.DEBUG) | 63 logger.setLevel(logging.DEBUG) |
63 | 64 |
64 build_type = options.pop('build_type') | |
65 available_tests = [MonkeyTest('testMonkey', options)] | 65 available_tests = [MonkeyTest('testMonkey', options)] |
66 attached_devices = android_commands.GetAttachedDevices() | 66 attached_devices = android_commands.GetAttachedDevices() |
67 if not attached_devices: | 67 if not attached_devices: |
68 raise Exception('You have no devices attached or visible!') | 68 raise Exception('You have no devices attached or visible!') |
69 | 69 |
70 # Actually run the tests. | 70 # Actually run the tests. |
71 logging.debug('Running monkey tests.') | 71 logging.debug('Running monkey tests.') |
72 available_tests *= len(attached_devices) | 72 available_tests *= len(attached_devices) |
73 sharder = python_test_sharder.PythonTestSharder( | 73 sharder = python_test_sharder.PythonTestSharder( |
74 attached_devices, 1, available_tests) | 74 attached_devices, 1, available_tests) |
75 result = sharder.RunShardedTests() | 75 result = sharder.RunShardedTests() |
76 result.LogFull('Monkey', 'Monkey', build_type) | 76 result.LogFull('Monkey', 'Monkey', options.build_type) |
77 result.PrintAnnotation() | 77 result.PrintAnnotation() |
78 | 78 |
79 | 79 |
80 def RunMonkey(adb, options): | |
bulach
2012/09/11 11:08:49
nit: if nothing else will use this, please make it
frankf
2012/09/11 17:36:51
I prefer to have explicit parameters with default
Isaac (away)
2012/09/13 03:06:18
This style of defaults is consistent with the rest
Isaac (away)
2012/09/13 03:06:18
Done.
| |
81 """Runs monkey test for a given package. | |
82 | |
83 Args: | |
84 package_name: Allowed package. | |
85 category: A list of allowed categories. | |
86 throttle: Delay between events (ms). | |
87 seed: Seed value for pseduo-random generator. Same seed value | |
88 generates the same sequence of events. Seed is randomized by | |
89 default. | |
90 event_count: Number of events to generate. | |
91 verbosity: Verbosity level [0-3]. | |
92 extra_args: A string of other args to pass to the command verbatim. | |
bulach
2012/09/11 11:08:49
if this is becomes a method, please remove this Ar
Isaac (away)
2012/09/13 03:06:18
Done.
| |
93 | |
94 Returns: | |
95 Output of the test run. | |
96 """ | |
97 category = options.category or [] | |
98 seed = options.seed or random.randint(1, 100) | |
99 throttle = options.throttle or 100 | |
100 event_count = options.event_count or 10000 | |
101 verbosity = options.verbosity or 1 | |
102 extra_args = options.extra_args or '' | |
103 | |
104 cmd = ['monkey', | |
105 '-p %s' % options.package_name, | |
106 ' '.join(['-c %s' % c for c in category]), | |
107 '--throttle %d' % throttle, | |
108 '-s %d' % seed, | |
109 '-v ' * verbosity, | |
110 '--monitor-native-crashes', | |
111 '--kill-process-after-error', | |
112 extra_args, | |
113 '%d' % event_count] | |
114 return adb.RunShellCommand(' '.join(cmd), | |
115 timeout_time=event_count*throttle*1.5) | |
116 | |
80 def main(): | 117 def main(): |
81 desc = 'Run the Monkey tests on 1 or more devices.' | 118 desc = 'Run the Monkey tests on 1 or more devices.' |
82 parser = optparse.OptionParser(description=desc) | 119 parser = optparse.OptionParser(description=desc) |
83 test_options_parser.AddBuildTypeOption(parser) | 120 test_options_parser.AddBuildTypeOption(parser) |
84 parser.add_option('--package-name', | 121 parser.add_option('--package-name', help='Allowed package.') |
85 help='Allowed package.') | |
86 parser.add_option('--activity-name', | 122 parser.add_option('--activity-name', |
87 default='com.google.android.apps.chrome.Main', | 123 default='com.google.android.apps.chrome.Main', |
88 help='Name of the activity to start [default: %default].') | 124 help='Name of the activity to start [default: %default].') |
89 parser.add_option('--category', | 125 parser.add_option('--category', |
90 help='A list of allowed categories [default: ""].') | 126 help='A list of allowed categories [default: ""].') |
91 parser.add_option('--throttle', default=100, type='int', | 127 parser.add_option('--throttle', default=100, type='int', |
92 help='Delay between events (ms) [default: %default]. ') | 128 help='Delay between events (ms) [default: %default]. ') |
93 parser.add_option('--seed', type='int', | 129 parser.add_option('--seed', type='int', |
94 help='Seed value for pseduo-random generator. Same seed' | 130 help='Seed value for pseduo-random generator. Same seed' |
95 ' value generates the same sequence of events. Seed is' | 131 ' value generates the same sequence of events. Seed is' |
96 ' randomized by default.') | 132 ' randomized by default.') |
97 parser.add_option('--event-count', default=10000, type='int', | 133 parser.add_option('--event-count', default=10000, type='int', |
98 help='Number of events to generate [default: %default].') | 134 help='Number of events to generate [default: %default].') |
99 parser.add_option('--verbosity', default=1, type='int', | 135 parser.add_option('--verbosity', default=1, type='int', |
100 help='Verbosity level [0-3] [default: %default].') | 136 help='Verbosity level [0-3] [default: %default].') |
101 parser.add_option('--extra-args', default='', | 137 parser.add_option('--extra-args', default='', |
102 help='String of other args to pass to the command verbatim' | 138 help='String of other args to pass to the command verbatim' |
103 ' [default: "%default"].') | 139 ' [default: "%default"].') |
104 (options, args) = parser.parse_args() | 140 (options, args) = parser.parse_args() |
105 | 141 |
106 if args: | 142 if args: |
107 parser.error('Unknown arguments: %s' % args) | 143 parser.error('Unknown arguments: %s' % args) |
108 | 144 |
109 if not options.package_name: | 145 if not options.package_name: |
110 parser.error('Missing package name') | 146 parser.error('Missing package name') |
111 | 147 |
112 if options.category: | 148 if options.category: |
113 options.category = options.category.split(',') | 149 options.category = options.category.split(',') |
114 | 150 |
115 DispatchPythonTests(vars(options)) | 151 DispatchPythonTests(options) |
116 | 152 |
117 | 153 |
118 if __name__ == '__main__': | 154 if __name__ == '__main__': |
119 main() | 155 main() |
OLD | NEW |