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