1554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe/* 2554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Copyright (C) 2015 The Android Open Source Project 3554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * 4554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License"); 5554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * you may not use this file except in compliance with the License. 6554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * You may obtain a copy of the License at 7554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * 8554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * http://www.apache.org/licenses/LICENSE-2.0 9554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * 10554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Unless required by applicable law or agreed to in writing, software 11554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS, 12554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * See the License for the specific language governing permissions and 14554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * limitations under the License. 15554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe */ 16554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 17554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepackage com.android.preload.actions; 18554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 19554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.ddmlib.IDevice; 20554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DeviceUtils; 21554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpData; 22554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpTableModel; 23554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.Main; 24554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 25554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.awt.event.ActionEvent; 26554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Date; 27554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Map; 28554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.concurrent.TimeUnit; 29554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 30554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport javax.swing.AbstractAction; 31554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 32554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepublic class RunMonkeyAction extends AbstractAction implements DeviceSpecific { 33554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 34554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private final static String DEFAULT_MONKEY_PACKAGES = 35554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe "com.android.calendar,com.android.gallery3d"; 36554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 37554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private IDevice device; 38554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private DumpTableModel dataTableModel; 39554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 40554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe public RunMonkeyAction(IDevice device, DumpTableModel dataTableModel) { 41554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe super("Run monkey"); 42554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe this.device = device; 43554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe this.dataTableModel = dataTableModel; 44554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 45554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 46554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe @Override 47554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe public void setDevice(IDevice device) { 48554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe this.device = device; 49554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 50554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 51554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe @Override 52554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe public void actionPerformed(ActionEvent e) { 53554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe String packages = Main.getUI().showInputDialog("Please enter packages name to run with" 54554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe + " the monkey, or leave empty for default."); 55554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe if (packages == null) { 56554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe return; 57554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 58554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe if (packages.isEmpty()) { 59554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe packages = DEFAULT_MONKEY_PACKAGES; 60554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 61554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe new Thread(new RunMonkeyRunnable(packages)).start(); 62554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 63554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 64554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private class RunMonkeyRunnable implements Runnable { 65554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 66554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private String packages; 67554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private final static int ITERATIONS = 1000; 68554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 69554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe public RunMonkeyRunnable(String packages) { 70554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe this.packages = packages; 71554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 72554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 73554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe @Override 74554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe public void run() { 75554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe Main.getUI().showWaitDialog(); 76554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 77554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe try { 78554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe String pkgs[] = packages.split(","); 79554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 80554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe for (String pkg : pkgs) { 81554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe Main.getUI().updateWaitDialog("Running monkey on " + pkg); 82554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 83554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe try { 84554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe // Stop running app. 85554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe forceStop(pkg); 86554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 87554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe // Little bit of breather here. 88554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe try { 89554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe Thread.sleep(1000); 90554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } catch (Exception e) { 91554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 92554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 93554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe DeviceUtils.doShell(device, "monkey -p " + pkg + " " + ITERATIONS, 1, 94554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe TimeUnit.MINUTES); 95554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 96554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe Main.getUI().updateWaitDialog("Retrieving heap data for " + pkg); 97554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe Map<String, String> data = Main.findAndGetClassData(device, pkg); 98554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe DumpData dumpData = new DumpData(pkg, data, new Date()); 99554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe dataTableModel.addData(dumpData); 100554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } catch (Exception e) { 101554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe e.printStackTrace(); 102554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } finally { 103554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe // Stop running app. 104554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe forceStop(pkg); 105554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 106554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 107554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } finally { 108554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe Main.getUI().hideWaitDialog(); 109554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 110554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 111554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe 112554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe private void forceStop(String packageName) { 113554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe // Stop running app. 114554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe DeviceUtils.doShell(device, "force-stop " + packageName, 5, TimeUnit.SECONDS); 115554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe DeviceUtils.doShell(device, "kill " + packageName, 5, TimeUnit.SECONDS); 116554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe DeviceUtils.doShell(device, "kill `pid " + packageName + "`", 5, TimeUnit.SECONDS); 117554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 118554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe } 119554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe}