सिद्धांत और Jetpack Compose में लागू करना
ज़्यादातर Android यूज़र इंटरफ़ेस (यूआई) टेस्ट के उलट, मैक्रोबेंचमार्क टेस्ट, ऐप्लिकेशन से अलग प्रोसेस में चलते हैं. ऐसा इसलिए ज़रूरी है, ताकि ऐप्लिकेशन की प्रोसेस को बंद करने और DEX बाइटकोड से मशीन कोड में कंपाइल करने जैसी सुविधाएं चालू की जा सकें.
UIAutomator लाइब्रेरी या अन्य ऐसे तरीकों का इस्तेमाल करके, अपने ऐप्लिकेशन की स्थिति को कंट्रोल किया जा सकता है जो टेस्ट प्रोसेस के दौरान टारगेट ऐप्लिकेशन को कंट्रोल कर सकते हैं.
मैक्रोबेंचमार्क के लिए, Espresso या ActivityScenario का इस्तेमाल नहीं किया जा सकता, क्योंकि ये ऐप्लिकेशन के साथ शेयर की गई प्रोसेस में चलते हैं.
यहां दिए गए उदाहरण में, रिसॉर्स आईडी का इस्तेमाल करके 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; } ); }
आपके बेंचमार्क को यूज़र इंटरफ़ेस (यूआई) को स्क्रोल करने की ज़रूरत नहीं है. इसके बजाय, यह कोई ऐनिमेशन चला सकता है. इसके लिए, खास तौर पर यूआई ऑटोमेटर का इस्तेमाल करना भी ज़रूरी नहीं है. यह परफ़ॉर्मेंस मेट्रिक तब तक इकट्ठा करता है, जब तक फ़्रेम जनरेट होते रहते हैं.
ऐप्लिकेशन के अंदरूनी हिस्सों पर जाना
कभी-कभी, आपको अपने ऐप्लिकेशन के उन हिस्सों की परफ़ॉर्मेंस की तुलना करनी होती है जिन्हें सीधे तौर पर ऐक्सेस नहीं किया जा सकता. उदाहरण के लिए, exported=false के तौर पर मार्क की गई इनर ऐक्टिविटी ऐक्सेस करना, Fragment पर जाना या यूज़र इंटरफ़ेस (यूआई) के किसी हिस्से को स्वाइप करके हटाना. मानदंडों को मैन्युअल तरीके से, ऐप्लिकेशन के इन हिस्सों पर नेविगेट करना होगा. जैसे, कोई उपयोगकर्ता करता है.
मैन्युअल तरीके से नेविगेट करने के लिए, setupBlock{} में मौजूद कोड में बदलाव करें, ताकि उसमें आपकी पसंद का इफ़ेक्ट शामिल हो. जैसे, बटन टैप करना या स्वाइप करना. आपके measureBlock{} में सिर्फ़ यूज़र इंटरफ़ेस (यूआई) में किए गए ऐसे बदलाव शामिल होने चाहिए जिनकी आपको तुलना करनी है:
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 लिखना
- Macrobenchmark मेट्रिक कैप्चर करना
- माइक्रोबेंचमार्क