ตั้งค่าสภาพแวดล้อมสำหรับการทดสอบประสิทธิภาพ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณสามารถระบุจุดคอขวดที่อาจเกิดขึ้นและปรับปรุงประสิทธิภาพโดยรวมของแอปได้โดยบันทึกกิจกรรมในอุปกรณ์ในช่วงเวลาสั้นๆ และรวบรวมร่องรอยของช่วงเริ่มต้นของแอป หน้านี้แสดงวิธี
ตั้งค่าสภาพแวดล้อมสำหรับการทดสอบประสิทธิภาพ
ใช้ไลบรารี Macrobenchmark
ไลบรารี
Macrobenchmark จะวัด
การโต้ตอบของผู้ใช้ปลายทางที่ใหญ่ขึ้น เช่น การเริ่มต้น การโต้ตอบกับ UI และ
ภาพเคลื่อนไหว ไลบรารีช่วยให้คุณควบคุมสภาพแวดล้อมด้านประสิทธิภาพที่คุณกำลังทดสอบได้โดยตรง
ซึ่งช่วยให้คุณควบคุมการคอมไพล์ การเริ่มต้น และการหยุดแอป
เพื่อวัดเวลาเริ่มต้นแอปที่แม่นยำได้โดยตรง นอกจากนี้ยังช่วยลด
สัญญาณรบกวนและความแตกต่างระหว่างการทดสอบ
ใช้อุปกรณ์ระดับกลางเพื่อระบุปัญหาด้านประสิทธิภาพที่อาจเกิดขึ้น
ทดสอบประสิทธิภาพในอุปกรณ์แต่ละประเภทที่คุณสนใจ อุปกรณ์ระดับไฮเอนด์ที่มีคอมโพเนนต์ที่รวดเร็วอาจซ่อนปัญหาด้านประสิทธิภาพในอุปกรณ์รุ่นก่อนหน้าที่มีความเร็วช้ากว่าหรือมี RAM น้อย
อุปกรณ์ระดับล่างอาจใช้เวลานานกว่าในการโหลดข้อมูลหรือเรียกใช้โค้ด ซึ่งทำให้ระบุจุดคอขวดได้ง่ายขึ้น
โดยปกติแล้วการเพิ่มประสิทธิภาพสำหรับอุปกรณ์ระดับล่างยัง
เป็นประโยชน์ต่อการเพิ่มประสิทธิภาพสำหรับอุปกรณ์ระดับสูงด้วย
ลดเสียงรบกวน
- เครือข่าย: ทดสอบแอปหรือกระบวนการด้วยความเร็ว Wi-Fi ของอินเทอร์เน็ตที่เสถียรและรวดเร็ว
หากเวลาเริ่มต้นของแอปมีคำขอเครือข่าย ให้สังเกตว่าส่วนนี้อาจมีความแปรปรวน
- การใช้ RAM: อย่าให้แอปอื่นๆ ทำงานในเบื้องหลังของอุปกรณ์
ขณะทดสอบประสิทธิภาพการเริ่มต้นแอป
- แบตเตอรี่: ตรวจสอบว่าอุปกรณ์ชาร์จแบตเตอรี่แล้วเพื่อหลีกเลี่ยงการจำกัดประสิทธิภาพการทำงานที่ใช้พลังงานต่ำของฮาร์ดแวร์
ทดสอบในบิลด์รุ่น
ใช้บิลด์รุ่นเพื่อทดสอบประสิทธิภาพ บิลด์ดีบักไม่เหมาะสําหรับการ
ดีบักประสิทธิภาพ เนื่องจาก
ไม่ได้ให้การเพิ่มประสิทธิภาพการคอมไพล์และส่งผลต่อประสิทธิภาพอย่างมาก
อย่างไรก็ตาม คุณใช้บิลด์รุ่นที่ไม่ได้ปิดบังเพื่อระบุชื่อคลาสและชื่อการดำเนินการได้
โดยเฉพาะอย่างยิ่ง เราขอแนะนำให้เปิดใช้ minify
(R8) และปิดใช้การปกปิดโค้ด โดยมี
-dontobfuscate
ในไฟล์ Proguard
การระบุเลย์เอาต์ ชิ้นงาน และทรัพยากรจะง่ายขึ้นหากบิลด์ไม่ได้
ปิดบัง
ตรวจสอบว่าคุณได้รวมแฟล็ก
profileable ไว้ในไฟล์ Manifest
เพื่อให้เหตุการณ์ที่กําหนดเองปรากฏในบิลด์ที่แก้ไขข้อบกพร่องไม่ได้ โดยฟีเจอร์นี้พร้อมใช้งานใน Android 10 (API ระดับ 29) ขึ้นไป
เพิ่มการติดตามที่กำหนดเองลงในการดำเนินการของแอป
เพิ่มการติดตามที่กำหนดเองภายในแอปเพื่อให้ระบุได้ง่ายขึ้นว่าแอปของคุณดำเนินการใดบ้างเมื่อเทียบกับ
ไลบรารีอื่นๆ ซึ่งจะช่วยให้คุณมีบริบทเพิ่มเติมเกี่ยวกับสิ่งที่แอปกำลังทำอยู่ตลอดเวลา
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Set up your environment for performance testing\n\nYou can identify potential bottlenecks and improve overall app performance by\nrecording device activity over a short period of time and [collecting traces of\nyour app's startup period](/topic/performance/tracing). This page shows how to\nset up your environment for performance testing.\n\nUse the Macrobenchmark library\n------------------------------\n\nThe [Macrobenchmark\nlibrary](/topic/performance/benchmarking/macrobenchmark-overview) measures\nlarger end-user interactions, such as startup, interacting with the UI, and\nanimations. The library provides direct control over the performance environment\nyou're testing. It lets you control compiling, starting, and stopping your app\nto directly measure precise app startup time. It also works to minimize the\nnoise and differences between test runs.\n\nUse mid-range devices to identify potential performance issues\n--------------------------------------------------------------\n\nTest performance on each device type you care about. High-end devices with fast\ncomponents can hide performance problems on earlier, slower, or low RAM devices.\nLower-end devices can take longer to load data or run code, making it easier to\nidentify bottlenecks. Optimizing performance for low-end devices usually also\nbenefits optimization for high-end devices.\n\nReduce noise\n------------\n\n- Network: test your apps or processes with strong and stable internet Wi-Fi speeds. If the app startup time includes a network request, note this as a place where variability might occur.\n- RAM usage: don't have any other apps running in the background of your device while testing app startup performance.\n- Battery: ensure your device is charged to avoid any hardware-specific low power performance throttling.\n\nTest on release builds\n----------------------\n\nUse release builds to test performance. Debug builds are [unsuitable for\nperformance\ndebugging](/studio/profile/measuring-performance#apk-considerations), as they\ndon't provide compilation optimization and significantly impact performance.\n\nHowever, it's okay to use an unobfuscated release build to identify classes and\noperation names. Specifically, we recommend enabling [minify\n(R8)](/studio/build/shrink-code) and disabling obfuscation, with\n[`-dontobfuscate`](/studio/build/shrink-code#obfuscate) in the proguard file.\nIt's easier to identify layouts, assets, and resources if the build is\nunobfuscated.\n\nMake sure you include the\n[profileable](/guide/topics/manifest/profileable-element) flag in the manifest\nso that your custom events are visible in non-debuggable builds. This flag is\navailable on Android 10 (API level 29) and later.\n\nAdd custom traces to your app operations\n----------------------------------------\n\nAdd [custom traces](/topic/performance/tracing/custom-events) within your app to\nmake it easier to identify what operations are performed by your app compared to\nother libraries. This helps give you more context about what the app is doing at\nall times."]]