अपने ऐप्लिकेशन के कोड में बदलाव करके, Microbenchmark लाइब्रेरी का इस्तेमाल करने का तरीका जानने के लिए, क्विकस्टार्ट सेक्शन देखें. अपने कोडबेस में ज़्यादा मुश्किल बदलाव करके, पूरा सेटअप करने का तरीका जानने के लिए, प्रोजेक्ट का पूरा सेटअप सेक्शन देखें.
क्विकस्टार्ट
इस सेक्शन में, बेंचमार्किंग आज़माने और मॉड्यूल में कोड ले जाए बिना, एक बार के लिए मेज़रमेंट करने का तरीका बताया गया है. परफ़ॉर्मेंस के सटीक नतीजे पाने के लिए, इन चरणों में अपने ऐप्लिकेशन में डीबग करने की सुविधा बंद करना शामिल है. इसलिए, इसे अपनी लोकल वर्किंग कॉपी में रखें और सोर्स कंट्रोल सिस्टम में बदलाव न करें.
एक बार के लिए बेंचमार्किंग करने के लिए, यह तरीका अपनाएं:
अपने मॉड्यूल की
build.gradleयाbuild.gradle.ktsफ़ाइल में लाइब्रेरी जोड़ें:Kotlin
dependencies { implementation("androidx.benchmark:benchmark-junit4:1.2.4") }
शानदार
dependencies { implementation 'androidx.benchmark:benchmark-junit4:1.2.4' }
androidTestImplementationडिपेंडेंसी के बजाय,implementationडिपेंडेंसी का इस्तेमाल करें. अगरandroidTestImplementationका इस्तेमाल किया जाता है, तो बेंचमार्क रन नहीं हो पाते, क्योंकि लाइब्रेरी मेनिफ़ेस्ट को ऐप्लिकेशन मेनिफ़ेस्ट में मर्ज नहीं किया जाता.debugबिल्ड टाइप को अपडेट करें, ताकि इसे डीबग न किया जा सके:Kotlin
android { ... buildTypes { debug { isDebuggable = false } } }
शानदार
android { ... buildTypes { debug { debuggable false } } }
testInstrumentationRunnerको बदलकरAndroidBenchmarkRunnerकरें:Kotlin
android { ... defaultConfig { testInstrumentationRunner = "androidx.benchmark.junit4.AndroidBenchmarkRunner" } }
शानदार
android { ... defaultConfig { testInstrumentationRunner "androidx.benchmark.junit4.AndroidBenchmarkRunner" } }
अपना बेंचमार्क जोड़ने के लिए,
androidTestडायरेक्ट्री में मौजूद किसी टेस्ट फ़ाइल मेंBenchmarkRuleका इंस्टेंस जोड़ें. बेंचमार्क लिखने के बारे में ज़्यादा जानने के लिए, Microbenchmark क्लास बनाना लेख पढ़ें.यहां दिए गए कोड स्निपेट में, इंस्ट्रुमेंटेड टेस्ट में बेंचमार्क जोड़ने का तरीका बताया गया है:
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() ); } } }
बेंचमार्क लिखने का तरीका जानने के लिए, Microbenchmark क्लास बनाना लेख पर जाएं.
प्रोजेक्ट का पूरा सेटअप
एक बार के लिए बेंचमार्किंग के बजाय, नियमित तौर पर बेंचमार्किंग करने के लिए, बेंचमार्क को उनके मॉड्यूल में अलग करें. इससे यह पक्का करने में मदद मिलती है कि उनका कॉन्फ़िगरेशन,
जैसे कि debuggable को false पर सेट करना, सामान्य टेस्ट से अलग हो.
Microbenchmark, आपके कोड को सीधे रन करता है. इसलिए, जिस कोड की बेंचमार्किंग करनी है उसे अलग Gradle मॉड्यूल में रखें और उस मॉड्यूल पर डिपेंडेंसी सेट करें. इसके लिए, पहली इमेज में दिखाया गया तरीका अपनाएं.
:app,
:microbenchmark, और :benchmarkable Gradle
मॉड्यूल शामिल हैं. इससे Microbenchmarks,
:benchmarkable मॉड्यूल में कोड की बेंचमार्किंग कर सकते हैं.नया Gradle मॉड्यूल जोड़ने के लिए, Android Studio में मॉड्यूल विज़र्ड का इस्तेमाल किया जा सकता है. विज़र्ड, बेंचमार्किंग के लिए पहले से कॉन्फ़िगर किया गया मॉड्यूल बनाता है. इसमें बेंचमार्क डायरेक्ट्री जोड़ी जाती है और debuggable को false पर सेट किया जाता है.
Android Studio में प्रोजेक्ट पैनल में, अपने प्रोजेक्ट या मॉड्यूल पर राइट क्लिक करें. इसके बाद, नया > मॉड्यूल पर क्लिक करें.
टेंप्लेट पैनल में, बेंचमार्क चुनें.
बेंचमार्क मॉड्यूल टाइप के तौर पर, Microbenchmark चुनें.
मॉड्यूल के नाम के लिए, "microbenchmark" टाइप करें.
पूरा करें पर क्लिक करें.
मॉड्यूल बन जाने के बाद, उसकी build.gradle या build.gradle.kts
फ़ाइल में बदलाव करें. इसके बाद, बेंचमार्क करने के लिए कोड वाले मॉड्यूल में androidTestImplementation जोड़ें:
Kotlin
dependencies { // The module name might be different. androidTestImplementation(project(":benchmarkable")) }
शानदार
dependencies { // The module name might be different. androidTestImplementation project(':benchmarkable') }
Microbenchmark क्लास बनाना
बेंचमार्क, इंस्ट्रुमेंटेशन के स्टैंडर्ड टेस्ट होते हैं. बेंचमार्क बनाने के लिए, लाइब्रेरी की ओर से दी गई
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 की तरह रन करें. इसके लिए, अपनी टेस्ट क्लास या तरीके के बगल में मौजूद गटर ऐक्शन का इस्तेमाल करें. इसके लिए, तीसरी इमेज में दिखाया गया तरीका अपनाएं.
इसके अलावा, कमांड लाइन से, तय किए गए Gradle मॉड्यूल के सभी टेस्ट रन करने के लिए, connectedCheck रन करें:
./gradlew benchmark:connectedCheckया कोई एक टेस्ट रन करें:
./gradlew benchmark:connectedCheck -P android.testInstrumentationRunnerArguments.class=com.example.benchmark.SampleBenchmark#benchmarkSomeWorkबेंचमार्क के नतीजे
Microbenchmark के सफलतापूर्वक रन होने के बाद, मेट्रिक सीधे 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" } }
शानदार
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}