The author | liu chang

Almond test engineer, focus on mobile testing and automated testing.

When we use various apps, we basically pay attention to: is this software a traffic hog? It’s running a little fast, isn’t it? Will there be a lag when switching pages? If we encounter an App that has these problems, we will basically remove it from our computer. Thus, in addition to providing the necessary features, fluency and flow/battery consumption are also important indicators of software popularity.

Today, from the perspective of our testers, we will talk about some indicators that need to be paid attention to in the process of App acceptance testing:

  • Memory footprint

  • CPU

  • Traffic cost

  • Power consumption

  • The startup time

Recently, I was reading the book “Mobile App Performance Evaluation and Optimization” published by Tencent TMQ team. I found some terms or concepts were not very clear, so I had to query some other points while reading the book. Now I will record these points. It is also a reading notes, for each of the following important knowledge points were sorted out.

A memory.

1. Memory leaks

Speaking of memory, the most classic memory problem is memory leaks. Baidu defines a Memory Leak as follows: A Memory Leak refers to a waste of system Memory caused by a program’s dynamically allocated heap Memory that is not released or cannot be released due to some reason, resulting in a slowing down of the program or even a system crash. In layman’s terms, in most applications, there is a type of functionality that requires the loading of additional resources, such as displaying text or images downloaded from the Web. These functions usually store resource objects to be used in memory. After you exit the function, you need to clear these resource objects. If you forget to clean up, or if the clean up is invalid for code reasons, a memory leak can occur.

2. Recycling

Garbage Collector refers to the Garbage in memory that has become invalid but cannot be released automatically and will never be returned to the operating system unless the system is restarted. As a result, a lot of garbage will be generated when the program runs for a long time. On the one hand, a lot of memory space will be wasted. On the other hand, if the same memory address is deleted twice, the program will be unstable and even crash.

During the execution of a Java program, a garbage collector is invoked periodically to check for objects that are no longer being used and to free up memory space occupied by them. However, the garbage collector’s collection is random. It may not be started once or many times during the running of the program. It does not automatically collect garbage because the program generates garbage immediately. So garbage collection does not completely prevent memory leaks.

On the other hand, garbage collection will also bring additional burden and space time overhead to system resources. The less likely it is to be activated, the less likely it is to be a burden.

3. Memory specifications

Memory indicators include VSS, RSS, PSS, and USS. Their meanings are:

VSS: Virtual Set Size Virtual memory (including shared library memory)

RSS: Resident Set Size Actual use of physical memory (including memory occupied by shared libraries)

PSS: Proportional Set Size Actual physical memory (Proportional allocation of shared library memory)

USS: Unique Set Size Physical memory occupied by the process alone (excluding memory occupied by the shared library)

Generally speaking, the size of memory usage has the following rules: VSS >= RSS >= PSS >= USS, and PSS is the index that is paid more attention to in the general test.

4. Monitoring and analysis tools

The following are some common memory analysis tools that are not detailed here.

4.1 the Memory Monitor

Android Monitor is a powerful performance analysis tool provided by Android Studio. It contains five modules: Logcat, Memory, CPU, Network, and GPU.

The Memory Monitor can view the Memory allocation of an App in real time, determine whether the App is stuck due to GC operations, and determine whether the Crash of the App is due to Memory overflow.

4.2 Heap Viewer

The Heap Viewer can view the size of the memory allocated by the App and the size of free memory in real time. And find Memory Leaks.

4.3 MAT

The MAT (Memory Analyzer Tool) is a well-worn Android Memory Analyzer Tool that can clearly understand the overall Memory usage. Although an Eclipse tool, you can also run it on its own without having to install Eclipse.

2. The CPU

1. Time slice

Time slice is the CPU allocated to each program time, each thread is allocated a time slice, called its time slice, that is, the process is allowed to run time, so that each program from the surface is simultaneous.

2. Jiffies

2.1 Concept of Jiffies

To talk about Jiffies, you need to mention two concepts: HZ and Tick

HZ: The Linux core emits timer interrupts (IRQ 0) every fixed period. The HZ is used to define the number of timer interrupts per second. For example, a HZ of 1000 means 1000 timer Interrupts per second.

Tick: indicates the reciprocal of HZ. Tick = 1/HZ. For example, if the HZ is 250, the tick is millisecond.

As a core Linux variable, Jiffies is an unsigned long variable that is used to record how many ticks the system has passed since it was started. Each time a Timer interrupt occurs, the Jiffies variable is incremented by one.

2.2 Viewing Jiffies methods

In Linux, run the cat /proc/stat command to view Jiffies of a specific machine, as shown in the figure:

In Linux, run the cat /proc/< process ID >/stat command to view Jiffies of a process:

3. CPU usage

In Linux, the CPU usage is classified into user state, system state, and idle state. The user state indicates the CPU execution time in user state, system state indicates the system kernel execution time, and idle state indicates the idle system process execution time.

The CPU usage of an App = CPU execution time of non-system idle processes/total CPU execution time, which can also be expressed as App user state Jiffies + App system state Jiffies/total mobile Jiffies.

4. The CPU is too high

The whole phone may be unresponsive and the overall performance may be reduced, leading to ANR, resulting in more power consumption and reduced user experience.

Three.

Definition 1.

Our mobile phones access the Internet through the carrier’s network. The carrier forwards data packets for our mobile phones. The total size (bytes) of data packets is the traffic.

2. Common flow testing methods

2.1 Packet capture test method

You can use Tcpdump to capture packets, export pCAP files, and open them in Wireshark for analysis.

