@@ -39,10 +39,12 @@ import java.io.FileReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application that injects random key events and other actions into the system.
|
* Application that injects random key events and other actions into the system.
|
||||||
@@ -140,12 +142,18 @@ public class Monkey {
|
|||||||
/** The delay between event inputs **/
|
/** The delay between event inputs **/
|
||||||
long mThrottle = 0;
|
long mThrottle = 0;
|
||||||
|
|
||||||
|
/** Whether to randomize each throttle (0-mThrottle ms) inserted between events. */
|
||||||
|
boolean mRandomizeThrottle = false;
|
||||||
|
|
||||||
/** The number of iterations **/
|
/** The number of iterations **/
|
||||||
int mCount = 1000;
|
int mCount = 1000;
|
||||||
|
|
||||||
/** The random number seed **/
|
/** The random number seed **/
|
||||||
long mSeed = 0;
|
long mSeed = 0;
|
||||||
|
|
||||||
|
/** The random number generator **/
|
||||||
|
Random mRandom = null;
|
||||||
|
|
||||||
/** Dropped-event statistics **/
|
/** Dropped-event statistics **/
|
||||||
long mDroppedKeyEvents = 0;
|
long mDroppedKeyEvents = 0;
|
||||||
|
|
||||||
@@ -421,19 +429,24 @@ public class Monkey {
|
|||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mRandom = new SecureRandom();
|
||||||
|
mRandom.setSeed((mSeed == 0) ? -1 : mSeed);
|
||||||
|
|
||||||
if (mScriptFileNames != null && mScriptFileNames.size() == 1) {
|
if (mScriptFileNames != null && mScriptFileNames.size() == 1) {
|
||||||
// script mode, ignore other options
|
// script mode, ignore other options
|
||||||
mEventSource = new MonkeySourceScript(mScriptFileNames.get(0), mThrottle);
|
mEventSource = new MonkeySourceScript(mRandom, mScriptFileNames.get(0), mThrottle,
|
||||||
|
mRandomizeThrottle);
|
||||||
mEventSource.setVerbose(mVerbose);
|
mEventSource.setVerbose(mVerbose);
|
||||||
|
|
||||||
mCountEvents = false;
|
mCountEvents = false;
|
||||||
} else if (mScriptFileNames != null && mScriptFileNames.size() > 1) {
|
} else if (mScriptFileNames != null && mScriptFileNames.size() > 1) {
|
||||||
if (mSetupFileName != null) {
|
if (mSetupFileName != null) {
|
||||||
mEventSource = new MonkeySourceRandomScript(mSetupFileName, mScriptFileNames,
|
mEventSource = new MonkeySourceRandomScript(mSetupFileName, mScriptFileNames,
|
||||||
mThrottle, mSeed);
|
mThrottle, mRandomizeThrottle, mRandom);
|
||||||
mCount++;
|
mCount++;
|
||||||
} else {
|
} else {
|
||||||
mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle, mSeed);
|
mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle,
|
||||||
|
mRandomizeThrottle, mRandom);
|
||||||
}
|
}
|
||||||
mEventSource.setVerbose(mVerbose);
|
mEventSource.setVerbose(mVerbose);
|
||||||
mCountEvents = false;
|
mCountEvents = false;
|
||||||
@@ -450,7 +463,7 @@ public class Monkey {
|
|||||||
if (mVerbose >= 2) { // check seeding performance
|
if (mVerbose >= 2) { // check seeding performance
|
||||||
System.out.println("// Seeded: " + mSeed);
|
System.out.println("// Seeded: " + mSeed);
|
||||||
}
|
}
|
||||||
mEventSource = new MonkeySourceRandom(mSeed, mMainApps, mThrottle);
|
mEventSource = new MonkeySourceRandom(mRandom, mMainApps, mThrottle, mRandomizeThrottle);
|
||||||
mEventSource.setVerbose(mVerbose);
|
mEventSource.setVerbose(mVerbose);
|
||||||
// set any of the factors that has been set
|
// set any of the factors that has been set
|
||||||
for (int i = 0; i < MonkeySourceRandom.FACTORZ_COUNT; i++) {
|
for (int i = 0; i < MonkeySourceRandom.FACTORZ_COUNT; i++) {
|
||||||
@@ -599,6 +612,8 @@ public class Monkey {
|
|||||||
mPkgWhitelistFile = nextOptionData();
|
mPkgWhitelistFile = nextOptionData();
|
||||||
} else if (opt.equals("--throttle")) {
|
} else if (opt.equals("--throttle")) {
|
||||||
mThrottle = nextOptionLong("delay (in milliseconds) to wait between events");
|
mThrottle = nextOptionLong("delay (in milliseconds) to wait between events");
|
||||||
|
} else if (opt.equals("--randomize-throttle")) {
|
||||||
|
mRandomizeThrottle = true;
|
||||||
} else if (opt.equals("--wait-dbg")) {
|
} else if (opt.equals("--wait-dbg")) {
|
||||||
// do nothing - it's caught at the very start of run()
|
// do nothing - it's caught at the very start of run()
|
||||||
} else if (opt.equals("--dbg-no-events")) {
|
} else if (opt.equals("--dbg-no-events")) {
|
||||||
@@ -1068,7 +1083,8 @@ public class Monkey {
|
|||||||
usage.append(" [--wait-dbg] [--dbg-no-events]\n");
|
usage.append(" [--wait-dbg] [--dbg-no-events]\n");
|
||||||
usage.append(" [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]\n");
|
usage.append(" [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]\n");
|
||||||
usage.append(" [--port port]\n");
|
usage.append(" [--port port]\n");
|
||||||
usage.append(" [-s SEED] [-v [-v] ...] [--throttle MILLISEC]\n");
|
usage.append(" [-s SEED] [-v [-v] ...]\n");
|
||||||
|
usage.append(" [--throttle MILLISEC] [--randomize-throttle]\n");
|
||||||
usage.append(" COUNT");
|
usage.append(" COUNT");
|
||||||
System.err.println(usage.toString());
|
System.err.println(usage.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,24 +17,38 @@
|
|||||||
package com.android.commands.monkey;
|
package com.android.commands.monkey;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class for keeping a monkey event queue
|
* class for keeping a monkey event queue
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class MonkeyEventQueue extends LinkedList<MonkeyEvent> {
|
public class MonkeyEventQueue extends LinkedList<MonkeyEvent> {
|
||||||
|
|
||||||
|
private Random mRandom;
|
||||||
private long mThrottle;
|
private long mThrottle;
|
||||||
|
private boolean mRandomizeThrottle;
|
||||||
public MonkeyEventQueue(long throttle) {
|
|
||||||
|
public MonkeyEventQueue(Random random, long throttle, boolean randomizeThrottle) {
|
||||||
super();
|
super();
|
||||||
|
mRandom = random;
|
||||||
mThrottle = throttle;
|
mThrottle = throttle;
|
||||||
|
mRandomizeThrottle = randomizeThrottle;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addLast(MonkeyEvent e) {
|
public void addLast(MonkeyEvent e) {
|
||||||
super.add(e);
|
super.add(e);
|
||||||
if (e.isThrottlable()) {
|
if (e.isThrottlable()) {
|
||||||
|
long throttle = mThrottle;
|
||||||
|
if (mRandomizeThrottle && (mThrottle > 0)) {
|
||||||
|
throttle = mRandom.nextLong();
|
||||||
|
if (throttle < 0) {
|
||||||
|
throttle = -throttle;
|
||||||
|
}
|
||||||
|
throttle %= mThrottle;
|
||||||
|
++throttle;
|
||||||
|
}
|
||||||
super.add(new MonkeyThrottleEvent(mThrottle));
|
super.add(new MonkeyThrottleEvent(mThrottle));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,7 @@ import android.view.KeyEvent;
|
|||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.WindowManagerImpl;
|
import android.view.WindowManagerImpl;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -202,7 +200,8 @@ public class MonkeySourceRandom implements MonkeyEventSource {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MonkeySourceRandom(long seed, ArrayList<ComponentName> MainApps, long throttle) {
|
public MonkeySourceRandom(Random random, ArrayList<ComponentName> MainApps,
|
||||||
|
long throttle, boolean randomizeThrottle) {
|
||||||
// default values for random distributions
|
// default values for random distributions
|
||||||
// note, these are straight percentages, to match user input (cmd line args)
|
// note, these are straight percentages, to match user input (cmd line args)
|
||||||
// but they will be converted to 0..1 values before the main loop runs.
|
// but they will be converted to 0..1 values before the main loop runs.
|
||||||
@@ -216,10 +215,9 @@ public class MonkeySourceRandom implements MonkeyEventSource {
|
|||||||
mFactors[FACTOR_FLIP] = 1.0f;
|
mFactors[FACTOR_FLIP] = 1.0f;
|
||||||
mFactors[FACTOR_ANYTHING] = 15.0f;
|
mFactors[FACTOR_ANYTHING] = 15.0f;
|
||||||
|
|
||||||
mRandom = new SecureRandom();
|
mRandom = random;
|
||||||
mRandom.setSeed((seed == 0) ? -1 : seed);
|
|
||||||
mMainApps = MainApps;
|
mMainApps = MainApps;
|
||||||
mQ = new MonkeyEventQueue(throttle);
|
mQ = new MonkeyEventQueue(random, throttle, randomizeThrottle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package com.android.commands.monkey;
|
package com.android.commands.monkey;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for generating MonkeyEvents from multiple scripts.
|
* Class for generating MonkeyEvents from multiple scripts.
|
||||||
@@ -36,7 +36,7 @@ public class MonkeySourceRandomScript implements MonkeyEventSource {
|
|||||||
private MonkeySourceScript mCurrentSource = null;
|
private MonkeySourceScript mCurrentSource = null;
|
||||||
|
|
||||||
/** The random number generator */
|
/** The random number generator */
|
||||||
private SecureRandom mRandom;
|
private Random mRandom;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a MonkeySourceRandomScript instance with an additional setup script.
|
* Creates a MonkeySourceRandomScript instance with an additional setup script.
|
||||||
@@ -44,21 +44,23 @@ public class MonkeySourceRandomScript implements MonkeyEventSource {
|
|||||||
* @param setupFileName The name of the setup script file on the device.
|
* @param setupFileName The name of the setup script file on the device.
|
||||||
* @param scriptFileNames An ArrayList of the names of the script files to be run randomly.
|
* @param scriptFileNames An ArrayList of the names of the script files to be run randomly.
|
||||||
* @param throttle The amount of time to sleep in ms between events.
|
* @param throttle The amount of time to sleep in ms between events.
|
||||||
* @param seed The seed of the random number generator.
|
* @param randomizeThrottle Whether to randomize throttle.
|
||||||
|
* @param random The random number generator.
|
||||||
*/
|
*/
|
||||||
public MonkeySourceRandomScript(String setupFileName, ArrayList<String> scriptFileNames,
|
public MonkeySourceRandomScript(String setupFileName, ArrayList<String> scriptFileNames,
|
||||||
long throttle, long seed) {
|
long throttle, boolean randomizeThrottle, Random random) {
|
||||||
if (setupFileName != null) {
|
if (setupFileName != null) {
|
||||||
mSetupSource = new MonkeySourceScript(setupFileName, throttle);
|
mSetupSource = new MonkeySourceScript(random, setupFileName, throttle,
|
||||||
|
randomizeThrottle);
|
||||||
mCurrentSource = mSetupSource;
|
mCurrentSource = mSetupSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String fileName: scriptFileNames) {
|
for (String fileName: scriptFileNames) {
|
||||||
mScriptSources.add(new MonkeySourceScript(fileName, throttle));
|
mScriptSources.add(new MonkeySourceScript(random, fileName, throttle,
|
||||||
|
randomizeThrottle));
|
||||||
}
|
}
|
||||||
|
|
||||||
mRandom = new SecureRandom();
|
mRandom = random;
|
||||||
mRandom.setSeed((seed == 0) ? -1 : seed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,10 +68,12 @@ public class MonkeySourceRandomScript implements MonkeyEventSource {
|
|||||||
*
|
*
|
||||||
* @param scriptFileNames An ArrayList of the names of the script files to be run randomly.
|
* @param scriptFileNames An ArrayList of the names of the script files to be run randomly.
|
||||||
* @param throttle The amount of time to sleep in ms between events.
|
* @param throttle The amount of time to sleep in ms between events.
|
||||||
* @param seed The seed of the random number generator.
|
* @param randomizeThrottle Whether to randomize throttle.
|
||||||
|
* @param random The random number generator.
|
||||||
*/
|
*/
|
||||||
public MonkeySourceRandomScript(ArrayList<String> scriptFileNames, long throttle, long seed) {
|
public MonkeySourceRandomScript(ArrayList<String> scriptFileNames, long throttle,
|
||||||
this(null, scriptFileNames, throttle, seed);
|
boolean randomizeThrottle, Random random) {
|
||||||
|
this(null, scriptFileNames, throttle, randomizeThrottle, random);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* monkey event queue. It takes a script to produce events sample script format:
|
* monkey event queue. It takes a script to produce events sample script format:
|
||||||
@@ -115,9 +116,10 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
* @param filename The filename of the script (on the device).
|
* @param filename The filename of the script (on the device).
|
||||||
* @param throttle The amount of time in ms to sleep between events.
|
* @param throttle The amount of time in ms to sleep between events.
|
||||||
*/
|
*/
|
||||||
public MonkeySourceScript(String filename, long throttle) {
|
public MonkeySourceScript(Random random, String filename, long throttle,
|
||||||
|
boolean randomizeThrottle) {
|
||||||
mScriptFileName = filename;
|
mScriptFileName = filename;
|
||||||
mQ = new MonkeyEventQueue(throttle);
|
mQ = new MonkeyEventQueue(random, throttle, randomizeThrottle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user