अपने ऐप्लिकेशन कोड में बदलाव करके, Microbenchmark लाइब्रेरी इस्तेमाल करने का तरीका जानने के लिए, क्विकस्टार्ट सेक्शन देखें. अपने कोड बेस में ज़्यादा जटिल बदलावों के साथ पूरा सेटअप करने का तरीका जानने के लिए, पूरा प्रोजेक्ट सेटअप सेक्शन देखें.
क्विकस्टार्ट
इस सेक्शन में, मानदंड की सुविधा को आज़माने और एक बार मेज़रमेंट करने का तरीका बताया गया है. इसके लिए, कोड को मॉड्यूल में ले जाने की ज़रूरत नहीं होती. परफ़ॉर्मेंस के सटीक नतीजे पाने के लिए, इन चरणों में आपके ऐप्लिकेशन में डीबग करने की सुविधा को बंद करना शामिल है. इसलिए, इसे सोर्स कंट्रोल सिस्टम में बदलाव किए बिना, लोकल वर्किंग कॉपी में रखें.
एक बार मानदंड तय करने के लिए, यह तरीका अपनाएं:
लाइब्रेरी को अपने मॉड्यूल की
build.gradleयाbuild.gradle.ktsफ़ाइल में जोड़ें:Kotlin
dependencies { implementation("androidx.benchmark:benchmark-junit4:1.2.4") }
Groovy
dependencies { implementation 'androidx.benchmark:benchmark-junit4:1.2.4' }
androidTestImplementationडिपेंडेंसी के बजाय,implementationडिपेंडेंसी का इस्तेमाल करें.androidTestImplementationका इस्तेमाल करने पर, मानदंड नहीं चल पाते. ऐसा इसलिए होता है, क्योंकि लाइब्रेरी मेनिफ़ेस्ट को ऐप्लिकेशन मेनिफ़ेस्ट में मर्ज नहीं किया जाता.debugबिल्ड टाइप को अपडेट करें, ताकि उसे डीबग न किया जा सके:Kotlin
android { ... buildTypes { debug { isDebuggable = false } } }
Groovy
android { ... buildTypes { debug { debuggable false } } }
testInstrumentationRunnerकोAndroidBenchmarkRunnerमें बदलें:Kotlin
android { ... defaultConfig { testInstrumentationRunner = "androidx.benchmark.junit4.AndroidBenchmarkRunner" } }
Groovy
android { ... defaultConfig { testInstrumentationRunner "androidx.benchmark.junit4.AndroidBenchmarkRunner" } }
अपनी मानदंड जोड़ने के लिए,
androidTestडायरेक्ट्री में मौजूद टेस्ट फ़ाइल मेंBenchmarkRuleका एक इंस्टेंस जोड़ें. बेंचमार्क लिखने के बारे में ज़्यादा जानने के लिए, माइक्रोबेंचमार्क क्लास बनाना लेख पढ़ें.यहां दिए गए कोड स्निपेट में, इंस्ट्रुमेंटेड टेस्ट में मानदंड जोड़ने का तरीका बताया गया है:
Kotlin
@RunWith(AndroidJUnit4::class) class SampleBenchmark { @get:Rule val benchmarkRule = BenchmarkRule() @Test fun benchmarkSomeWork() { benchmarkRule.measureRepeated { doSomeWork() } } }
Java
@RunWith(AndroidJUnit4.class) class SampleBenchmark { @Rule public BenchmarkRule benchmarkRule = new BenchmarkRule(); @Test public void benchmarkSomeWork() { BenchmarkRuleKt.measureRepeated( (Function1<BenchmarkRule.Scope, Unit>) scope -> doSomeWork() ); } } }
बेंचमार्क लिखने का तरीका जानने के लिए, माइक्रोबेंचमार्क क्लास बनाना पर जाएं.
प्रोजेक्ट का पूरा सेटअप
एक बार बेंचमार्क करने के बजाय, नियमित तौर पर बेंचमार्क करने के लिए, बेंचमार्क को उनके अपने मॉड्यूल में अलग करें. इससे यह पक्का करने में मदद मिलती है कि उनका कॉन्फ़िगरेशन, सामान्य टेस्ट से अलग हो. जैसे, debuggable को false पर सेट करना.
Microbenchmark आपके कोड को सीधे तौर पर चलाता है. इसलिए, जिस कोड की परफ़ॉर्मेंस की जांच करनी है उसे एक अलग Gradle मॉड्यूल में रखें. साथ ही, उस मॉड्यूल पर डिपेंडेंसी सेट करें, जैसा कि इमेज 1 में दिखाया गया है.
:app, :microbenchmark, और :benchmarkable Gradle मॉड्यूल वाला ऐप्लिकेशन स्ट्रक्चर. इसकी मदद से, Microbenchmarks, :benchmarkable मॉड्यूल में कोड को बेंचमार्क कर सकता है.नया Gradle मॉड्यूल जोड़ने के लिए, Android Studio में मॉड्यूल विज़र्ड का इस्तेमाल किया जा सकता है. विज़र्ड, मानदंड के लिए पहले से कॉन्फ़िगर किया गया एक मॉड्यूल बनाता है. इसमें मानदंड डायरेक्ट्री जोड़ी जाती है और debuggable को false पर सेट किया जाता है.
Android Studio में प्रोजेक्ट पैनल में जाकर, अपने प्रोजेक्ट या मॉड्यूल पर राइट क्लिक करें. इसके बाद, नया > मॉड्यूल पर क्लिक करें.
टेंप्लेट पैनल में, मानदंड को चुनें.
बेंचमार्क मॉड्यूल टाइप के तौर पर, माइक्रोबेंचमार्क चुनें.
मॉड्यूल के नाम के लिए, "microbenchmark" टाइप करें.
पूरा करें पर क्लिक करें.
मॉड्यूल बनाने के बाद, उसकी build.gradle या build.gradle.kts फ़ाइल बदलें. साथ ही, मानदंड के लिए कोड वाले मॉड्यूल में androidTestImplementation जोड़ें:
Kotlin
dependencies { // The module name might be different. androidTestImplementation(project(":benchmarkable")) }
Groovy
dependencies { // The module name might be different. androidTestImplementation project(':benchmarkable') }
माइक्रोबेंचमार्क क्लास बनाना
बेंचमार्क, स्टैंडर्ड इंस्ट्रुमेंटेशन टेस्ट होते हैं. बेंचमार्क बनाने के लिए, लाइब्रेरी में मौजूद BenchmarkRule क्लास का इस्तेमाल करें. गतिविधियों की तुलना करने के लिए, ActivityScenario या ActivityScenarioRule का इस्तेमाल करें. यूआई कोड की परफ़ॉर्मेंस की जांच करने के लिए, @UiThreadTest का इस्तेमाल करें.
यहां दिए गए कोड में, मानदंड का एक सैंपल दिखाया गया है:
Kotlin
@RunWith(AndroidJUnit4::class) class SampleBenchmark { @get:Rule val benchmarkRule = BenchmarkRule() @Test fun benchmarkSomeWork() { benchmarkRule.measureRepeated { doSomeWork() } } }
Java
@RunWith(AndroidJUnit4.class) class SampleBenchmark { @Rule public BenchmarkRule benchmarkRule = new BenchmarkRule(); @Test public void benchmarkSomeWork() { final BenchmarkState state = benchmarkRule.getState(); while (state.keepRunning()) { doSomeWork(); } } }
सेटअप के लिए समय तय करने की सुविधा बंद करना
कोड के जिन सेक्शन को आपको runWithTimingDisabled{} ब्लॉक की मदद से मेज़र नहीं करना है उनके लिए टाइमिंग को बंद किया जा सकता है. आम तौर पर, इन सेक्शन में कुछ कोड होते हैं जिन्हें आपको मानदंड के हर दोहराव पर चलाना होता है.
Kotlin
// using random with the same seed, so that it generates the same data every run private val random = Random(0) // create the array once and just copy it in benchmarks private val unsorted = IntArray(10_000) { random.nextInt() } @Test fun benchmark_quickSort() { // ... benchmarkRule.measureRepeated { // copy the array with timing disabled to measure only the algorithm itself listToSort = runWithTimingDisabled { unsorted.copyOf() } // sort the array in place and measure how long it takes SortingAlgorithms.quickSort(listToSort) } // assert only once not to add overhead to the benchmarks assertTrue(listToSort.isSorted) }
Java
private final int[] unsorted = new int[10000]; public SampleBenchmark() { // Use random with the same seed, so that it generates the same data every // run. Random random = new Random(0); // Create the array once and copy it in benchmarks. Arrays.setAll(unsorted, (index) -> random.nextInt()); } @Test public void benchmark_quickSort() { final BenchmarkState state = benchmarkRule.getState(); int[] listToSort = new int[0]; while (state.keepRunning()) { // Copy the array with timing disabled to measure only the algorithm // itself. state.pauseTiming(); listToSort = Arrays.copyOf(unsorted, 10000); state.resumeTiming(); // Sort the array in place and measure how long it takes. SortingAlgorithms.quickSort(listToSort); } // Assert only once, not to add overhead to the benchmarks. assertTrue(SortingAlgorithmsKt.isSorted(listToSort)); }
measureRepeated ब्लॉक और runWithTimingDisabled के अंदर किए जाने वाले काम को कम से कम करने की कोशिश करें. measureRepeated ब्लॉक को कई बार चलाया जाता है. इससे मानदंड को चलाने में लगने वाले कुल समय पर असर पड़ सकता है. अगर आपको मानदंड के कुछ नतीजों की पुष्टि करनी है, तो मानदंड के हर दोहराव के बजाय, आखिरी नतीजे की पुष्टि की जा सकती है.
बेंचमार्क टेस्ट रन करना
Android Studio में, अपने मानदंड को उसी तरह चलाएं जिस तरह किसी @Test को चलाया जाता है. इसके लिए, अपनी टेस्ट क्लास या तरीके के बगल में मौजूद गटर ऐक्शन का इस्तेमाल करें. इसकी जानकारी, तीसरी इमेज में दी गई है.
इसके अलावा, कमांड लाइन से connectedCheck चलाएं, ताकि Gradle मॉड्यूल में मौजूद सभी टेस्ट चलाए जा सकें:
./gradlew benchmark:connectedCheckया फिर, एक टेस्ट:
./gradlew benchmark:connectedCheck -P android.testInstrumentationRunnerArguments.class=com.example.benchmark.SampleBenchmark#benchmarkSomeWorkबेंचमार्क के नतीजे
माइक्रोबेंचमार्क के सही तरीके से चलने के बाद, मेट्रिक सीधे Android Studio में दिखती हैं. साथ ही, अतिरिक्त मेट्रिक और डिवाइस की जानकारी वाली पूरी बेंचमार्क रिपोर्ट, JSON फ़ॉर्मैट में उपलब्ध होती है.
JSON रिपोर्ट और प्रोफ़ाइलिंग ट्रेस भी डिवाइस से होस्ट पर अपने-आप कॉपी हो जाते हैं. ये होस्ट मशीन पर इस जगह पर लिखे जाते हैं:
project_root/module/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/device_id/
डिफ़ॉल्ट रूप से, JSON रिपोर्ट को टेस्ट APK के बाहरी शेयर किए गए मीडिया फ़ोल्डर में, उपयोगकर्ता के डिवाइस पर डिस्क में सेव किया जाता है. आम तौर पर, यह फ़ोल्डर /storage/emulated/0/Android/media/**app_id**/**app_id**-benchmarkData.json में मौजूद होता है.
कॉन्फ़िगरेशन से जुड़ी गड़बड़ियां
यह लाइब्रेरी इन स्थितियों का पता लगाती है, ताकि यह पक्का किया जा सके कि आपका प्रोजेक्ट और एनवायरमेंट, रिलीज़ के लिए सटीक परफ़ॉर्मेंस के लिए सेट अप किया गया है:
- डीबग करने लायक को
falseपर सेट किया गया है. - किसी फ़िज़िकल डिवाइस का इस्तेमाल किया जा रहा हो. एम्युलेटर का इस्तेमाल नहीं किया जा सकता.
- अगर डिवाइस रूट किया गया है, तो घड़ियां लॉक हो जाती हैं.
- डिवाइस का बैटरी लेवल कम से कम 25% होना चाहिए.
अगर इनमें से कोई भी जांच पूरी नहीं होती है, तो बेंचमार्क एक गड़बड़ी की रिपोर्ट करता है, ताकि गलत मेज़रमेंट को रोका जा सके.
गड़बड़ी के कुछ टाइप को चेतावनियों के तौर पर दबाने और उन्हें बेंचमार्क को रोकने से रोकने के लिए, गड़बड़ी के टाइप को कॉमा लगाकर अलग की गई लिस्ट में इंस्ट्रुमेंटेशन आर्ग्युमेंट androidx.benchmark.suppressErrors में पास करें.
इसे अपनी Gradle स्क्रिप्ट से सेट किया जा सकता है, जैसा कि इस उदाहरण में दिखाया गया है:
Kotlin
android { defaultConfig { … testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "DEBUGGABLE,LOW-BATTERY" } }
Groovy
android { defaultConfig { … testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "DEBUGGABLE,LOW-BATTERY" } }
कमांड लाइन से भी गड़बड़ियों को दबाया जा सकता है:
$ ./gradlew :benchmark:connectedCheck -P andoidtestInstrumentationRunnerArguments.androidx.benchmark.supperssErrors=DEBUGGABLE,LOW-BATTERY
गड़बड़ियों को छिपाने से, मानदंड को गलत तरीके से कॉन्फ़िगर की गई स्थिति में चलाया जा सकता है. साथ ही, मानदंड के आउटपुट का नाम जान-बूझकर बदल दिया जाता है. इसके लिए, टेस्ट के नामों के आगे गड़बड़ी का नाम जोड़ दिया जाता है. उदाहरण के लिए, पिछले स्निपेट में दिए गए सप्रेशन के साथ डीबग करने लायक मानदंड चलाने पर, टेस्ट के नामों के आगे DEBUGGABLE_ जुड़ जाता है.
आपके लिए सुझाव
- ध्यान दें: JavaScript बंद होने पर, लिंक टेक्स्ट दिखता है
- Macrobenchmark लिखना
- Gradle के बिना Microbenchmarks बनाना
- बेसलाइन प्रोफ़ाइलें बनाना {:#creating-profile-rules}