├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── app ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── google │ │ └── android │ │ └── perftesting │ │ ├── ApplicationTest.java │ │ ├── MainActivityTest.java │ │ ├── SimpleListActivityTest.java │ │ ├── TestListener.java │ │ ├── common │ │ ├── PerfTest.java │ │ └── PerfTestingUtils.java │ │ └── testrules │ │ ├── EnableBatteryStatsDump.java │ │ ├── EnableDeviceGetPropsInfo.java │ │ ├── EnableLogcatDump.java │ │ ├── EnableNetStatsDump.java │ │ ├── EnablePerTestTraceFile.java │ │ ├── EnablePostTestDumpsys.java │ │ └── EnableTestTracing.java │ ├── debug │ └── AndroidManifest.xml │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── google │ │ └── android │ │ └── perftesting │ │ ├── MainActivity.java │ │ ├── MainActivityFragment.java │ │ ├── RecyclerViewActivity.java │ │ ├── SimpleListActivity.java │ │ └── contacts │ │ ├── Contact.java │ │ ├── ContactsArrayAdapter.java │ │ ├── ContactsArrayAdapterFixed.java │ │ └── ContactsRecyclerAdapter.java │ └── res │ ├── drawable-hdpi │ └── bbq.png │ ├── drawable-ldpi │ └── bbq.png │ ├── drawable-mdpi │ └── bbq.png │ ├── drawable-xhdpi │ └── bbq.png │ ├── drawable-xxhdpi │ └── bbq.png │ ├── drawable-xxxhdpi │ └── bbq.png │ ├── layout │ ├── activity_main.xml │ ├── activity_recyclerview.xml │ ├── activity_simple_list.xml │ ├── fragment_main.xml │ └── item_contact.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── buildSrc └── src │ └── main │ └── groovy │ └── com │ └── google │ └── android │ └── perftesting │ ├── PerfTestTaskGeneratorPlugin.groovy │ ├── RunCloudRoboTestsTask.groovy │ └── RunLocalPerfTestsTask.groovy ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── run_perf_tests.py └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Gradle cache. 2 | .gradle 3 | 4 | # File system files. 5 | .DS_Store 6 | 7 | # AndroidStudio workspace state. 8 | *.iml 9 | /.idea 10 | local.properties 11 | 12 | # Build dirs. 13 | /app/build 14 | /build 15 | /buildSrc/build 16 | 17 | # Test run data. 18 | /captures 19 | /testdata 20 | /logs 21 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We really love contributions! Simply follow these three steps to contribute: 4 | 5 | 1. Sign one of the Contributor License Agreements below. 6 | 2. Fork the repo, develop, and test your changes. 7 | 3. Submit a pull request to the repo with your changes. 8 | 9 | We will review your change and if it all looks good, your change will be merged. 10 | If it appears your change needs additional work, we will let you know with 11 | useful comments so you can adjust your pull request accordingly. 12 | 13 | ## Contributor License Agreements 14 | 15 | Before creating a pull request, please fill out either the individual or 16 | corporate Contributor License Agreement. 17 | 18 | - **Individual:** If you are an individual writing original source code and 19 | you're sure you own the intellectual property, then you'll need to sign the 20 | [individual CLA][indv-cla]. 21 | - **Corporate:** If you work for a company that wants to allow you to contribute 22 | your work to this client library, then you'll need to sign the 23 | [corporate CLA][corp-cla]. 24 | 25 | Follow either of the two links above to access the appropriate CLA and 26 | instructions for how to sign and return it. Once we receive it, we'll add you 27 | to the official list of contributors and be able to accept your changes. 28 | 29 | [indv-cla]: https://cla.developers.google.com/about/google-individual 30 | [corp-cla]: https://cla.developers.google.com/about/google-corporate 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This code is DEPRECATED 2 | 3 | The associated codelab and this code have been deprecated. Please use the following resources instead: 4 | * [Firebase Performance Monitoring documentation](https://firebase.google.com/docs/perf-mon) 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | buildToolsVersion '26.0.3' 6 | 7 | defaultConfig { 8 | applicationId "com.google.android.perftesting" 9 | minSdkVersion 14 10 | targetSdkVersion 27 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | testHandleProfiling true 16 | testFunctionalTest true 17 | } 18 | buildTypes { 19 | debug { 20 | debuggable true 21 | } 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | 28 | lintOptions { 29 | abortOnError false 30 | } 31 | 32 | } 33 | 34 | dependencies { 35 | String supportLibVersion = "27.0.2" 36 | compile fileTree(include: ['*.jar'], dir: 'libs') 37 | compile "com.android.support:appcompat-v7:${supportLibVersion}" 38 | compile "com.android.support:recyclerview-v7:${supportLibVersion}" 39 | compile "com.android.support:support-v4:${supportLibVersion}" 40 | compile 'com.github.bumptech.glide:glide:3.8.0' 41 | // TODO(developer): Comment in the following dependencies to enable Espresso and related libraries in this project. 42 | // Force the use of a specific version of the annotations library because the app compile 43 | // dependencies require this version transitively, but an androidTestCompile dependency 44 | // requires a different version. See http://goo.gl/iH4WDq for more info. 45 | // androidTestCompile "com.android.support:support-annotations:${supportLibVersion}" 46 | // androidTestCompile 'com.android.support.test:runner:0.5' 47 | // androidTestCompile 'com.android.support.test:rules:0.5' 48 | // androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' 49 | } 50 | 51 | // Create performance testing tasks for all connected Android devices using a Gradle plugin defined 52 | // in the buildSrc directory. 53 | // apply plugin: PerfTestTaskGeneratorPlugin 54 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /ssd2/apps/public/stable/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.app.Application; 20 | import android.test.ApplicationTestCase; 21 | 22 | /** 23 | * Testing Fundamentals 24 | */ 25 | public class ApplicationTest extends ApplicationTestCase { 26 | 27 | public ApplicationTest() { 28 | super(Application.class); 29 | } 30 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/MainActivityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | 20 | import android.support.test.rule.ActivityTestRule; 21 | import android.test.ActivityInstrumentationTestCase2; 22 | 23 | import org.junit.Before; 24 | import org.junit.Rule; 25 | 26 | /** 27 | * For a small sample on just the Espresso framework see https://goo.gl/GOUP47 28 | */ 29 | // @RunWith(AndroidJUnit4.class) 30 | // @LargeTest 31 | // TODO(developer): Uncomment the below annotation to have this test added to the set of perf tests. 32 | // @PerfTest 33 | public class MainActivityTest { 34 | 35 | public static final String STRING_TO_BE_TYPED = "Espresso"; 36 | 37 | /** 38 | * A JUnit {@link Rule @Rule} to launch your activity under test. This is a replacement 39 | * for {@link ActivityInstrumentationTestCase2}. 40 | *

41 | * Rules are interceptors which are executed for each test method and will run before 42 | * any of your setup code in the {@link Before @Before} method. 43 | *