2.2 Statistical test method

2.2.1 Reading The Linux Traffic Statistics File

The tcp_SND and tcp_RCV values of the App are obtained by using the statistics function of TCP sending and receiving length provided by Android itself. After testing for a period of time, the values are counted respectively and tcp_snd is used

The difference between the two is worth sending traffic, with tcp_RCV the difference between the two is worth receiving traffic.

2.2.2 Use the Android traffic statistics API
  • TrafficStats

Android version 2.2 adds the android.net.TrafficStats class to handle traffic statistics.

Part of the method is as follows:

Static Long getMobileRxBytes() // The total number of bytes received over the mobile data network static Long getMobileTxBytes() // The total number of bytes sent over the mobile data network static Long GetTotalRxBytes () static Long getTotalTxBytes() static Long getUidRxBytes(int uid) Static long getUidTxBytes(int uid) static long getUidTxBytes(int uid) static long getUidTxBytes(int uid) static long getUidTxBytes(int uidCopy the code
  • NetworkStatsManager

In Android version 6.0, the NetworkStatsManager class was introduced to break down TrafficStats, which allows you to obtain more accurate network history data, rather than data since the device was restarted. Part of the method is as follows:

NetworkStats.Bucket querySummaryForDevice(int networkType, String subscriberId, long startTime, NetworkStats queryDetailsForUid(int networkType, String subscriberId, Long startTime, long endTime, int uid) // query the traffic statistics of a uid within the specified networkType and interval. NetworkStats queryDetails(int networkType, String subscriberId, Long startTime, long endTime) // Query the detailed traffic statistics (including each UID) for a specified network type within a specified interval.Copy the code

4. Power

1. Power consumption scenario

  • Location, especially by calling GPS location.

  • Network transmission, especially in non-wifi environments.

  • The screen brightness

  • CPU frequency

  • Memory scheduling frequency

  • Wake_locker times and times

  • Other sensors

2. Test method

2.1 Obtaining electric quantity Records from system files

Use adb shell dumpsys batterystats> batterystats. TXT to print detailed power consumption information and save the statistics to batterystats. TXT.

2.2 Import BatteryStats. TXT to Google open-source tool Battery Historian for analysis

Because this tool is developed by Go language, you need to pre-install the Go language development environment. Of course, if you do not want to configure the Go language environment, the official also provides a more convenient solution, by installing the Docker environment to use this tool. I will write a separate article to record the configuration and installation of this tool, the specific use method and the representative meaning of parameters, and put a screenshot of the operation of this tool first.

3. Optimization method

3.1 CPU time slice

When the application is running in the background, the active running of the application should be minimized. When abnormal CPU time slice consumption is detected, the analysis is conducted in the thread.

3.2 wake lock

Do not sign up for Wake Lock while the foreground is running.

When running in the background, registration of Wake Lock should be minimized under the premise of ensuring business requirements.

Reduce the wake frequency of the system by using partial wake Lock instead of Wake Lock.

3.3 the sensor

Set the GPS usage duration and frequency reasonably.

3.4 Cloud power saving strategy

Considering the diversity of user usage scenarios, it is difficult to locate the root cause of abnormal power consumption of users. Therefore, in order to further clarify these problems, it is possible to periodically report grayscale users’ mobile phone power data to analyze the problem.

V. Start time

Adb shell am start -w packagename/activity: adb shell am start -w packagename/activity

Note:

WaitTime: Total time, including the previous application Activity Pause time and the new application start time

ThisTime: How long it takes for the last Activity in a sequence to start an Activity

TotalTime: indicates the time taken to start a new application, including the start of a new process and the start of an Activity, but excluding the time taken by the previous application Activity Pause

6. Summary

App performance problems will directly affect product experience: traffic consumption, fast power loss, lag, crash and other phenomena will give users a bad experience and impression, which is not conducive to product activity and user retention. Many experienced programmers often overlook these minor performance issues, which makes it especially important as a tester to pay attention to and discover these performance issues well before a release. However, it was not easy to do App performance testing until then. It is easy to capture the indicators of memory and CPU, but the sensitivity and processing methods of some data need to be gradually accumulated by experience. In short, performance testing is a rather tedious work, but it is not easy for sufferers, easy for sufferers. I hope that those who have already walked on this road or are preparing to set foot on this road can constantly improve their quality and stick to it.

reference

  • Memory leaks in Java and garbage collection mechanism (https://www.cnblogs.com/tv151579/archive/2014/04/05/3647447.html)

  • Android performance test, the CPU time slice (https://www.cnblogs.com/by-dream/p/5143192.html)

  • Explaining App performance triangle (https://www.2cto.com/kf/201607/529622.html)

  • Android Batterystats of special performance test (http://blog.csdn.net/itfootball/article/details/49004699)

The full text after


You may also be interested in the following articles:

  • Four-dimensional Reading: My Secret Technique for Efficient Study

  • The skills necessary for an engineer to grow

  • IOS screen adaptation

  • Principle and implementation of data interaction between Web and App

  • Apple’s three kits in health care

  • Talk about the mobile cross-platform database Realm

  • Processing of complex business state: from state mode to FSM

  • How to become a Data analyst: A First look at data

  • React to Preact Migration Guide

  • Brief introduction to back-end cache system

  • Syntax sugar in Objective-C

  • The principle and implementation of Android picture editing — Graffiti and Mosaic

Almond technology station

Long press the left QR code to pay attention to us, here is a group of passionate young people looking forward to meeting with you.