当前位置:网站首页>Exploration of mobile application performance tools
Exploration of mobile application performance tools
2022-07-02 17:02:00 【Testerhome official】
This paper is written by Grow naturally Published in TesterHome Forum , Click on Link to the original text You can view more of the author's articles and contact ta Online communication .
The way to explore mobile application performance tools —— Andrews
One 、 Preface
With PerfDog charge , The self-developed performance tool that no one cared about before suddenly became a very important thing . But when someone starts using it , Untested tools expose many problems , These questions made me realize , This tool is not just as simple as getting performance . In the subsequent process of deepening the cultivation of this product , I have accumulated a lot of knowledge , Also got some insights , Next , I will share my experience with you .
Two 、 The road to explore
First time to know adb
At the beginning of Android performance collection , Collect information online , Almost all roads lead to one place ——adb.
however , Not long after the beginning , I met a big pit . In the use of adb shell dumpsys SurfaceFlinger --latency
obtain frame time when , Some mobile phones always send commands twice before they have a return value . But into adb shell Send interactively dumpsys SurfaceFlinger --latency
Command does not have this problem . So the answer is obvious , Direct execution adb shell <command>
On command , Output buffer , When the buffer is full , Will be output together .
To solve this problem , Need to know a little adb Related knowledge . There are many related materials on the Internet , Let me just say .
adb There are three parts :client、server and daemon(adbd), among client and server stay pc End ,daemon On the mobile phone end . Their mode of communication is client<–>server<–>daemon.adb server By default, it will listen to local 5037 port , wait for client The order of , And I just want to realize one by myself client and server signal communication , It can be obtained in time server Return .
The order is first ddmlib agreement , Send again shell:, The code will reflect .
client Packet format :4 Byte packet length + Instructions
notes : The packet length is not in bytes , It is a hexadecimal number with large byte order , So despite the fact that 4 byte , But it is only equivalent to unsigned short. stay python in , Use it directly "{:04x}".format(len(cmd)) that will do .
server Each time a command is received, it will first return a 4 Bytes of OKAY, Then there is the return data , No length , Read until it is empty .
adb client Of python Simple implementation :
client = socket.socket()
client.connect(("127.0.0.1", 5037))
cmd = "host:transport:serial" # here serial Replace with a mobile phone serial, This is a ddmlib A kind of agreement in , Used to perform adb -s <serialNumber> shell <command>
client.send("{:04x}{}".format(len(cmd), cmd).encode("utf-8"))
print(client.recv(4)) # server Returns the b'OKAY'
cmd = "shell:ls"
client.send("{:04x}{}".format(len(cmd), cmd).encode("utf-8"))
print(client.recv(4)) # server Returns the b'OKAY'
content = b""
while True:
chunk = client.read(4096)
if not chunk:
break
content += chunk
print(content)
Of course , stay python in , There must be a third-party library that can be used directly :adbutils
github:https://github.com/openatx/adbutils
initialization
- Equipment monitoring
Access and unplug of monitoring equipment , Update the device list in real time at the same time , It is a necessary function of a performance tool .
To achieve this , Then you have to be with adb Dealing with . This protocol needs to use host:track-devices
, In addition to the above host:transport:serial
, And the last one is host-serial
, Used to map ports .
After sending the protocol , Whenever there is a change in equipment ,server Will send the device list to client.
Although the implementation is relatively simple , But there is no need to build the wheel again , Third party Library :ConnectionTracer
github:https://github.com/williamfzc/connectiontracer
- Application list
After connecting the device , The device list is about to be loaded , But just by pm list package
The only data obtained is the package name .
To get the application name and icon of the application , I passed dumpsys package Package name
Get the app Of apk The position of , then apk pull To local , Finally using aapt Resolve the application name and icon .
There is no doubt that such an approach is not desirable , If there are many games on the mobile phone , Limited by usb Transmission rate , Each initialization takes a long time adb pull On . Then we use the file server to store the application name and icon corresponding to the package name , Besides , Use pm list package -3
Get third party packages , Reduce the number of packets obtained , In this way, you can first query whether the package name has recorded data , If it does not exist, then pull To local parsing , After parsing, upload the package information to the file server , So you don't have to parse it next time .
But there are still several problems :
1、 For mobile phones with uncollected packets, it still takes a long time to initialize ;
2、 The package icon is fixed , If game has updated icon , Will be inconsistent with the record ;
3、 There are some package icons that are not regular jpg or png Format , It is svg Format ;
The final solution is shown below app_process The section will give .
Performance collection
By calling adb shell, You can already get most of the performance data , Refer to Alibaba's for the data acquisition methods mobileperf,github:https://github.com/alibaba/mobileperf
If you want to be like PerfDog equally , I also encountered the following problems :
- How to stop and store data uniformly ?
First, if you want to obtain so many performance indicators at the same time , Linear execution certainly won't work , And it's io Most operations , So I used the multithreading method .
The way to control the thread stop is not to kill the thread roughly , But use python threading Of Event To break a loop in a thread :while not self._stop_event.is_set():
.
The collected data are all placed in the same queue in , You can read this queue To get the data collected by each thread .
- How can we achieve something like PerfDog Same interval 1s A performance data , And ensure that the time of data fetched by different commands is consistent ?
In the original version , In each data fetching thread , Record when the method was executed cost_time
, After getting the data , perform time.sleep(1-cost_time)
, such , The time interval of each thread will not vary too much because of the deviation of the acquisition or calculation speed .
But this can only ensure that the time interval is consistent , There is no guarantee that the collection time of different threads is exactly the same . For this point , The main thing is to reduce the front end / The difficulty of drawing charts on the client side and ensuring the uniformity of chart data .
My method is to add a new thread , Used to control the interval 、 Generate a uniform timestamp . Add a new Event, Controlled by this thread , Other threads need to wait for this Event Data collection can only be carried out after it is ready , Finally, get the ready time provided by the thread as the timestamp of the data .
- How to determine whether the process of the test package is alive ?
Before starting to get data , Find the corresponding package pid, command :/data/local/tmp/busybox ps | grep \"{packageName}\" | grep -v grep | /data/local/tmp/busybox awk '{ {print $1}}'
, If not used busybox,adb shell Medium ps This effect cannot be achieved . If the return value is not obtained at this time , End the collection directly , And inform the front-end application that it is not started .
For later use adb shell ps pid
In the process , If no return value is obtained , Then it is determined that the application has exited , End collection , And inform the front-end application that it has exited .
- Use adb The screenshot function consumes a lot of performance ?
Use minicap A screenshot , It can effectively reduce the performance consumption of screenshots , However, relevant components need to be pushed into the mobile phone during initialization . Please refer to github:https://github.com/openstf/minicap
It is worth noting that , When the mobile phone is horizontal, only half of the picture is cut , Need to change minicap Screenshot parameters to set whether to rotate .
minicap Screenshot command :adb shell LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -P [email protected]/0
-P The following parameter format :{RealWidth}x{RealHeight}@{VirtualWidth}x{VirtualHeight}/{Rotation}
rotation Get orders :adb shell dumpsys input | grep 'SurfaceOrientation' | awk 'NR==1{ print $2 }'
,0 Is not horizontal screen ,1 It's a horizontal screen , Just judge rotion yes 1 be Rotation by 90 that will do .
- Use dumpsys meminfo obtain PSS Will lead to how Caton solves ?
PerfDog In the official documents of , It is clear that the Android memory fetches PSS. Found a lot of information , Use adb The only stable access is dumpsys meminfo package
. But after repeated tests , The fatal drawback of this method is that it will consume a lot of cpu resources , It's causing a jam .
It will lead to Caton , It is a fatal flaw for any performance testing tool , But if you don't take PSS No way , That can only jump out adb, Looking for a new direction .
breakthrough
Turning point is that I found this article :https://testerhome.com/topics/27321 , In the article app_process It has been introduced in great detail , I won't go back to .
But here's the problem , He didn't release the code , What should I do ? Search through the Internet , Found a project :https://github.com/anysou/APP_process , After a lot of trouble , It's also the first one successfully played apk, Took out one of them dex file . thus , Performance tools are also in the next stage .
- perform shell
Use adb shell Need to pass through adb server Interact with mobile phones , If you can directly interact with the mobile phone, it will be faster ?
This function has been implemented in the above project , Use app_process After starting the project , Just use socket Connected to the phone 8888( The port is preset in the project code ), And then send the command , End with a newline character to get the execution result .
The principle is to call Runtime.getRuntime().exec()
It is a pity , perform dumpsys meminfo package
It's still going to get stuck .
- obtain Context
For the use of app_process The process started , I hope it is a background process , No installation operation is required , There is no display on the front desk , But this process is not Activity, So there is no Context. But doing a lot of things requires Context. So how can we trap the white wolf with empty hands ?
Because I android Don't know much , So I know a little about various methods on the Internet . But in the end I worked out an effective way .
import android.app.ActivityThread; // If you write here and find it marked red , Please continue with the following
import android.context.Context;
...
Context sysContext = ActivityThread.systemMain().getSystemContext();
android.app.ActivityThread This class cannot be imported under normal conditions , because ActivityThread Class quilt @hide 了 .
The solution is simple , Refer to this article :https://hardiannicko.medium.com/create-your-own-android-hidden-apis-fa3cca02d345
It's too complicated ? Use the ready-made ones directly :https://drive.google.com/drive/folders/17oMwQ0xBcSGn159mgbqxcXXEcneUmnph
The way to use it is to replace packaging sdk Of android.jar, course github:https://github.com/anggrayudi/android-hidden-api
- PackageManager
PackageManager similar adb Medium pm, Responsible for management pack , Use this class , Be able to obtain all kinds of application information .
PackageManager pm = sysContext.getPackageManager();
for (ApplicationInfo appInfo : pm.getInstalledApplication(0)) {
System.out.println(appInfo.loadLabel(pm).toString()); // Application name
System.out.println(appInfo.packageName); // Package name
Drawable icon = appInfo.loadIcon(pm); // Icon
if(icon == null){
icon = appInfo.loadLogo(pm); // You can try this if you can't get the one above
}
}
This avoids the above use adb Can't get the app name and icon .
- ActivityManager
Used to obtain process memory data , Use it to get PSS It won't cause Caton . Go straight to the code :
Debug.MemoryInfo[] memoryInfo = ((ActivityManager) sysContext.getSystemService(Context.ACTIVITY_SERIVCE)).getProcessMemoryInfo(new int[]{
pid});
if(memoryInfo.length > 0){
System.out.println((float) memoryInfo[0].getTotalPss() / 1024);
}
in addition to , also NetworkManager and BatteryManager etc. , I'm not going to introduce it .
3、 ... and 、 introspection
You can't just study things on the surface , Sometimes a breakthrough can be found by observing all aspects .
Don't specialize in just one language , Try more , Sometimes the breadth of technology is more important than the depth . You never know what language the next support department will use .
As a support department , The most important thing is not how powerful the technology is , Instead, the tools should be able to meet the daily use needs of the test group . meanwhile , Timely only for internal use , We should also polish the details , To give users a better experience .
Willing to share , Sum up from time to time .
At the end
It has been more than a year since graduation , Think back to the time when I was enrolled in the primary school , I don't know why it turns out to be a test , Maybe it's because I could only python Well . Along the way , I found that I was not particularly interested in writing business code , But for writing this tool is particularly positive , Maybe this is the right way . I remember an interview , The interviewer described me as “ There are many wild ways ”, At that time, I scoffed at the idea , But after work , I feel more and more that this description is very appropriate .
I don't know how far I can go on this road , But I still hope that in the rest of the time , Can work hard to create , Instead of following the footsteps of others .
Last, last , It is a pity that this article has no code but dry goods , But this is the property of the company after all , Sorry, we can't open source for the time being , I have also applied to my superiors for open source , If there is any result in the follow-up, it must be advertised .
This paper is written by Grow naturally Published in TesterHome Forum , Click on Link to the original text You can view more of the author's articles and contact ta Online communication .
Want to learn more about testing / Test development technology 、 Dry goods knowledge of test management and quality assurance ?
Want to get to know quality industry leaders and industry elites ?
Welcome to the 10th China Internet testing and Development Conference · ShenZhen Railway Station >>
边栏推荐
- System Verilog implements priority arbiter
- Day 18 of leetcode dynamic planning introduction
- IP address translation address segment
- PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
- LeetCode 1. Sum of two numbers
- LeetCode 3. Longest substring without duplicate characters
- LeetCode 5. Longest Palindromic Substring
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
- MOSFET器件手册关键参数解读
- Usage of sprintf() function in C language
猜你喜欢
linux下配置Mysql授权某个用户远程访问,不受ip限制
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
一文看懂:数据指标体系的4大类型
LeetCode 1. Sum of two numbers
【Leetcode】14. Longest Common Prefix
Serial port controls steering gear rotation
电脑自带软件使图片底色变为透明(抠图白底)
Day 18 of leetcode dynamic planning introduction
PWM控制舵机
易语言abcd排序
随机推荐
P6774 [noi2020] tears in the era (block)
IP地址转换地址段
[fluent] dart data type boolean type (boolean type definition | logical operation)
IP address translation address segment
The macrogenome microbiome knowledge you want is all here (2022.7)
[leetcode] 14. Préfixe public le plus long
一文看懂:数据指标体系的4大类型
Privacy computing technology innovation and industry practice seminar: Learning
How openharmony starts FA of remote devices
你想要的宏基因组-微生物组知识全在这(2022.7)
Yolov5 practice: teach object detection by hand
DigiCert SSL证书支持中文域名申请吗?
Serial port controls steering gear rotation
[error record] error -32000 received from application: there are no running service protocol
LeetCode 4. 寻找两个正序数组的中位数(hard)
国内比较好的OJ平台[通俗易懂]
PWM breathing lamp
uboot的作用和功能
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
DGraph: 大规模动态图数据集