From 3e456daa45dacf81bb339dfdde46b606ec6b9c7a Mon Sep 17 00:00:00 2001 From: Jim Miller Date: Wed, 22 Apr 2009 17:04:48 -0700 Subject: [PATCH] Add network statistics collection to monkey runs. --- .../com/android/commands/monkey/Monkey.java | 14 ++- .../commands/monkey/MonkeyNetworkMonitor.java | 105 ++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 cmds/monkey/src/com/android/commands/monkey/MonkeyNetworkMonitor.java diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java index fe0de9968..5e9f07c8c 100644 --- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java +++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java @@ -130,7 +130,8 @@ public class Monkey { float[] mFactors = new float[MonkeySourceRandom.FACTORZ_COUNT]; MonkeyEventSource mEventSource; - + private MonkeyNetworkMonitor mNetworkMonitor = new MonkeyNetworkMonitor(); + /** * Monitor operations happening in the system. */ @@ -222,14 +223,14 @@ public class Monkey { return 1; } } - + /** * Run the procrank tool to insert system status information into the debug report. */ private void reportProcRank() { commandLineReport("procrank", "procrank"); } - + /** * Run "cat /data/anr/traces.txt". Wait about 5 seconds first, to let the asynchronous * report writing complete. @@ -401,7 +402,9 @@ public class Monkey { signalPersistentProcesses(); } + mNetworkMonitor.start(); int crashedAtCycle = runMonkeyCycles(); + mNetworkMonitor.stop(); synchronized (this) { if (mRequestAnrTraces) { @@ -423,6 +426,7 @@ public class Monkey { try { mAm.setActivityWatcher(null); + mNetworkMonitor.unregister(mAm); } catch (RemoteException e) { // just in case this was latent (after mCount cycles), make sure // we report it @@ -442,6 +446,9 @@ public class Monkey { System.out.print(" flips="); System.out.println(mDroppedFlipEvents); } + + // report network stats + mNetworkMonitor.dump(); if (crashedAtCycle < mCount - 1) { System.err.println("** System appears to have crashed at event " @@ -602,6 +609,7 @@ public class Monkey { try { mAm.setActivityWatcher(new ActivityWatcher()); + mNetworkMonitor.register(mAm); } catch (RemoteException e) { System.err.println("** Failed talking with activity manager!"); return false; diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyNetworkMonitor.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyNetworkMonitor.java new file mode 100644 index 000000000..e8d98126d --- /dev/null +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyNetworkMonitor.java @@ -0,0 +1,105 @@ +/** +** Copyright 2007, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.commands.monkey; + +import android.app.IActivityManager; +import android.app.IIntentReceiver; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Bundle; +import android.os.RemoteException; +import android.os.SystemClock; + +/** + * Class for monitoring network connectivity during monkey runs. + */ +public class MonkeyNetworkMonitor extends IIntentReceiver.Stub { + private static final boolean LDEBUG = false; + private final IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); + private long mCollectionStartTime; // time we started collecting data + private long mEventTime; // time of last event (connect, disconnect, etc.) + private int mLastNetworkType = -1; // unknown + private long mWifiElapsedTime = 0; // accumulated time spent on wifi since start() + private long mMobileElapsedTime = 0; // accumulated time spent on mobile since start() + private long mElapsedTime = 0; // amount of time spent between start() and stop() + + public void performReceive(Intent intent, int resultCode, String data, Bundle extras, + boolean ordered) throws RemoteException { + NetworkInfo ni = (NetworkInfo) intent.getParcelableExtra( + ConnectivityManager.EXTRA_NETWORK_INFO); + if (LDEBUG) System.out.println("Network state changed: " + + "type=" + ni.getType() + ", state=" + ni.getState()); + updateNetworkStats(); + if (NetworkInfo.State.CONNECTED == ni.getState()) { + if (LDEBUG) System.out.println("Network connected"); + mLastNetworkType = ni.getType(); + } else if (NetworkInfo.State.DISCONNECTED == ni.getState()) { + if (LDEBUG) System.out.println("Network not connected"); + mLastNetworkType = -1; // unknown since we're disconnected + } + mEventTime = SystemClock.elapsedRealtime(); + } + + private void updateNetworkStats() { + long timeNow = SystemClock.elapsedRealtime(); + long delta = timeNow - mEventTime; + switch (mLastNetworkType) { + case ConnectivityManager.TYPE_MOBILE: + if (LDEBUG) System.out.println("Adding to mobile: " + delta); + mMobileElapsedTime += delta; + break; + case ConnectivityManager.TYPE_WIFI: + if (LDEBUG) System.out.println("Adding to wifi: " + delta); + mWifiElapsedTime += delta; + break; + default: + if (LDEBUG) System.out.println("Unaccounted for: " + delta); + break; + } + mElapsedTime = timeNow - mCollectionStartTime; + } + + public void start() { + mWifiElapsedTime = 0; + mMobileElapsedTime = 0; + mElapsedTime = 0; + mEventTime = mCollectionStartTime = SystemClock.elapsedRealtime(); + } + + public void register(IActivityManager am) throws RemoteException { + if (LDEBUG) System.out.println("registering Receiver"); + am.registerReceiver(null, this, filter, null); + } + + public void unregister(IActivityManager am) throws RemoteException { + if (LDEBUG) System.out.println("unregistering Receiver"); + am.unregisterReceiver(this); + } + + public void stop() { + updateNetworkStats(); + } + + public void dump() { + System.out.println("## Network stats: elapsed time=" + mElapsedTime + "ms (" + + mMobileElapsedTime + "ms mobile, " + + mWifiElapsedTime + "ms wifi, " + + (mElapsedTime - mMobileElapsedTime - mWifiElapsedTime) + "ms not connected)"); + } + } \ No newline at end of file