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.test.tilebenchmark; 18 19 import com.test.tilebenchmark.ProfileActivity.ProfileCallback; 20 21 import java.io.File; 22 import java.util.HashMap; 23 import java.util.Map; 24 25 import android.content.res.Resources; 26 import android.os.Bundle; 27 import android.os.Environment; 28 import android.test.ActivityInstrumentationTestCase2; 29 import android.util.Log; 30 31 public class PerformanceTest extends 32 ActivityInstrumentationTestCase2<ProfileActivity> { 33 34 private class StatAggregator extends PlaybackGraphs { 35 private HashMap<String, Double> mDataMap = new HashMap<String, Double>(); 36 private int mCount = 0; 37 38 public void aggregate() { 39 mCount++; 40 Resources resources = mView.getResources(); 41 for (int metricIndex = 0; metricIndex < Metrics.length; metricIndex++) { 42 for (int statIndex = 0; statIndex < Stats.length; statIndex++) { 43 String metricLabel = resources.getString( 44 Metrics[metricIndex].getLabelId()); 45 String statLabel = resources.getString( 46 Stats[statIndex].getLabelId()); 47 48 String label = metricLabel + " " + statLabel; 49 double aggVal = mDataMap.containsKey(label) ? mDataMap 50 .get(label) : 0; 51 52 aggVal += mStats[metricIndex][statIndex]; 53 mDataMap.put(label, aggVal); 54 } 55 } 56 for (Map.Entry<String, Double> e : mSingleStats.entrySet()) { 57 double aggVal = mDataMap.containsKey(e.getKey()) 58 ? mDataMap.get(e.getKey()) : 0; 59 mDataMap.put(e.getKey(), aggVal + e.getValue()); 60 } 61 } 62 63 public Bundle getBundle() { 64 Bundle b = new Bundle(); 65 int count = 0 == mCount ? Integer.MAX_VALUE : mCount; 66 for (Map.Entry<String, Double> e : mDataMap.entrySet()) { 67 b.putDouble(e.getKey(), e.getValue() / count); 68 } 69 return b; 70 } 71 } 72 73 ProfileActivity mActivity; 74 ProfiledWebView mView; 75 StatAggregator mStats = new StatAggregator(); 76 77 private static final String LOGTAG = "PerformanceTest"; 78 private static final String TEST_LOCATION = "webkit/page_cycler"; 79 private static final String URL_PREFIX = "file://"; 80 private static final String URL_POSTFIX = "/index.html?skip=true"; 81 private static final int MAX_ITERATIONS = 4; 82 private static final String TEST_DIRS[] = { 83 "intl1"//, "alexa_us", "android", "dom", "intl2", "moz", "moz2" 84 }; 85 86 public PerformanceTest() { 87 super(ProfileActivity.class); 88 } 89 90 @Override 91 protected void setUp() throws Exception { 92 super.setUp(); 93 mActivity = getActivity(); 94 mView = (ProfiledWebView) mActivity.findViewById(R.id.web); 95 } 96 97 private boolean loadUrl(final String url) { 98 try { 99 Log.d(LOGTAG, "test starting for url " + url); 100 mActivity.runOnUiThread(new Runnable() { 101 @Override 102 public void run() { 103 mView.loadUrl(url); 104 } 105 }); 106 synchronized (mStats) { 107 mStats.wait(); 108 } 109 mStats.aggregate(); 110 } catch (InterruptedException e) { 111 e.printStackTrace(); 112 return false; 113 } 114 return true; 115 } 116 117 private boolean runIteration() { 118 File sdFile = Environment.getExternalStorageDirectory(); 119 for (String testDirName : TEST_DIRS) { 120 File testDir = new File(sdFile, TEST_LOCATION + "/" + testDirName); 121 Log.d(LOGTAG, "Testing dir: '" + testDir.getAbsolutePath() 122 + "', exists=" + testDir.exists()); 123 for (File siteDir : testDir.listFiles()) { 124 if (!siteDir.isDirectory()) 125 continue; 126 127 if (!loadUrl(URL_PREFIX + siteDir.getAbsolutePath() 128 + URL_POSTFIX)) { 129 return false; 130 } 131 } 132 } 133 return true; 134 } 135 136 public void testMetrics() { 137 String state = Environment.getExternalStorageState(); 138 139 if (!Environment.MEDIA_MOUNTED.equals(state) 140 && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 141 Log.d(LOGTAG, "ARG Can't access sd card!"); 142 // Can't read the SD card, fail and die! 143 getInstrumentation().sendStatus(1, null); 144 return; 145 } 146 147 // use mGraphs as a condition variable between the UI thread and 148 // this(the testing) thread 149 mActivity.setCallback(new ProfileCallback() { 150 @Override 151 public void profileCallback(RunData data) { 152 Log.d(LOGTAG, "test completion callback"); 153 mStats.setData(data); 154 synchronized (mStats) { 155 mStats.notify(); 156 } 157 } 158 }); 159 160 for (int i = 0; i < MAX_ITERATIONS; i++) 161 if (!runIteration()) { 162 getInstrumentation().sendStatus(1, null); 163 return; 164 } 165 getInstrumentation().sendStatus(0, mStats.getBundle()); 166 } 167 } 168