Add support for random throttle.

See http://b/1716328
This commit is contained in:
Ying Wang
2010-02-01 11:41:29 -08:00
parent 1c2669905d
commit bae4515153
5 changed files with 62 additions and 28 deletions

View File

@@ -39,10 +39,12 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/**
* Application that injects random key events and other actions into the system.
@@ -140,12 +142,18 @@ public class Monkey {
/** The delay between event inputs **/
long mThrottle = 0;
/** Whether to randomize each throttle (0-mThrottle ms) inserted between events. */
boolean mRandomizeThrottle = false;
/** The number of iterations **/
int mCount = 1000;
/** The random number seed **/
long mSeed = 0;
/** The random number generator **/
Random mRandom = null;
/** Dropped-event statistics **/
long mDroppedKeyEvents = 0;
@@ -421,19 +429,24 @@ public class Monkey {
return -4;
}
mRandom = new SecureRandom();
mRandom.setSeed((mSeed == 0) ? -1 : mSeed);
if (mScriptFileNames != null && mScriptFileNames.size() == 1) {
// script mode, ignore other options
mEventSource = new MonkeySourceScript(mScriptFileNames.get(0), mThrottle);
mEventSource = new MonkeySourceScript(mRandom, mScriptFileNames.get(0), mThrottle,
mRandomizeThrottle);
mEventSource.setVerbose(mVerbose);
mCountEvents = false;
} else if (mScriptFileNames != null && mScriptFileNames.size() > 1) {
if (mSetupFileName != null) {
mEventSource = new MonkeySourceRandomScript(mSetupFileName, mScriptFileNames,
mThrottle, mSeed);
mThrottle, mRandomizeThrottle, mRandom);
mCount++;
} else {
mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle, mSeed);
mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle,
mRandomizeThrottle, mRandom);
}
mEventSource.setVerbose(mVerbose);
mCountEvents = false;
@@ -450,7 +463,7 @@ public class Monkey {
if (mVerbose >= 2) { // check seeding performance
System.out.println("// Seeded: " + mSeed);
}
mEventSource = new MonkeySourceRandom(mSeed, mMainApps, mThrottle);
mEventSource = new MonkeySourceRandom(mRandom, mMainApps, mThrottle, mRandomizeThrottle);
mEventSource.setVerbose(mVerbose);
// set any of the factors that has been set
for (int i = 0; i < MonkeySourceRandom.FACTORZ_COUNT; i++) {
@@ -599,6 +612,8 @@ public class Monkey {
mPkgWhitelistFile = nextOptionData();
} else if (opt.equals("--throttle")) {
mThrottle = nextOptionLong("delay (in milliseconds) to wait between events");
} else if (opt.equals("--randomize-throttle")) {
mRandomizeThrottle = true;
} else if (opt.equals("--wait-dbg")) {
// do nothing - it's caught at the very start of run()
} else if (opt.equals("--dbg-no-events")) {
@@ -1068,7 +1083,8 @@ public class Monkey {
usage.append(" [--wait-dbg] [--dbg-no-events]\n");
usage.append(" [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]\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");
System.err.println(usage.toString());
}

View File

@@ -17,6 +17,7 @@
package com.android.commands.monkey;
import java.util.LinkedList;
import java.util.Random;
/**
* class for keeping a monkey event queue
@@ -24,17 +25,30 @@ import java.util.LinkedList;
@SuppressWarnings("serial")
public class MonkeyEventQueue extends LinkedList<MonkeyEvent> {
private Random mRandom;
private long mThrottle;
private boolean mRandomizeThrottle;
public MonkeyEventQueue(long throttle) {
public MonkeyEventQueue(Random random, long throttle, boolean randomizeThrottle) {
super();
mRandom = random;
mThrottle = throttle;
mRandomizeThrottle = randomizeThrottle;
}
@Override
public void addLast(MonkeyEvent e) {
super.add(e);
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));
}
}

View File

@@ -23,9 +23,7 @@ import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManagerImpl;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
/**
@@ -202,7 +200,8 @@ public class MonkeySourceRandom implements MonkeyEventSource {
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
// 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.
@@ -216,10 +215,9 @@ public class MonkeySourceRandom implements MonkeyEventSource {
mFactors[FACTOR_FLIP] = 1.0f;
mFactors[FACTOR_ANYTHING] = 15.0f;
mRandom = new SecureRandom();
mRandom.setSeed((seed == 0) ? -1 : seed);
mRandom = random;
mMainApps = MainApps;
mQ = new MonkeyEventQueue(throttle);
mQ = new MonkeyEventQueue(random, throttle, randomizeThrottle);
}
/**

View File

@@ -16,8 +16,8 @@
package com.android.commands.monkey;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Random;
/**
* Class for generating MonkeyEvents from multiple scripts.
@@ -36,7 +36,7 @@ public class MonkeySourceRandomScript implements MonkeyEventSource {
private MonkeySourceScript mCurrentSource = null;
/** The random number generator */
private SecureRandom mRandom;
private Random mRandom;
/**
* 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 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 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,
long throttle, long seed) {
long throttle, boolean randomizeThrottle, Random random) {
if (setupFileName != null) {
mSetupSource = new MonkeySourceScript(setupFileName, throttle);
mSetupSource = new MonkeySourceScript(random, setupFileName, throttle,
randomizeThrottle);
mCurrentSource = mSetupSource;
}
for (String fileName: scriptFileNames) {
mScriptSources.add(new MonkeySourceScript(fileName, throttle));
mScriptSources.add(new MonkeySourceScript(random, fileName, throttle,
randomizeThrottle));
}
mRandom = new SecureRandom();
mRandom.setSeed((seed == 0) ? -1 : seed);
mRandom = random;
}
/**
@@ -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 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) {
this(null, scriptFileNames, throttle, seed);
public MonkeySourceRandomScript(ArrayList<String> scriptFileNames, long throttle,
boolean randomizeThrottle, Random random) {
this(null, scriptFileNames, throttle, randomizeThrottle, random);
}
/**

View File

@@ -26,6 +26,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
import java.util.Random;
/**
* 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 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;
mQ = new MonkeyEventQueue(throttle);
mQ = new MonkeyEventQueue(random, throttle, randomizeThrottle);
}
/**