แนวคิดและการใช้งาน Jetpack Compose
การทดสอบ Macrobenchmark จะทํางานในกระบวนการแยกต่างหากจากแอป ซึ่งแตกต่างจากการทดสอบ UI ของ Android ส่วนใหญ่ ซึ่งจำเป็นต่อการเปิดใช้สิ่งต่างๆ เช่น การหยุดกระบวนการของแอปและการคอมไพล์จากไบต์โค้ด DEX เป็นรหัสเครื่อง
คุณสามารถขับเคลื่อนสถานะของแอปได้โดยใช้ไลบรารี UIAutomator หรือกลไกอื่นๆ
ที่ควบคุมแอปเป้าหมายจากกระบวนการทดสอบได้
คุณไม่สามารถใช้ Espresso หรือ ActivityScenario สำหรับ
Macrobenchmark เนื่องจากเครื่องมือเหล่านี้คาดว่าจะทำงานในกระบวนการที่ใช้ร่วมกันกับแอป
ตัวอย่างต่อไปนี้ค้นหา RecyclerView โดยใช้รหัสทรัพยากรและ
เลื่อนลงหลายครั้ง
Kotlin
@Test fun scrollList() { benchmarkRule.measureRepeated( // ... setupBlock = { uiAutomator { // Before starting to measure, navigate to the UI to be measured startIntent(Intent("$packageName.RECYCLER_VIEW_ACTIVITY")) } } ) { uiAutomator { val recycler = onElement { className == "androidx.recyclerview.widget.RecyclerView" } // Scroll down several times repeat(3) { recycler.fling(Direction.DOWN) } } } }
Java
@Test public void scrollList() { benchmarkRule.measureRepeated( // ... /* setupBlock */ scope -> { // Before measuring, navigate to the UI to be measured. val intent = Intent("$packageName.RECYCLER_VIEW_ACTIVITY") scope.startActivityAndWait(); return Unit.INSTANCE; }, /* measureBlock */ scope -> { UiDevice device = scope.getDevice(); UiObject2 recycler = device.findObject(By.res(scope.getPackageName(), "recycler")); // Set gesture margin to avoid triggering gesture navigation // with input events from automation. recycler.setGestureMargin(device.getDisplayWidth() / 5); // Fling the recycler several times. for (int i = 0; i < 3; i++) { recycler.fling(Direction.DOWN); } return Unit.INSTANCE; } ); }
การทดสอบประสิทธิภาพไม่จำเป็นต้องเลื่อน UI แต่สามารถเรียกใช้ ภาพเคลื่อนไหวแทนได้ และไม่จำเป็นต้องใช้ UI Automator โดยเฉพาะ โดยจะรวบรวมเมตริกประสิทธิภาพตราบใดที่ยังมีการสร้างเฟรม
ไปยังส่วนภายในของแอป
บางครั้งคุณอาจต้องการเปรียบเทียบประสิทธิภาพของบางส่วนในแอปที่เข้าถึงจากภายนอกไม่ได้โดยตรง เช่น การเข้าถึงกิจกรรมภายใน
ที่ทำเครื่องหมายด้วย exported=false การไปยัง Fragment หรือ
การปัดส่วนใดส่วนหนึ่งของ UI ออก โดยเกณฑ์เปรียบเทียบต้องไปยังส่วนต่างๆ ของแอปเหล่านี้ด้วยตนเองเหมือนผู้ใช้
หากต้องการไปยังส่วนต่างๆ ด้วยตนเอง ให้เปลี่ยนโค้ดภายใน setupBlock{} ให้มี
เอฟเฟกต์ที่ต้องการ เช่น การแตะปุ่มหรือการปัด measureBlock{} ของคุณมี
เฉพาะการจัดการ UI ที่คุณต้องการเปรียบเทียบจริงๆ ดังนี้
Kotlin
@Test fun nonExportedActivityScrollList() { benchmarkRule.measureRepeated( // ... setupBlock = setupBenchmark() ) { // ... } } private fun setupBenchmark(): MacrobenchmarkScope.() -> Unit = { uiAutomator { // Before starting to measure, navigate to the UI to be measured startApp(TARGET_PACKAGE) // click a button to launch the target activity. onElement { textAsString() == "RecyclerView" }.click() // wait until the activity is shown waitForStableInActiveWindow() } }
Java
@Test public void scrollList() { benchmarkRule.measureRepeated( // ... /* setupBlock */ scope -> { // Before measuring, navigate to the default activity. scope.startActivityAndWait(); // Click a button to launch the target activity. // While you use resourceId here to find the button, you can also // use accessibility info or button text content. UiObject2 launchRecyclerActivity = scope.getDevice().findObject( By.res(packageName, "launchRecyclerActivity") ) launchRecyclerActivity.click(); // Wait until activity is shown. scope.getDevice().wait( Until.hasObject(By.clazz("$packageName.NonExportedRecyclerActivity")), 10000L ) return Unit.INSTANCE; }, /* measureBlock */ scope -> { // ... } ); }
แนะนำสำหรับคุณ
- หมายเหตุ: ข้อความลิงก์จะแสดงเมื่อ JavaScript ปิดอยู่
- การเขียนการทดสอบประสิทธิภาพระดับสูง
- บันทึกเมตริก Macrobenchmark
- Microbenchmark