44 | * {@link ActivityTestRule} will create and launch of the activity for you and also expose 45 | * the activity under test. To get a reference to the activity you can use 46 | * the {@link ActivityTestRule#getActivity()} method. 47 | */ 48 | // TODO(developer): Uncomment below member variable to add a test activity to this test class. 49 | // @Rule 50 | // public ActivityTestRule mActivityRule = new ActivityTestRule<>( 51 | // MainActivity.class); 52 | // 53 | // @Rule 54 | // public Timeout globalTimeout= new Timeout(20, TimeUnit.SECONDS); 55 | // 56 | // @Rule 57 | // public EnableTestTracing mEnableTestTracing = new EnableTestTracing(); 58 | // 59 | // @Rule 60 | // public EnablePostTestDumpsys mEnablePostTestDumpsys = new EnablePostTestDumpsys(); 61 | // 62 | // @Rule 63 | // public EnableLogcatDump mEnableLogcatDump = new EnableLogcatDump(); 64 | // 65 | // @Rule 66 | // public EnableNetStatsDump mEnableNetStatsDump = new EnableNetStatsDump(); 67 | // 68 | // 69 | // // TODO(developer): Uncomment below test method to add a simple test to the project. 70 | // @Test 71 | // @PerfTest 72 | // public void changeTextSameActivityTest() throws InterruptedException { 73 | // // Type text and then press the button. 74 | // onView(withId(R.id.edit_text_view)) 75 | // .perform(clearText(), typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); 76 | // onView(withId(R.id.edit_text_button)).perform(click()); 77 | // 78 | // // Check that the text was changed. 79 | // // Note that this test may fail if test device or emulator uses 3rd party keyboard app. 80 | // // If that happens, retry this test after switching keyboard setting to default keyboard. 81 | // onView(withId(R.id.display_text_view)).check(matches(withText(STRING_TO_BE_TYPED))); 82 | // } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/SimpleListActivityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.support.test.rule.ActivityTestRule; 20 | import android.support.test.runner.AndroidJUnit4; 21 | import android.test.ActivityInstrumentationTestCase2; 22 | import android.test.suitebuilder.annotation.LargeTest; 23 | 24 | import org.junit.Before; 25 | import org.junit.Rule; 26 | import org.junit.runner.RunWith; 27 | 28 | /** 29 | * For a small sample on just the Espresso framework see https://goo.gl/GOUP47 30 | */ 31 | @RunWith(AndroidJUnit4.class) 32 | @LargeTest 33 | // TODO(developer): Uncomment the below annotation to have this test added to the set of perf tests. 34 | // @PerfTest 35 | public class SimpleListActivityTest { 36 | public static final int SCROLL_TIME_IN_MILLIS = 4000; 37 | public static final long MAX_ADAPTER_VIEW_PROCESSING_TIME_IN_MILLIS = 500; 38 | 39 | /** 40 | * A JUnit {@link Rule @Rule} to launch your activity under test. This is a replacement 41 | * for {@link ActivityInstrumentationTestCase2}. 42 | *

43 | * Rules are interceptors which are executed for each test method and will run before 44 | * any of your setup code in the {@link Before @Before} method. 45 | *

46 | * {@link ActivityTestRule} will create and launch of the activity for you and also expose 47 | * the activity under test. To get a reference to the activity you can use 48 | * the {@link ActivityTestRule#getActivity()} method. 49 | */ 50 | // TODO(developer): Uncomment below member variable to add a test activity to this test class. 51 | // @Rule 52 | // public ActivityTestRule mActivityRule = new ActivityTestRule<>( 53 | // SimpleListActivity.class); 54 | // 55 | // @Rule 56 | // public Timeout globalTimeout= new Timeout( 57 | // SCROLL_TIME_IN_MILLIS + MAX_ADAPTER_VIEW_PROCESSING_TIME_IN_MILLIS, TimeUnit.MILLISECONDS); 58 | // 59 | // @Rule 60 | // public EnableTestTracing mEnableTestTracing = new EnableTestTracing(); 61 | // 62 | // @Rule 63 | // public EnablePostTestDumpsys mEnablePostTestDumpsys = new EnablePostTestDumpsys(); 64 | // 65 | // @Rule 66 | // public EnableLogcatDump mEnableLogcatDump = new EnableLogcatDump(); 67 | // 68 | // @Rule 69 | // public EnableNetStatsDump mEnableNetStatsDump = new EnableNetStatsDump(); 70 | 71 | 72 | // TODO(developer): Uncomment below test method to add a list scrolling test to the project. 73 | // @Test 74 | // @PerfTest 75 | // public void scrollFullList() throws InterruptedException { 76 | // ListView listView = (ListView) mActivityRule.getActivity().findViewById(android.R.id.list); 77 | // 78 | // // Get last position and offset for zero-indexed position tracking. 79 | // int lastPosition = listView.getAdapter().getCount() - 1; 80 | // 81 | // // Espresso method of scrolling to the last item. 82 | // // onData(anything()).atPosition(lastPosition); 83 | // 84 | // // Standard Android method of scrolling to the last position. 85 | // listView.smoothScrollToPositionFromTop(lastPosition, 0, SCROLL_TIME_IN_MILLIS); 86 | // 87 | // // Scrolling is performed asynchronously so we need to periodically loop and detect if 88 | // // we're finished scrolling yet. This can be delayed by any work being done to display 89 | // // data items in the ListView. 90 | // while (listView.getLastVisiblePosition() != lastPosition) { 91 | // Thread.sleep(300); 92 | // } 93 | // } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/TestListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.content.Context; 20 | import android.support.annotation.NonNull; 21 | import android.util.Log; 22 | 23 | import com.google.android.perftesting.common.PerfTestingUtils; 24 | import com.google.android.perftesting.testrules.EnableBatteryStatsDump; 25 | import com.google.android.perftesting.testrules.EnableDeviceGetPropsInfo; 26 | 27 | import org.junit.runner.notification.Failure; 28 | import org.junit.runner.notification.RunListener; 29 | 30 | import java.io.File; 31 | import java.io.FileWriter; 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.io.InputStreamReader; 35 | import java.io.PrintWriter; 36 | import java.io.Reader; 37 | 38 | /** 39 | * Initiate pre-test and post-test procedures; including cleaning and moving files from the 40 | * internal app data directory to the external one. Also perform writing to test-start and test-end 41 | * files which are used to indicate whether there was a fatal test exception 42 | * (e.g. OutOfMemoryException). 43 | */ 44 | public class TestListener extends RunListener { 45 | private static final String LOG_TAG = "TestListener"; 46 | public static final String TEST_DATA_SUBDIR_NAME = "testdata"; 47 | public EnableBatteryStatsDump mEnableBatteryStatsDump; 48 | public EnableDeviceGetPropsInfo mEnableDeviceGetPropsInfo; 49 | 50 | // TODO(developer): Uncomment the following two methods to enable log files to be pulled as well as battery and location request information to be requested. 51 | // @Override 52 | // public void testRunStarted(Description description) throws Exception { 53 | // Log.w(LOG_TAG, "Test run started."); 54 | // // Cleanup data from past test runs. 55 | // deleteExistingTestFilesInAppData(); 56 | // deleteExistingTestFilesInExternalData(); 57 | // 58 | // mEnableBatteryStatsDump = new EnableBatteryStatsDump( 59 | // PerfTestingUtils.getTestRunFile("batterystats.dumpsys.log")); 60 | // mEnableBatteryStatsDump.before(); 61 | // 62 | // mEnableDeviceGetPropsInfo = new EnableDeviceGetPropsInfo( 63 | // PerfTestingUtils.getTestRunFile("getprops.log")); 64 | // mEnableDeviceGetPropsInfo.before(); 65 | // 66 | // // This isn't available until the next version of Google Play services. 67 | // // resetLocationRequestTracking(); 68 | // super.testRunStarted(description); 69 | // } 70 | // 71 | // @Override 72 | // public void testRunFinished(Result result) throws Exception { 73 | // Log.w(LOG_TAG, "Test run finished."); 74 | // 75 | // super.testRunFinished(result); 76 | // 77 | // try { 78 | // Log.w(LOG_TAG, "Test run finished"); 79 | // 80 | // if (mEnableBatteryStatsDump != null) { 81 | // mEnableBatteryStatsDump.after(); 82 | // } 83 | // 84 | // Log.w(LOG_TAG, "Battery stats collected."); 85 | // 86 | // if (mEnableDeviceGetPropsInfo != null) { 87 | // mEnableDeviceGetPropsInfo.after(); 88 | // } 89 | // Log.w(LOG_TAG, "getprops collected."); 90 | // 91 | // dumpLocationRequestInformation(); 92 | // Log.w(LOG_TAG, "Location request information collected."); 93 | // 94 | // // Create a file indicating the test run is complete. This can be checked to ensure 95 | // // the extraction of files for the test run was successful. 96 | // File testRunFinishedFile = PerfTestingUtils.getTestRunFile("testRunComplete.log"); 97 | // testRunFinishedFile.createNewFile(); 98 | // Log.w(LOG_TAG, "testRunComplete file written."); 99 | // 100 | // copyTestFilesToExternalData(); 101 | // 102 | // Log.w(LOG_TAG, "Done copying files to external data directory"); 103 | // } catch (Exception e) { 104 | // Log.e(LOG_TAG, "Issue taking all log files after test run", e); 105 | // } 106 | // } 107 | 108 | @Override 109 | public void testFailure(Failure failure) throws Exception { 110 | super.testFailure(failure); 111 | logTestFailure(failure); 112 | } 113 | 114 | /** 115 | * Move files from the app's internal file location to a location that can be read on retail 116 | * devices with simple ADB pull commands. 117 | */ 118 | private void copyTestFilesToExternalData() throws IOException, InterruptedException { 119 | ProcessBuilder processBuilder = new ProcessBuilder(); 120 | 121 | Context appUnderTestContext = PerfTestingUtils.getAppContext(); 122 | File externalAppStorageDir = appUnderTestContext.getExternalFilesDir(null); 123 | File externalTestFilesStorageDir = new File(externalAppStorageDir, TEST_DATA_SUBDIR_NAME); 124 | if (!externalTestFilesStorageDir.exists()) { 125 | if (!externalTestFilesStorageDir.mkdirs()) { 126 | throw new RuntimeException("Not able to create exportable directory for test data"); 127 | } 128 | } 129 | 130 | String srcAbsolutePath = PerfTestingUtils.getTestRunDir().getAbsolutePath(); 131 | String destAbsolutePath = externalTestFilesStorageDir.getAbsolutePath(); 132 | 133 | Log.w(LOG_TAG, "Moving test data from " + srcAbsolutePath + " to " + destAbsolutePath); 134 | 135 | processBuilder.command("cp", "-r", srcAbsolutePath, destAbsolutePath); 136 | processBuilder.redirectErrorStream(); 137 | Process process = processBuilder.start(); 138 | process.waitFor(); 139 | if (process.exitValue() != 0) { 140 | StringBuilder errOutput = new StringBuilder(); 141 | char[] charBuffer = new char[1024]; 142 | int readSize; 143 | InputStream errorStream = null; 144 | Reader reader = null; 145 | try { 146 | errorStream = process.getInputStream(); 147 | reader = new InputStreamReader(errorStream); 148 | while ((readSize = reader.read()) > 0) { 149 | errOutput.append(charBuffer, 0, readSize); 150 | } 151 | } finally { 152 | if (errorStream != null) try { errorStream.close(); } catch (Exception ignored) {} 153 | if (reader != null) try { reader.close(); } catch (Exception ignored) {} 154 | } 155 | String errorString = errOutput.toString(); 156 | Log.e(LOG_TAG, errorString); 157 | throw new IOException("Not able to move test data to external storage directory:" 158 | + " src=" + srcAbsolutePath + ", dest=" + destAbsolutePath + ", out=" + 159 | errorString); 160 | } 161 | } 162 | 163 | private void deleteExistingTestFilesInExternalData() throws IOException, InterruptedException { 164 | ProcessBuilder processBuilder = new ProcessBuilder(); 165 | 166 | Context appUnderTestContext = PerfTestingUtils.getAppContext(); 167 | File externalAppStorageDir = appUnderTestContext.getExternalFilesDir(null); 168 | File externalTestFilesStorageDir = new File(externalAppStorageDir, TEST_DATA_SUBDIR_NAME); 169 | String destAbsolutePath = externalTestFilesStorageDir.getAbsolutePath(); 170 | 171 | processBuilder.command("rm", "-r", destAbsolutePath); 172 | processBuilder.redirectErrorStream(); 173 | Process process = processBuilder.start(); 174 | process.waitFor(); 175 | } 176 | 177 | private void deleteExistingTestFilesInAppData() throws IOException, InterruptedException { 178 | ProcessBuilder processBuilder = new ProcessBuilder(); 179 | 180 | File externalTestFilesStorageDir = getAppDataLogDir(); 181 | String destAbsolutePath = externalTestFilesStorageDir.getAbsolutePath(); 182 | 183 | processBuilder.command("rm", "-r", destAbsolutePath); 184 | processBuilder.redirectErrorStream(); 185 | Process process = processBuilder.start(); 186 | process.waitFor(); 187 | } 188 | 189 | private void resetLocationRequestTracking() throws IOException, InterruptedException { 190 | ProcessBuilder processBuilder = new ProcessBuilder(); 191 | 192 | processBuilder.command("dumpsys", "activity", "service", 193 | "com.google.android.location.internal.GoogleLocationManagerService", "--reset"); 194 | processBuilder.redirectErrorStream(); 195 | Process process = processBuilder.start(); 196 | process.waitFor(); 197 | } 198 | 199 | private void dumpLocationRequestInformation() { 200 | try { 201 | ProcessBuilder processBuilder = new ProcessBuilder(); 202 | 203 | File externalTestFilesStorageDir = getAppDataLogDir(); 204 | File locationRequestLogFile = new File(externalTestFilesStorageDir, "locationRequests" 205 | + ".dumpsys.log"); 206 | String destAbsolutePath = locationRequestLogFile.getAbsolutePath(); 207 | 208 | processBuilder.command("dumpsys", "activity", "service", 209 | "com.google.android.location.internal.GoogleLocationManagerService"); 210 | processBuilder.redirectErrorStream(); 211 | Process process = processBuilder.start(); 212 | process.waitFor(); 213 | 214 | FileWriter fileWriter = null; 215 | InputStreamReader inputStreamReader = null; 216 | try { 217 | fileWriter = new FileWriter(destAbsolutePath); 218 | inputStreamReader = new InputStreamReader(process.getInputStream()); 219 | char[] charBuffer = new char[1024]; 220 | int readSize; 221 | while ((readSize = inputStreamReader.read(charBuffer, 0, charBuffer.length)) > -1) { 222 | fileWriter.write(charBuffer, 0, readSize); 223 | } 224 | 225 | } finally { 226 | try { inputStreamReader.close(); } catch (Exception ignored) {} 227 | try { fileWriter.close(); } catch (Exception ignored) {} 228 | } 229 | 230 | if (process.exitValue()!= 0) { 231 | throw new IOException("Exit value non-zero."); 232 | } 233 | } catch (Exception e) { 234 | Log.e(LOG_TAG, "Unable to retrieve location provide logging information", e); 235 | } 236 | } 237 | 238 | private void logTestFailure(Failure failure) throws IOException, InterruptedException { 239 | File failureLogFile = PerfTestingUtils.getTestFile( 240 | failure.getDescription().getClassName(), 241 | failure.getDescription().getMethodName(), "test.failure.log"); 242 | 243 | FileWriter fileWriter = null; 244 | PrintWriter printWriter = null; 245 | String eol = System.getProperty("line.separator"); 246 | try { 247 | fileWriter = new FileWriter(failureLogFile); 248 | printWriter = new PrintWriter(fileWriter); 249 | printWriter.append(failure.getTestHeader()); 250 | printWriter.append(eol); 251 | failure.getException().printStackTrace(printWriter); 252 | printWriter.append(eol); 253 | } finally { 254 | if (printWriter != null) { try { printWriter.close(); } catch (Exception ignored) { } } 255 | if (fileWriter != null) { try { fileWriter.close(); } catch (Exception ignored) { } } 256 | } 257 | } 258 | 259 | @NonNull 260 | private File getAppDataLogDir() { 261 | Context appUnderTestContext = PerfTestingUtils.getAppContext(); 262 | File externalAppStorageDir = appUnderTestContext.getFilesDir(); 263 | File externalTestFilesStorageDir = new File(externalAppStorageDir, TEST_DATA_SUBDIR_NAME); 264 | if (!externalTestFilesStorageDir.exists()) { 265 | externalTestFilesStorageDir.mkdirs(); 266 | } 267 | return externalTestFilesStorageDir; 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/common/PerfTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.common; 18 | 19 | /** 20 | * Used to annotate a test as having a performance component to it. 21 | * 22 | * TODO: Perhaps add a timeout parameter. 23 | */ 24 | public @interface PerfTest { 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/common/PerfTestingUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.common; 18 | 19 | import android.content.Context; 20 | import android.support.test.InstrumentationRegistry; 21 | 22 | import java.io.File; 23 | 24 | /** 25 | * Centralized methods to ensure tests perform certain actions in the same manner. 26 | */ 27 | public class PerfTestingUtils { 28 | /** 29 | * Retrieve the directory where test files should be written to. 30 | * This directory is local to the application and removed when the app is uninstalled. 31 | */ 32 | public static File getTestDir(String className, String testName) { 33 | 34 | File rootDir = getAppContext().getFilesDir(); 35 | 36 | // Create a test data subdirectory. 37 | File testFileDir = new File(rootDir, "testdata"); 38 | if (getTranslatedTestName(className, testName) != null) { 39 | testFileDir = new File(testFileDir, getTranslatedTestName(className, testName)); 40 | } 41 | 42 | if (!testFileDir.exists()) { 43 | if (!testFileDir.mkdirs()) { 44 | throw new RuntimeException("Unable to create test file directory."); 45 | } 46 | } 47 | return testFileDir; 48 | 49 | } 50 | 51 | /** 52 | * Retrieve the app-under-test's {@code Context}. 53 | */ 54 | public static Context getAppContext() { 55 | return InstrumentationRegistry.getInstrumentation().getTargetContext(); 56 | } 57 | 58 | /** 59 | * Retrieve a file handle that is within the testing directory where tests should be written to. 60 | */ 61 | public static File getTestRunFile(String filename) { 62 | return new File(getTestDir(null, null), filename); 63 | } 64 | 65 | /** 66 | * Retrieve the test run directory where tests should be written to. 67 | */ 68 | public static File getTestRunDir() { 69 | return getTestDir(null, null); 70 | } 71 | 72 | private static String getTranslatedTestName(String className, String testName) { 73 | if (className == null || testName == null) { 74 | return null; 75 | } 76 | String base = className + "_" + testName; 77 | 78 | // Shorten the common strings so "com.google.android" comes out as "c.g.a" for brevity. 79 | base = base.replace("com", "c") 80 | .replace("google", "g") 81 | .replace("android", "a") 82 | .replace("perfmatters", "pm") 83 | .replace("automatingperformancetesting", "apt"); 84 | return base; 85 | 86 | } 87 | 88 | /** 89 | * Retrieve a file handle that is within the testing directory where tests should be written to. 90 | */ 91 | public static File getTestFile(String className, String testName, String filename) { 92 | return new File(getTestDir(className, testName), filename); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnableBatteryStatsDump.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import android.os.Trace; 20 | 21 | import org.junit.rules.ExternalResource; 22 | import org.junit.runner.Description; 23 | import org.junit.runners.model.Statement; 24 | 25 | import java.io.BufferedReader; 26 | import java.io.File; 27 | import java.io.FileWriter; 28 | import java.io.InputStreamReader; 29 | import java.util.logging.Level; 30 | import java.util.logging.Logger; 31 | 32 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile; 33 | 34 | /** 35 | * This rule resets battery stats before a test and executes a dumpsys for batterystats after 36 | * performing the test. If the API level is less than 21 then this rule will do nothing since 37 | * this dumpsys command isn't supported. It has limited use for short tests and is meant for tests 38 | * you would typically mark as Large. For short tests you can manually use this in a 39 | * {@code org.junit.runner.notification.RunListener}. 40 | * 41 | *

 42 |  * @Rule
 43 |  * public EnableBatteryStatsDump mEnableBatteryStatsDump = new EnableBatteryStatsDump();
 44 |  * 
45 | */ 46 | public class EnableBatteryStatsDump extends ExternalResource { 47 | 48 | private Logger logger = Logger.getLogger(EnableBatteryStatsDump.class.getName()); 49 | 50 | private String mTestName; 51 | 52 | private String mTestClass; 53 | 54 | private File mLogFileAbsoluteLocation = null; 55 | 56 | public EnableBatteryStatsDump() { } 57 | 58 | /** 59 | * Allow the the log to be written to a specific location. 60 | */ 61 | public EnableBatteryStatsDump(File logFileAbsoluteLocation) { 62 | mLogFileAbsoluteLocation = logFileAbsoluteLocation; 63 | } 64 | 65 | @Override 66 | public Statement apply(Statement base, Description description) { 67 | mTestName = description.getMethodName(); 68 | mTestClass = description.getClassName(); 69 | return super.apply(base, description); 70 | } 71 | 72 | @Override 73 | public void before() { 74 | if (android.os.Build.VERSION.SDK_INT >= 21) { 75 | try { 76 | ProcessBuilder builder = new ProcessBuilder(); 77 | builder.command("dumpsys", "batterystats", "--reset"); 78 | Process process = builder.start(); 79 | process.waitFor(); 80 | } catch (Exception exception) { 81 | logger.log(Level.SEVERE, "Unable to reset dumpsys", exception); 82 | } 83 | } 84 | } 85 | 86 | public void after() { 87 | if (android.os.Build.VERSION.SDK_INT >= 21) { 88 | FileWriter fileWriter = null; 89 | BufferedReader bufferedReader = null; 90 | try { 91 | Trace.beginSection("Taking battery dumpsys"); 92 | ProcessBuilder processBuilder = new ProcessBuilder(); 93 | 94 | processBuilder.command("dumpsys", "batterystats"); 95 | processBuilder.redirectErrorStream(); 96 | Process process = processBuilder.start(); 97 | if (mLogFileAbsoluteLocation == null) { 98 | mLogFileAbsoluteLocation = getTestFile(mTestClass, mTestName, 99 | "battery.dumpsys.log"); 100 | } 101 | fileWriter = new FileWriter(mLogFileAbsoluteLocation); 102 | bufferedReader = new BufferedReader( 103 | new InputStreamReader(process.getInputStream())); 104 | String line; 105 | while ((line = bufferedReader.readLine()) != null) { 106 | fileWriter.append(line); 107 | fileWriter.append(System.lineSeparator()); 108 | } 109 | process.waitFor(); 110 | if (process.exitValue() != 0) { 111 | throw new Exception("Error while taking dumpsys, exitCode=" + 112 | process.exitValue()); 113 | } 114 | } catch (Exception exception) { 115 | logger.log(Level.SEVERE, "Unable to take a dumpsys", exception); 116 | } finally { 117 | if (fileWriter != null) { 118 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } 119 | } 120 | if (bufferedReader != null) { 121 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } 122 | } 123 | } 124 | } 125 | } 126 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnableDeviceGetPropsInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import org.junit.rules.ExternalResource; 20 | import org.junit.runner.Description; 21 | import org.junit.runners.model.Statement; 22 | 23 | import java.io.BufferedReader; 24 | import java.io.File; 25 | import java.io.FileWriter; 26 | import java.io.InputStreamReader; 27 | import java.util.logging.Level; 28 | import java.util.logging.Logger; 29 | 30 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile; 31 | 32 | /** 33 | * This rule records the result of calling the getprops command on a device. It has limited use for 34 | * short tests except when these properties are changing between tests. Alternatively, it can be 35 | * used manually in a {@code org.junit.runner.notification.RunListener}. 36 | * 37 | *
 38 |  * @Rule
 39 |  * public EnableDeviceGetPropsInfo mEnableDeviceGetPropsInfo = new EnableDeviceGetPropsInfo();
 40 |  * 
41 | */ 42 | public class EnableDeviceGetPropsInfo extends ExternalResource { 43 | 44 | private Logger logger = Logger.getLogger(EnableDeviceGetPropsInfo.class.getName()); 45 | 46 | private String mTestName; 47 | 48 | private String mTestClass; 49 | 50 | private File mLogFileAbsoluteLocation = null; 51 | 52 | public EnableDeviceGetPropsInfo() { } 53 | 54 | /** 55 | * Allow the the log to be written to a specific location. 56 | */ 57 | public EnableDeviceGetPropsInfo(File logFileAbsoluteLocation) { 58 | mLogFileAbsoluteLocation = logFileAbsoluteLocation; 59 | } 60 | 61 | @Override 62 | public Statement apply(Statement base, Description description) { 63 | mTestName = description.getMethodName(); 64 | mTestClass = description.getClassName(); 65 | return super.apply(base, description); 66 | } 67 | 68 | @Override 69 | public void before() { 70 | // Do nothing. 71 | } 72 | 73 | public void after() { 74 | FileWriter fileWriter = null; 75 | BufferedReader bufferedReader = null; 76 | try { 77 | ProcessBuilder processBuilder = new ProcessBuilder(); 78 | 79 | processBuilder.command("getprop"); 80 | processBuilder.redirectErrorStream(); 81 | Process process = processBuilder.start(); 82 | if (mLogFileAbsoluteLocation == null) { 83 | mLogFileAbsoluteLocation = getTestFile(mTestClass, mTestName, 84 | "getprops.log"); 85 | } 86 | fileWriter = new FileWriter(mLogFileAbsoluteLocation); 87 | bufferedReader = new BufferedReader( 88 | new InputStreamReader(process.getInputStream())); 89 | String line; 90 | while ((line = bufferedReader.readLine()) != null) { 91 | fileWriter.append(line); 92 | fileWriter.append("\n"); 93 | } 94 | process.waitFor(); 95 | if (process.exitValue() != 0) { 96 | throw new Exception("Error while taking getprops, exitCode=" + 97 | process.exitValue()); 98 | } 99 | } catch (Exception exception) { 100 | logger.log(Level.SEVERE, "Unable to take a getprops", exception); 101 | } finally { 102 | if (fileWriter != null) { 103 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } 104 | } 105 | if (bufferedReader != null) { 106 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } 107 | } 108 | } 109 | } 110 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnableLogcatDump.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import android.os.Build; 20 | import android.os.Trace; 21 | import android.util.Log; 22 | 23 | import com.google.android.perftesting.common.PerfTestingUtils; 24 | 25 | import org.junit.rules.ExternalResource; 26 | import org.junit.runner.Description; 27 | import org.junit.runners.model.Statement; 28 | 29 | /** 30 | * This rule executes clears logcat prior to a test then records the output at the end of a test. 31 | * 32 | *
33 |  * @Rule
34 |  * public EnableLogcatDump mEnableLogcatDump = new EnableLogcatDump();
35 |  * 
36 | */ 37 | public class EnableLogcatDump extends ExternalResource { 38 | 39 | private static final String LOG_TAG = "EnableLogcatDump"; 40 | 41 | private String mTestName; 42 | 43 | private String mTestClass; 44 | 45 | @Override 46 | public Statement apply(Statement base, Description description) { 47 | mTestName = description.getMethodName(); 48 | mTestClass = description.getClassName(); 49 | return super.apply(base, description); 50 | } 51 | 52 | /** 53 | * Clear logcat buffer prior to test run. 54 | */ 55 | public void before() throws Throwable { 56 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 57 | ProcessBuilder processBuilder = new ProcessBuilder(); 58 | processBuilder.command("logcat", "-c"); 59 | processBuilder.redirectErrorStream(); 60 | Process process = processBuilder.start(); 61 | process.waitFor(); 62 | if (process.exitValue() != 0) { 63 | Log.e(LOG_TAG, "Error while clearing logcat, exitValue=" + process.exitValue()); 64 | } 65 | } 66 | } 67 | 68 | /** 69 | * Extract logcat buffer to a file ater test run. 70 | */ 71 | public void after() { 72 | try { 73 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 74 | Trace.beginSection("Taking logcat"); 75 | } 76 | ProcessBuilder processBuilder = new ProcessBuilder(); 77 | 78 | processBuilder.command("logcat", "-d", 79 | "-f", PerfTestingUtils.getTestFile(mTestClass, mTestName, "logcat.log") 80 | .getAbsolutePath()); 81 | processBuilder.redirectErrorStream(); 82 | Process process = processBuilder.start(); 83 | process.waitFor(); 84 | if (process.exitValue() != 0) { 85 | Log.e(LOG_TAG, "Error exit value while extracting logcat, exitValue=" + 86 | process.exitValue()); 87 | } 88 | } catch (Exception ignored) { 89 | Log.e(LOG_TAG, "Error while extracting logcat", ignored); 90 | } finally { 91 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 92 | Trace.endSection(); 93 | } 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnableNetStatsDump.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import android.os.Trace; 20 | 21 | import org.junit.rules.ExternalResource; 22 | import org.junit.runner.Description; 23 | import org.junit.runners.model.Statement; 24 | 25 | import java.io.BufferedReader; 26 | import java.io.File; 27 | import java.io.FileWriter; 28 | import java.io.InputStreamReader; 29 | import java.util.logging.Level; 30 | import java.util.logging.Logger; 31 | 32 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile; 33 | 34 | /** 35 | * This rule resets network stats before a test and executes a dumpsys for netstats after 36 | * performing the test. 37 | * 38 | *
 39 |  * @Rule
 40 |  * public EnableNetStatsDump mEnableNetStatsDump = new EnableNetStatsDump();
 41 |  * 
42 | */ 43 | public class EnableNetStatsDump extends ExternalResource { 44 | 45 | private Logger logger = Logger.getLogger(EnableNetStatsDump.class.getName()); 46 | 47 | private String mTestName; 48 | 49 | private String mTestClass; 50 | 51 | private File mLogFileAbsoluteLocation = null; 52 | 53 | public EnableNetStatsDump() { 54 | 55 | } 56 | 57 | /** 58 | * Allow the the log to be written to a specific location. 59 | */ 60 | public EnableNetStatsDump(File logFileAbsoluteLocation) { 61 | mLogFileAbsoluteLocation = logFileAbsoluteLocation; 62 | } 63 | 64 | @Override 65 | public Statement apply(Statement base, Description description) { 66 | mTestName = description.getMethodName(); 67 | mTestClass = description.getClassName(); 68 | return super.apply(base, description); 69 | } 70 | 71 | @Override 72 | public void before() { 73 | if (android.os.Build.VERSION.SDK_INT >= 21) { 74 | try { 75 | ProcessBuilder builder = new ProcessBuilder(); 76 | builder.command("dumpsys", "netstats", "--reset"); 77 | Process process = builder.start(); 78 | process.waitFor(); 79 | } catch (Exception exception) { 80 | logger.log(Level.SEVERE, "Unable to reset dumpsys", exception); 81 | } 82 | } 83 | } 84 | 85 | public void after() { 86 | if (android.os.Build.VERSION.SDK_INT >= 21) { 87 | FileWriter fileWriter = null; 88 | BufferedReader bufferedReader = null; 89 | try { 90 | Trace.beginSection("Taking netstats dumpsys"); 91 | ProcessBuilder processBuilder = new ProcessBuilder(); 92 | 93 | processBuilder.command("dumpsys", "netstats", "full", "detail"); 94 | processBuilder.redirectErrorStream(); 95 | Process process = processBuilder.start(); 96 | if (mLogFileAbsoluteLocation == null) { 97 | mLogFileAbsoluteLocation = getTestFile(mTestClass, mTestName, 98 | "netstats.dumpsys.log"); 99 | } 100 | fileWriter = new FileWriter(mLogFileAbsoluteLocation); 101 | bufferedReader = new BufferedReader( 102 | new InputStreamReader(process.getInputStream())); 103 | String line; 104 | while ((line = bufferedReader.readLine()) != null) { 105 | fileWriter.append(line); 106 | fileWriter.append(System.lineSeparator()); 107 | } 108 | process.waitFor(); 109 | if (process.exitValue() != 0) { 110 | throw new Exception("Error while taking dumpsys, exitCode=" + 111 | process.exitValue()); 112 | } 113 | } catch (Exception exception) { 114 | logger.log(Level.SEVERE, "Unable to take a dumpsys", exception); 115 | } finally { 116 | if (fileWriter != null) { 117 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } 118 | } 119 | if (bufferedReader != null) { 120 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } 121 | } 122 | } 123 | } 124 | } 125 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnablePerTestTraceFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import org.junit.rules.ExternalResource; 20 | import org.junit.runner.Description; 21 | import org.junit.runners.model.Statement; 22 | 23 | /** 24 | * This rule enables pulling an atrace file that can be run through systrace.py 25 | * 26 | * This JUnit rule requires the test be running on a device that has been rooted. If you don't 27 | * have a device with root you should prefer the systrace be pulled at the system level. 28 | * 29 | * To enable, add this rule to your test class. 30 | * 31 | *
32 |  * @Rule
33 |  * public EnablePerTestTraceFile mEnablePerTestTraceFile = new EnablePerTestTraceFile();
34 |  * 
35 | */ 36 | public class EnablePerTestTraceFile extends ExternalResource { 37 | 38 | private String mTestName; 39 | private boolean aTraceInUse = false; 40 | 41 | @Override 42 | public Statement apply(Statement base, Description description) { 43 | mTestName = description.getMethodName(); 44 | return super.apply(base, description); 45 | } 46 | 47 | @Override 48 | public void before() { 49 | try { 50 | ProcessBuilder builder = new ProcessBuilder(); 51 | builder.command("atrace", "--async_start", "-a", 52 | // NOTE: Using the android app BuildConfig specifically. 53 | com.google.android.perftesting.BuildConfig.APPLICATION_ID); 54 | Process process = builder.start(); 55 | process.waitFor(); 56 | if (process.exitValue() == 0) { 57 | aTraceInUse = true; 58 | } 59 | } catch (Exception ignored) { 60 | // Not much we can do if atrace isn't enabled on the device. 61 | } 62 | } 63 | 64 | @Override 65 | public void after() { 66 | if (aTraceInUse) { 67 | try { 68 | ProcessBuilder builder = new ProcessBuilder(); 69 | builder.command("atrace", "--async_stop", "-a", 70 | // NOTE: Using the android app BuildConfig specifically. 71 | com.google.android.perftesting.BuildConfig.APPLICATION_ID); 72 | Process process = builder.start(); 73 | process.waitFor(); 74 | } catch (Exception ignored) { 75 | // Not much we can do if atrace isn't enabled on the device. 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnablePostTestDumpsys.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import android.os.Trace; 20 | 21 | import org.junit.rules.ExternalResource; 22 | import org.junit.runner.Description; 23 | import org.junit.runners.model.Statement; 24 | 25 | import java.io.BufferedReader; 26 | import java.io.FileWriter; 27 | import java.io.InputStreamReader; 28 | import java.util.logging.Level; 29 | import java.util.logging.Logger; 30 | 31 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile; 32 | 33 | /** 34 | * This rule executes a dumpsys graphics data dump after performing the test. If the API level is 35 | * less than 23 then this rule will do nothing since this dumpsys command isn't supported. 36 | * 37 | *
 38 |  * @Rule
 39 |  * public EnablePostTestDumpSys mEnablePostTestDumpSys = new EnablePostTestDumpSys();
 40 |  * 
41 | */ 42 | public class EnablePostTestDumpsys extends ExternalResource { 43 | 44 | private Logger logger = Logger.getLogger(EnablePostTestDumpsys.class.getName()); 45 | 46 | private String mTestName; 47 | private String mTestClass; 48 | 49 | @Override 50 | public Statement apply(Statement base, Description description) { 51 | mTestName = description.getMethodName(); 52 | mTestClass = description.getClassName(); 53 | return super.apply(base, description); 54 | } 55 | 56 | @Override 57 | public void before() { 58 | try { 59 | ProcessBuilder builder = new ProcessBuilder(); 60 | builder.command("dumpsys", "gfxinfo", "--reset", 61 | // NOTE: Using the android app BuildConfig specifically. 62 | com.google.android.perftesting.BuildConfig.APPLICATION_ID); 63 | Process process = builder.start(); 64 | process.waitFor(); 65 | } catch (Exception exception) { 66 | logger.log(Level.SEVERE, "Unable to reset dumpsys", exception); 67 | } 68 | } 69 | 70 | public void after() { 71 | if (android.os.Build.VERSION.SDK_INT >= 23) { 72 | FileWriter fileWriter = null; 73 | BufferedReader bufferedReader = null; 74 | try { 75 | Trace.beginSection("Taking Dumpsys"); 76 | ProcessBuilder processBuilder = new ProcessBuilder(); 77 | 78 | // TODO: If less than API level 23 we should remove framestats. 79 | processBuilder.command("dumpsys", "gfxinfo", 80 | // NOTE: Using the android app BuildConfig specifically. 81 | com.google.android.perftesting.BuildConfig.APPLICATION_ID, 82 | "framestats"); 83 | processBuilder.redirectErrorStream(); 84 | Process process = processBuilder.start(); 85 | fileWriter = new FileWriter(getTestFile(mTestClass, mTestName, "gfxinfo.dumpsys" 86 | + ".log")); 87 | bufferedReader = new BufferedReader( 88 | new InputStreamReader(process.getInputStream())); 89 | String line; 90 | while ((line = bufferedReader.readLine()) != null) { 91 | fileWriter.append(line); 92 | fileWriter.append(System.lineSeparator()); 93 | } 94 | process.waitFor(); 95 | if (process.exitValue() != 0) { 96 | throw new Exception("Error while taking dumpsys, exitCode=" + 97 | process.exitValue()); 98 | } 99 | } catch (Exception exception) { 100 | logger.log(Level.SEVERE, "Unable to take a dumpsys", exception); 101 | } finally { 102 | if (fileWriter != null) { 103 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } 104 | } 105 | if (bufferedReader != null) { 106 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } 107 | } 108 | Trace.endSection(); 109 | } 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/google/android/perftesting/testrules/EnableTestTracing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.testrules; 18 | 19 | import android.os.Trace; 20 | 21 | import org.junit.rules.ExternalResource; 22 | import org.junit.runner.Description; 23 | import org.junit.runners.model.Statement; 24 | 25 | /** 26 | * This rule enables {@link Trace Tracing} for each test. The section name 27 | * used for the Trace API is the name of the test being run. 28 | * 29 | * To enable AndroidTracing on a test simply add this rule like so and it will be enabled/disabled 30 | * when the platform support for Tracing exists (API Level 18 or higher). 31 | * 32 | *
33 |  * @Rule
34 |  * public EnableTestTracing mEnableTestTracing = new EnableTestTracing();
35 |  * 
36 | */ 37 | public class EnableTestTracing extends ExternalResource { 38 | 39 | private String mTestName; 40 | 41 | @Override 42 | public Statement apply(Statement base, Description description) { 43 | mTestName = description.getMethodName(); 44 | return super.apply(base, description); 45 | } 46 | 47 | @Override 48 | public void before() { 49 | if (android.os.Build.VERSION.SDK_INT >= 18) { 50 | Trace.beginSection(mTestName); 51 | } 52 | } 53 | 54 | @Override 55 | public void after() { 56 | if (android.os.Build.VERSION.SDK_INT >= 18) { 57 | Trace.endSection(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.content.Intent; 20 | import android.os.Bundle; 21 | import android.support.v7.app.AppCompatActivity; 22 | import android.view.View; 23 | 24 | public class MainActivity extends AppCompatActivity { 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | } 31 | 32 | public void openRecyclerView(View view) { 33 | startActivity(new Intent(this, RecyclerViewActivity.class)); 34 | } 35 | 36 | public void openSimpleListView(View view) { 37 | startActivity(new Intent(this, SimpleListActivity.class)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/MainActivityFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.os.Bundle; 20 | import android.support.v4.app.Fragment; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.EditText; 25 | import android.widget.TextView; 26 | 27 | /** 28 | * A placeholder fragment containing a simple view. 29 | */ 30 | public class MainActivityFragment extends Fragment { 31 | 32 | EditText mEditText; 33 | TextView mTextView; 34 | 35 | public MainActivityFragment() { 36 | } 37 | 38 | @Override 39 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 40 | Bundle savedInstanceState) { 41 | View rootView = inflater.inflate(R.layout.fragment_main, container, false); 42 | rootView.findViewById(R.id.edit_text_button).setOnClickListener(new View.OnClickListener() { 43 | @Override 44 | public void onClick(View v) { 45 | performAction(); 46 | } 47 | }); 48 | mEditText = (EditText)rootView.findViewById(R.id.edit_text_view); 49 | mTextView = (TextView)rootView.findViewById(R.id.display_text_view); 50 | return rootView; 51 | } 52 | 53 | public void performAction() { 54 | mTextView.setText(mEditText.getText()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/RecyclerViewActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.os.Bundle; 20 | import android.support.v7.app.AppCompatActivity; 21 | import android.support.v7.widget.LinearLayoutManager; 22 | import android.support.v7.widget.RecyclerView; 23 | 24 | import com.google.android.perftesting.contacts.Contact; 25 | import com.google.android.perftesting.contacts.ContactsRecyclerAdapter; 26 | 27 | import java.util.List; 28 | 29 | /** 30 | * RecyclerViewActivity creates a RecyclerView and fills it with Contacts. 31 | */ 32 | public class RecyclerViewActivity extends AppCompatActivity { 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_recyclerview); 38 | 39 | // Create a new list of 1000 contacts. 40 | List contacts = Contact.createContactsList(1000); 41 | 42 | RecyclerView contactsView = (RecyclerView) findViewById(R.id.contactList); 43 | ContactsRecyclerAdapter adapter = new ContactsRecyclerAdapter(contacts); 44 | contactsView.setAdapter(adapter); 45 | contactsView.setLayoutManager(new LinearLayoutManager(this)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/SimpleListActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting; 18 | 19 | import android.os.Bundle; 20 | import android.support.v7.app.AppCompatActivity; 21 | import android.widget.ListView; 22 | 23 | import com.google.android.perftesting.contacts.Contact; 24 | import com.google.android.perftesting.contacts.ContactsArrayAdapter; 25 | 26 | import java.util.List; 27 | 28 | /** 29 | * RecyclerViewActivity creates a ListView and fills it with Contacts. 30 | */ 31 | public class SimpleListActivity extends AppCompatActivity { 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_simple_list); 37 | 38 | // Create a new list of 1000 contacts. 39 | List contacts = Contact.createContactsList(1000); 40 | 41 | ListView listView = (ListView) findViewById(android.R.id.list); 42 | // TODO(developer): Use the ContactsArrayAdapterFixed for a quick performance improvement. 43 | // ContactsArrayAdapterFixed adapter = new ContactsArrayAdapterFixed(this, contacts); 44 | ContactsArrayAdapter adapter = new ContactsArrayAdapter(this, contacts); 45 | listView.setAdapter(adapter); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/contacts/Contact.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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 | package com.google.android.perftesting.contacts; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * Contact represents a listing of information for 23 | * a particular person to be stored in your phone. 24 | */ 25 | public class Contact { 26 | private String mName; 27 | 28 | public Contact(String name) { 29 | mName = name; 30 | } 31 | 32 | public String getName() { 33 | return mName; 34 | } 35 | 36 | public static List createContactsList(int numContacts) { 37 | List contacts = new ArrayList<>(); 38 | 39 | for (int i = 1; i <= numContacts; i++) { 40 | contacts.add(new Contact("Contact " + i)); 41 | } 42 | 43 | return contacts; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/contacts/ContactsArrayAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.contacts; 18 | 19 | import android.content.Context; 20 | import android.graphics.Bitmap; 21 | import android.graphics.BitmapFactory; 22 | import android.view.LayoutInflater; 23 | import android.view.View; 24 | import android.view.ViewGroup; 25 | import android.widget.ArrayAdapter; 26 | import android.widget.ImageView; 27 | import android.widget.TextView; 28 | 29 | import com.google.android.perftesting.R; 30 | 31 | import java.util.List; 32 | 33 | /** 34 | * ContactsArrayAdapter adapts a Contact to a View for use in a ListView. 35 | */ 36 | public class ContactsArrayAdapter extends ArrayAdapter { 37 | 38 | public final String TAG = "ContactsArrayAdapter"; 39 | 40 | public ContactsArrayAdapter(Context context, List contacts) { 41 | super(context, 0, contacts); 42 | } 43 | 44 | @ Override 45 | public View getView(int position, View convertView, ViewGroup parent ) { 46 | 47 | Contact contact = getItem(position); 48 | LayoutInflater inflater = LayoutInflater.from(getContext()); 49 | 50 | // This line is wrong, we're inflating a new view always instead of only if it's null. 51 | // For demonstration purposes, we will leave this here to show the resulting jank. 52 | convertView = inflater.inflate(R.layout.item_contact, parent, false); 53 | 54 | TextView contactName = (TextView) convertView.findViewById(R.id.contact_name); 55 | ImageView contactImage = (ImageView) convertView.findViewById(R.id.contact_image); 56 | 57 | contactName.setText(contact.getName()); 58 | 59 | // Let's just create another bitmap when we need one. This makes no attempts to re-use 60 | // bitmaps that were previously used in rendering past list view elements, causing a large 61 | // amount of memory to be consumed as you scroll farther down the list. 62 | Bitmap bm = BitmapFactory.decodeResource(convertView.getResources(), R.drawable.bbq); 63 | contactImage.setImageBitmap(bm); 64 | return convertView; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/contacts/ContactsArrayAdapterFixed.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.contacts; 18 | 19 | import android.content.Context; 20 | import android.view.LayoutInflater; 21 | import android.view.View; 22 | import android.view.ViewGroup; 23 | import android.widget.ArrayAdapter; 24 | import android.widget.ImageView; 25 | import android.widget.TextView; 26 | 27 | import com.google.android.perftesting.R; 28 | 29 | import com.bumptech.glide.Glide; 30 | 31 | import java.util.List; 32 | 33 | /** 34 | * ContactsArrayAdapter adapts a Contact to a View for use in a ListView. 35 | */ 36 | public class ContactsArrayAdapterFixed extends ArrayAdapter { 37 | 38 | public final String TAG = "ContactsArrayAdapter"; 39 | 40 | public ContactsArrayAdapterFixed(Context context, List contacts) { 41 | super(context, 0, contacts); 42 | } 43 | 44 | @ Override 45 | public View getView(int position, View convertView, ViewGroup parent ) { 46 | 47 | Contact contact = getItem(position); 48 | LayoutInflater inflater = LayoutInflater.from(getContext()); 49 | 50 | if (convertView == null) { 51 | convertView = inflater.inflate(R.layout.item_contact, parent, false); 52 | } 53 | 54 | TextView contactName = (TextView) convertView.findViewById(R.id.contact_name); 55 | ImageView contactImage = (ImageView) convertView.findViewById(R.id.contact_image); 56 | 57 | Glide.with(contactImage.getContext()) 58 | .load(R.drawable.bbq) 59 | .fitCenter() 60 | .into(contactImage); 61 | 62 | contactName.setText(contact.getName()); 63 | 64 | return convertView; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/google/android/perftesting/contacts/ContactsRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc. 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.google.android.perftesting.contacts; 18 | 19 | import android.content.Context; 20 | import android.support.v7.widget.RecyclerView; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.ImageView; 25 | import android.widget.TextView; 26 | 27 | import com.google.android.perftesting.R; 28 | 29 | import com.bumptech.glide.Glide; 30 | 31 | import java.util.List; 32 | 33 | /** 34 | * ContactsRecyclerAdapter adapts a Contact to a View for use in a RecyclerView. 35 | */ 36 | public class ContactsRecyclerAdapter extends RecyclerView.Adapter { 37 | 38 | private static final String TAG = "ContactsRecyclerAdapter"; 39 | 40 | private List mContacts; 41 | 42 | public ContactsRecyclerAdapter(List contacts) { 43 | mContacts = contacts; 44 | } 45 | 46 | @Override 47 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 48 | Context context = parent.getContext(); 49 | LayoutInflater inflater = LayoutInflater.from(context); 50 | 51 | View contactView = inflater.inflate(R.layout.item_contact, parent, false); 52 | 53 | return new ViewHolder(contactView); 54 | } 55 | 56 | @Override 57 | public void onBindViewHolder(ViewHolder holder, int position) { 58 | Contact contact = mContacts.get(position); 59 | 60 | TextView textView = holder.nameTextView; 61 | ImageView contactPic = holder.imageView; 62 | 63 | textView.setText(contact.getName()); 64 | Glide.with(contactPic.getContext()) 65 | .load(R.drawable.bbq) 66 | .fitCenter() 67 | .into(contactPic); 68 | } 69 | 70 | @Override 71 | public int getItemCount() { 72 | return mContacts.size(); 73 | } 74 | 75 | public static class ViewHolder extends RecyclerView.ViewHolder { 76 | 77 | public TextView nameTextView; 78 | public ImageView imageView; 79 | 80 | public ViewHolder(View itemView) { 81 | super(itemView); 82 | 83 | nameTextView = (TextView) itemView.findViewById(R.id.contact_name); 84 | imageView = (ImageView) itemView.findViewById(R.id.contact_image); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/bbq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlecodelabs/android-perf-testing/211ff4cb39c91702d2872c0015451b67bbd818ee/app/src/main/res/drawable-hdpi/bbq.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/bbq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlecodelabs/android-perf-testing/211ff4cb39c91702d2872c0015451b67bbd818ee/app/src/main/res/drawable-ldpi/bbq.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/bbq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlecodelabs/android-perf-testing/211ff4cb39c91702d2872c0015451b67bbd818ee/app/src/main/res/drawable-mdpi/bbq.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/bbq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlecodelabs/android-perf-testing/211ff4cb39c91702d2872c0015451b67bbd818ee/app/src/main/res/drawable-xhdpi/bbq.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/bbq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlecodelabs/android-perf-testing/211ff4cb39c91702d2872c0015451b67bbd818ee/app/src/main/res/drawable-xxhdpi/bbq.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxxhdpi/bbq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlecodelabs/android-perf-testing/211ff4cb39c91702d2872c0015451b67bbd818ee/app/src/main/res/drawable-xxxhdpi/bbq.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_recyclerview.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 19 | 20 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_simple_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 23 | 24 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 24 | 29 | 34 |