Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.frameworkperf; |
| 18 | |
| 19 | import android.app.Activity; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 22 | import android.content.ServiceConnection; |
Mohamad Ayyash | c508d03 | 2014-08-25 19:27:57 -0700 | [diff] [blame] | 23 | import android.graphics.Color; |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 24 | import android.os.Binder; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.Handler; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 27 | import android.os.IBinder; |
| 28 | import android.os.Message; |
| 29 | import android.os.Messenger; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 30 | import android.os.PowerManager; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.WindowManager; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 35 | import android.widget.AdapterView; |
| 36 | import android.widget.ArrayAdapter; |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 37 | import android.widget.Button; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 38 | import android.widget.CheckBox; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 39 | import android.widget.Spinner; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 40 | import android.widget.TextView; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | |
| 44 | /** |
| 45 | * So you thought sync used up your battery life. |
| 46 | */ |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 47 | public class FrameworkPerfActivity extends Activity |
| 48 | implements AdapterView.OnItemSelectedListener { |
Dianne Hackborn | 2e9562a | 2011-10-28 17:12:12 -0700 | [diff] [blame] | 49 | static final String TAG = "Perf"; |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 50 | static final boolean DEBUG = false; |
Dianne Hackborn | 2e9562a | 2011-10-28 17:12:12 -0700 | [diff] [blame] | 51 | |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 52 | Spinner mFgSpinner; |
| 53 | Spinner mBgSpinner; |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 54 | Spinner mLimitSpinner; |
| 55 | TextView mLimitLabel; |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 56 | TextView mTestTime; |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 57 | Button mStartButton; |
| 58 | Button mStopButton; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 59 | CheckBox mLocalCheckBox; |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 60 | TextView mLog; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 61 | PowerManager.WakeLock mPartialWakeLock; |
| 62 | |
| 63 | long mMaxRunTime = 5000; |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 64 | boolean mLimitIsIterations; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 65 | boolean mStarted; |
| 66 | |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 67 | final String[] mAvailOpLabels; |
| 68 | final String[] mAvailOpDescriptions; |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 69 | final String[] mLimitLabels = { "Time", "Iterations" }; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 70 | |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 71 | int mFgTestIndex = -1; |
| 72 | int mBgTestIndex = -1; |
| 73 | TestService.Op mFgTest; |
| 74 | TestService.Op mBgTest; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 75 | int mCurOpIndex = 0; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 76 | TestConnection mCurConnection; |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 77 | boolean mConnectionBound; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 78 | |
| 79 | final ArrayList<RunResult> mResults = new ArrayList<RunResult>(); |
| 80 | |
Guang Zhu | 5eefd77 | 2011-11-17 14:32:41 -0800 | [diff] [blame] | 81 | Object mResultNotifier = new Object(); |
| 82 | |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 83 | class TestConnection implements ServiceConnection, IBinder.DeathRecipient { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 84 | Messenger mService; |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 85 | boolean mLinked; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 86 | |
| 87 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 88 | try { |
| 89 | if (!(service instanceof Binder)) { |
| 90 | // If remote, we'll be killing ye. |
| 91 | service.linkToDeath(this, 0); |
| 92 | mLinked = true; |
| 93 | } |
| 94 | mService = new Messenger(service); |
| 95 | dispatchCurOp(this); |
| 96 | } catch (RemoteException e) { |
| 97 | // Whoops, service has disappeared... try starting again. |
| 98 | Log.w(TAG, "Test service died, starting again"); |
| 99 | startCurOp(); |
| 100 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override public void onServiceDisconnected(ComponentName name) { |
| 104 | } |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 105 | |
| 106 | @Override public void binderDied() { |
| 107 | cleanup(); |
| 108 | connectionDied(this); |
| 109 | } |
| 110 | |
| 111 | void cleanup() { |
| 112 | if (mLinked) { |
| 113 | mLinked = false; |
| 114 | mService.getBinder().unlinkToDeath(this, 0); |
| 115 | } |
| 116 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 119 | static final int MSG_DO_NEXT_TEST = 1000; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 120 | |
| 121 | final Handler mHandler = new Handler() { |
| 122 | @Override public void handleMessage(Message msg) { |
| 123 | switch (msg.what) { |
| 124 | case TestService.RES_TEST_FINISHED: { |
| 125 | Bundle bundle = (Bundle)msg.obj; |
| 126 | bundle.setClassLoader(getClassLoader()); |
| 127 | RunResult res = (RunResult)bundle.getParcelable("res"); |
| 128 | completeCurOp(res); |
| 129 | } break; |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 130 | case MSG_DO_NEXT_TEST: { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 131 | startCurOp(); |
| 132 | } break; |
| 133 | } |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | final Messenger mMessenger = new Messenger(mHandler); |
| 138 | |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 139 | public FrameworkPerfActivity() { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 140 | mAvailOpLabels = new String[TestService.mAvailOps.length]; |
| 141 | mAvailOpDescriptions = new String[TestService.mAvailOps.length]; |
| 142 | for (int i=0; i<TestService.mAvailOps.length; i++) { |
| 143 | TestService.Op op = TestService.mAvailOps[i]; |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 144 | if (op == null) { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 145 | mAvailOpLabels[i] = "All"; |
| 146 | mAvailOpDescriptions[i] = "All tests"; |
| 147 | } else { |
| 148 | mAvailOpLabels[i] = op.getName(); |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 149 | if (mAvailOpLabels[i] == null) { |
| 150 | mAvailOpLabels[i] = "Nothing"; |
| 151 | } |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 152 | mAvailOpDescriptions[i] = op.getLongName(); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public void onCreate(Bundle savedInstanceState) { |
| 159 | super.onCreate(savedInstanceState); |
| 160 | |
| 161 | // Set the layout for this activity. You can find it |
| 162 | // in res/layout/hello_activity.xml |
| 163 | setContentView(R.layout.main); |
| 164 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 165 | mFgSpinner = findViewById(R.id.fgspinner); |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 166 | ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, |
| 167 | android.R.layout.simple_spinner_item, mAvailOpLabels); |
| 168 | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| 169 | mFgSpinner.setAdapter(adapter); |
| 170 | mFgSpinner.setOnItemSelectedListener(this); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 171 | mBgSpinner = findViewById(R.id.bgspinner); |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 172 | adapter = new ArrayAdapter<String>(this, |
| 173 | android.R.layout.simple_spinner_item, mAvailOpLabels); |
| 174 | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| 175 | mBgSpinner.setAdapter(adapter); |
| 176 | mBgSpinner.setOnItemSelectedListener(this); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 177 | mLimitSpinner = findViewById(R.id.limitspinner); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 178 | adapter = new ArrayAdapter<String>(this, |
| 179 | android.R.layout.simple_spinner_item, mLimitLabels); |
| 180 | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| 181 | mLimitSpinner.setAdapter(adapter); |
| 182 | mLimitSpinner.setOnItemSelectedListener(this); |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 183 | |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 184 | mTestTime = (TextView)findViewById(R.id.testtime); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 185 | mLimitLabel = (TextView)findViewById(R.id.limitlabel); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 186 | |
| 187 | mStartButton = (Button)findViewById(R.id.start); |
| 188 | mStartButton.setOnClickListener(new View.OnClickListener() { |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 189 | @Override public void onClick(View v) { |
| 190 | startRunning(); |
| 191 | } |
| 192 | }); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 193 | mStopButton = (Button)findViewById(R.id.stop); |
| 194 | mStopButton.setOnClickListener(new View.OnClickListener() { |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 195 | @Override public void onClick(View v) { |
| 196 | stopRunning(); |
| 197 | } |
| 198 | }); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 199 | mStopButton.setEnabled(false); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 200 | mLocalCheckBox = (CheckBox)findViewById(R.id.local); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 201 | |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 202 | mLog = (TextView)findViewById(R.id.log); |
Mohamad Ayyash | c508d03 | 2014-08-25 19:27:57 -0700 | [diff] [blame] | 203 | mLog.setTextColor(Color.RED); |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 204 | |
| 205 | PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); |
| 206 | mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Scheduler"); |
| 207 | mPartialWakeLock.setReferenceCounted(false); |
| 208 | } |
| 209 | |
| 210 | @Override |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 211 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 212 | if (parent == mFgSpinner || parent == mBgSpinner || parent == mLimitSpinner) { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 213 | TestService.Op op = TestService.mAvailOps[position]; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 214 | if (parent == mFgSpinner) { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 215 | mFgTestIndex = position; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 216 | mFgTest = op; |
| 217 | ((TextView)findViewById(R.id.fgtext)).setText(mAvailOpDescriptions[position]); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 218 | } else if (parent == mBgSpinner) { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 219 | mBgTestIndex = position; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 220 | mBgTest = op; |
| 221 | ((TextView)findViewById(R.id.bgtext)).setText(mAvailOpDescriptions[position]); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 222 | } else if (parent == mLimitSpinner) { |
| 223 | mLimitIsIterations = (position != 0); |
| 224 | if (mLimitIsIterations) { |
| 225 | mLimitLabel.setText("Iterations: "); |
| 226 | } else { |
| 227 | mLimitLabel.setText("Test time (ms): "); |
| 228 | } |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onNothingSelected(AdapterView<?> parent) { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 238 | public void onResume() { |
| 239 | super.onResume(); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void onDestroy() { |
| 244 | super.onDestroy(); |
| 245 | stopRunning(); |
| 246 | if (mPartialWakeLock.isHeld()) { |
| 247 | mPartialWakeLock.release(); |
| 248 | } |
| 249 | } |
| 250 | |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 251 | void dispatchCurOp(TestConnection conn) { |
| 252 | if (mCurConnection != conn) { |
| 253 | Log.w(TAG, "Dispatching on invalid connection: " + conn); |
| 254 | return; |
| 255 | } |
| 256 | TestArgs args = new TestArgs(); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 257 | if (mLimitIsIterations) { |
| 258 | args.maxOps = mMaxRunTime; |
| 259 | } else { |
| 260 | args.maxTime = mMaxRunTime; |
| 261 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 262 | if (mFgTestIndex == 0 && mBgTestIndex == 0) { |
| 263 | args.combOp = mCurOpIndex; |
| 264 | } else if (mFgTestIndex != 0 && mBgTestIndex != 0) { |
| 265 | args.fgOp = mFgTestIndex; |
| 266 | args.bgOp = mBgTestIndex; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 267 | } else { |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 268 | // Skip null test. |
| 269 | if (mCurOpIndex == 0) { |
| 270 | mCurOpIndex = 1; |
| 271 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 272 | if (mFgTestIndex != 0) { |
| 273 | args.fgOp = mFgTestIndex; |
| 274 | args.bgOp = mCurOpIndex; |
| 275 | } else { |
| 276 | args.fgOp = mCurOpIndex; |
Dianne Hackborn | df7221c | 2013-02-26 14:53:55 -0800 | [diff] [blame] | 277 | args.bgOp = mBgTestIndex; |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 278 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 279 | } |
| 280 | Bundle bundle = new Bundle(); |
| 281 | bundle.putParcelable("args", args); |
| 282 | Message msg = Message.obtain(null, TestService.CMD_START_TEST, bundle); |
| 283 | msg.replyTo = mMessenger; |
| 284 | try { |
| 285 | conn.mService.send(msg); |
| 286 | } catch (RemoteException e) { |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 287 | Log.w(TAG, "Failure communicating with service", e); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | void completeCurOp(RunResult result) { |
| 292 | log(String.format("%s: fg=%d*%gms/op (%dms) / bg=%d*%gms/op (%dms)", |
| 293 | result.name, result.fgOps, result.getFgMsPerOp(), result.fgTime, |
| 294 | result.bgOps, result.getBgMsPerOp(), result.bgTime)); |
Guang Zhu | 5eefd77 | 2011-11-17 14:32:41 -0800 | [diff] [blame] | 295 | synchronized (mResults) { |
| 296 | mResults.add(result); |
| 297 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 298 | if (!mStarted) { |
| 299 | log("Stop"); |
| 300 | stopRunning(); |
| 301 | return; |
| 302 | } |
| 303 | if (mFgTest != null && mBgTest != null) { |
| 304 | log("Finished"); |
| 305 | stopRunning(); |
| 306 | return; |
| 307 | } |
| 308 | if (mFgTest == null && mBgTest == null) { |
| 309 | mCurOpIndex+=2; |
| 310 | if (mCurOpIndex >= TestService.mOpPairs.length) { |
| 311 | log("Finished"); |
| 312 | stopRunning(); |
| 313 | return; |
| 314 | } |
| 315 | } else { |
| 316 | mCurOpIndex++; |
| 317 | if (mCurOpIndex >= TestService.mAvailOps.length) { |
| 318 | log("Finished"); |
| 319 | stopRunning(); |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | startCurOp(); |
| 324 | } |
| 325 | |
| 326 | void disconnect() { |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 327 | final TestConnection conn = mCurConnection; |
| 328 | if (conn != null) { |
| 329 | if (DEBUG) { |
| 330 | RuntimeException here = new RuntimeException("here"); |
| 331 | here.fillInStackTrace(); |
| 332 | Log.i(TAG, "Unbinding " + conn, here); |
| 333 | } |
| 334 | if (mConnectionBound) { |
| 335 | unbindService(conn); |
| 336 | mConnectionBound = false; |
| 337 | } |
| 338 | if (conn.mLinked) { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 339 | Message msg = Message.obtain(null, TestService.CMD_TERMINATE); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 340 | try { |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 341 | conn.mService.send(msg); |
| 342 | return; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 343 | } catch (RemoteException e) { |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 344 | Log.w(TAG, "Test service aleady died when terminating"); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 347 | conn.cleanup(); |
| 348 | } |
| 349 | connectionDied(conn); |
| 350 | } |
| 351 | |
| 352 | void connectionDied(TestConnection conn) { |
| 353 | if (mCurConnection == conn) { |
| 354 | // Now that we know the test process has died, we can commence |
| 355 | // the next test. Just give a little delay to allow the activity |
| 356 | // manager to know it has died as well (not a disaster if it hasn't |
| 357 | // yet, though). |
| 358 | if (mConnectionBound) { |
| 359 | unbindService(conn); |
| 360 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 361 | mCurConnection = null; |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 362 | mHandler.sendMessageDelayed(Message.obtain(null, MSG_DO_NEXT_TEST), 100); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | void startCurOp() { |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 367 | if (DEBUG) Log.i(TAG, "startCurOp: mCurConnection=" + mCurConnection); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 368 | if (mCurConnection != null) { |
| 369 | disconnect(); |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 370 | return; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 371 | } |
| 372 | if (mStarted) { |
| 373 | mHandler.removeMessages(TestService.RES_TEST_FINISHED); |
| 374 | mHandler.removeMessages(TestService.RES_TERMINATED); |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 375 | mHandler.removeMessages(MSG_DO_NEXT_TEST); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 376 | mCurConnection = new TestConnection(); |
| 377 | Intent intent; |
| 378 | if (mLocalCheckBox.isChecked()) { |
| 379 | intent = new Intent(this, LocalTestService.class); |
| 380 | } else { |
| 381 | intent = new Intent(this, TestService.class); |
| 382 | } |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 383 | if (DEBUG) { |
| 384 | RuntimeException here = new RuntimeException("here"); |
| 385 | here.fillInStackTrace(); |
| 386 | Log.i(TAG, "Binding " + mCurConnection, here); |
| 387 | } |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 388 | bindService(intent, mCurConnection, BIND_AUTO_CREATE|BIND_IMPORTANT); |
Dianne Hackborn | dd13793 | 2011-11-08 10:59:28 -0800 | [diff] [blame] | 389 | mConnectionBound = true; |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 390 | } |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void startRunning() { |
| 394 | if (!mStarted) { |
| 395 | log("Start"); |
| 396 | mStarted = true; |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 397 | mStartButton.setEnabled(false); |
| 398 | mStopButton.setEnabled(true); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 399 | mLocalCheckBox.setEnabled(false); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 400 | mTestTime.setEnabled(false); |
| 401 | mFgSpinner.setEnabled(false); |
| 402 | mBgSpinner.setEnabled(false); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 403 | mLimitSpinner.setEnabled(false); |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 404 | updateWakeLock(); |
| 405 | startService(new Intent(this, SchedulerService.class)); |
| 406 | mCurOpIndex = 0; |
Dianne Hackborn | 0500b3c | 2011-11-01 15:28:43 -0700 | [diff] [blame] | 407 | mMaxRunTime = Integer.parseInt(mTestTime.getText().toString()); |
Guang Zhu | 5eefd77 | 2011-11-17 14:32:41 -0800 | [diff] [blame] | 408 | synchronized (mResults) { |
| 409 | mResults.clear(); |
| 410 | } |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 411 | startCurOp(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void stopRunning() { |
| 416 | if (mStarted) { |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 417 | disconnect(); |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 418 | mStarted = false; |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 419 | mStartButton.setEnabled(true); |
| 420 | mStopButton.setEnabled(false); |
Dianne Hackborn | 6b57b7e | 2011-11-07 14:45:49 -0800 | [diff] [blame] | 421 | mLocalCheckBox.setEnabled(true); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 422 | mTestTime.setEnabled(true); |
| 423 | mFgSpinner.setEnabled(true); |
| 424 | mBgSpinner.setEnabled(true); |
Christopher Tate | 80a682b | 2011-11-18 17:14:14 -0800 | [diff] [blame] | 425 | mLimitSpinner.setEnabled(true); |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 426 | updateWakeLock(); |
| 427 | stopService(new Intent(this, SchedulerService.class)); |
Guang Zhu | 5eefd77 | 2011-11-17 14:32:41 -0800 | [diff] [blame] | 428 | synchronized (mResults) { |
Dianne Hackborn | df7221c | 2013-02-26 14:53:55 -0800 | [diff] [blame] | 429 | Log.i("PerfRes", "\tTEST\tFgOps\tFgMsPerOp\tFgTime\tFgName\tBgOps\tBgMsPerOp\t" |
| 430 | + "BgTime\tBgName"); |
Guang Zhu | 5eefd77 | 2011-11-17 14:32:41 -0800 | [diff] [blame] | 431 | for (int i=0; i<mResults.size(); i++) { |
| 432 | RunResult result = mResults.get(i); |
| 433 | float fgMsPerOp = result.getFgMsPerOp(); |
| 434 | float bgMsPerOp = result.getBgMsPerOp(); |
| 435 | String fgMsPerOpStr = fgMsPerOp != 0 ? Float.toString(fgMsPerOp) : ""; |
| 436 | String bgMsPerOpStr = bgMsPerOp != 0 ? Float.toString(bgMsPerOp) : ""; |
| 437 | Log.i("PerfRes", "\t" + result.name + "\t" + result.fgOps |
| 438 | + "\t" + result.getFgMsPerOp() + "\t" + result.fgTime |
| 439 | + "\t" + result.fgLongName + "\t" + result.bgOps |
| 440 | + "\t" + result.getBgMsPerOp() + "\t" + result.bgTime |
| 441 | + "\t" + result.bgLongName); |
| 442 | } |
| 443 | } |
| 444 | synchronized (mResultNotifier) { |
| 445 | mResultNotifier.notifyAll(); |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void updateWakeLock() { |
| 451 | if (mStarted) { |
| 452 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 453 | if (!mPartialWakeLock.isHeld()) { |
| 454 | mPartialWakeLock.acquire(); |
| 455 | } |
| 456 | } else { |
| 457 | getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 458 | if (mPartialWakeLock.isHeld()) { |
| 459 | mPartialWakeLock.release(); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void log(String s) { |
| 465 | mLog.setText(mLog.getText() + "\n" + s); |
Dianne Hackborn | 2e9562a | 2011-10-28 17:12:12 -0700 | [diff] [blame] | 466 | Log.i(TAG, s); |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 467 | } |
Dianne Hackborn | d4c4b76 | 2011-10-27 12:58:16 -0700 | [diff] [blame] | 468 | } |