preface

As you know, Android ADB is a powerful command-line tool that allows you to communicate directly with your device, manipulate it, and retrieve its current performance parameters.

Since it is so powerful, can you extend a performance checking tool with ADB tools? The answer is yes. Of course, performance detection and tuning is an eternal topic, which requires systematic and comprehensive in-depth work. The following uses a memory detection tool as an example.

scenario

In many cases, application memory usage is directly related to application memory usage, such as lag, frame loss, and fast power consumption. How to detect the application memory usage becomes the first step to optimize these scenarios. Using ADB tools, the application memory changes can be obtained by periodic sampling mechanism, and then based on actual business scenarios, the parts to be tuned can be gradually located.

Architecture design

Architecture diagram

  • PerformanceTool: Test entry
  • DeviceFinder: Takes care of finding devices
  • Config: Records and provides various configurations
  • Monitor: Monitors performance parameters. MemoryMonitor is only implemented here
  • CmdExecutor: Executes ADB instructions

Methods to design

In the Monitor, for example

The core can be divided into three methods: start execution, collect data, output chart

Code implementation

The development environment

Python: python3, matplotlib, XLWT, XLRD, xlutils

IDE: VS Code

OS: Windows 10

The key way to

Memory ADB instruction

adb -s [deviceSerialNo] shell dumpsys meminfo  [packageName]
Copy the code

Detection entry PerformanceTool

Separated into three steps

Step 1: Find the device

Step 2: Start the memory detection script

Step 3: Generate Excel files and output charts


from MemoryMonitor import MemoryMonitor
import DeviceFinder
import Config
import time
import os
def main() :
    print("Detection Start")
    # Find device
    isFindDeviceSuccess = DeviceFinder.findDevice()
    if isFindDeviceSuccess ==  False :
        print("Detection End")
        return
    Create a report directory
    if os.path.exists(Config.reportPath) ==  False :
        os.makedirs(Config.reportPath)
    Record the start detection time, unit: s
    startDetectionTime = time.time()
    Execute the memory detection program
    memoryMonitor = MemoryMonitor()
    memoryMonitor.execute()
    # The test is finished and the chart is generated
    while time.time() - startDetectionTime <= Config.detectionTime * 60 :
        continue
    print("generateChart start...")
    memoryMonitor.generateChart()
    print("generateChart end")
    # End all tests
    memoryMonitor.stop()
    print("Detection End")
main()
Copy the code

The source address

Am I

The effect

After configuring the Config file, the following line graph is output

The original file is also saved in the corresponding Excel file

mobileperf

In fact, Mobileperf follows a similar approach: sample-record-output. More comprehensive and detailed detection items, including CPU, FPS frame rate, memory, logCAT and a series of performance parameters detection, and monkey support, a big step towards automated detection.

Mobileperf address: Click me

Afterword.

The premise of performance optimization is to have enough comprehensive, detailed data to provide analysis, starting from the phenomenon, from the outside to the inside, step by step to the part to be tuned. This article is merely a primer.