Play with PerfDogService Demo analysis from scratch

1.Demo folder

After downloading PerfDogService, the folder looks like this.

Document description:

PerfDogService.exe: Service startup file. Generally, it does not need to be started manually. The automatic script will automatically start this program.
perfdog. Proto: definition file of grpc interface and protobuf structure
Demo / Python: the demo of Python 3 depends on the libraries grpcio and protobuf
Demo / Java: the demo of Java, which depends on the libraries grpc netty shaded, grpc protobuf and grpc stub
PerfDogService.jar: the jar package that the service depends on

Let's just focus on the demo folder regardless of others;
After the expansion is the Demo of Java and Python. This time, we only analyze and transform Python

Python's Demo has only three files

Demo.py: the execution entry of our program. At the same time, we mainly operate on this file;
perfdog_pb2.py: this file is generated by the protocol buffer compiler, that is, by perfdog Proto is generated from this file. Let's not modify it.
perfdog_pb2_grpc.py: this file is generated by the gRPC Python protocol compiler plug-in. Do not modify it
Here is a brief introduction to the = = protocol buffer==
Protocol buffer is a language independent and platform independent data serialization structured mechanism;
You only need to construct the data once, and then you can use the generated special source code to easily write and read structured data in various data streams in various languages;

You use python language. You have worked hard to define a pile of dictionaries or classes or other data in python. You want to use it for Lao Li next door. However, Lao Li uses java. At this time, Xiao Wang next door also needs to use it. However, Xiao Wang uses C + +, and various languages cannot be called directly. At this time, through the mechanism of protocol buffer, first compile your data to generate a special source code, and Lao Li and Xiao Wang can call this source code.

2.Demo.py file parsing

In fact, there is only one Run function in the whole article, and the official comments are relatively clear. Let's have a simple look

There's nothing to say about 0 to 3. It's to arouse PerfDogService service and establish a connection with the server through token. stub has many other methods besides establishing a connection

Let's look at step 4

deviceEventIterator Here is an iteratable object that stores all current connected devices, such as Android la Ios,Simulator, wait; 

The most commonly used device should be device, where the uid and name of the tested model are saved. The uid here is the unique identification of the currently connected device. If you have installed ADB, you can enter ADB devices on the console to view the information of the currently connected device. For example:

5 is actually starting to initialize the device
6. Start to obtain all installed applications of the currently tested equipment. The applications are saved in the app object, which is also an iterative object,
Iterative apps can obtain each app, as well as the name, package name, version, Icon, whether it is a system app, subVersion, etc.

Step 7 here is actually to obtain the hardware information of the current tested device, such as CPU, model, Android version and so on

Step 8: select the performance data indicators to be collected here. I have added an additional CPU Core utilization
Step 9 starts collecting performance data

Collecting performance data information is actually carried out through multithreading, so Sleep(20) collects 20s data

10 and 11 are notes added for different time periods

Step 12 is two operations
1. Upload the whole data to the cloud
2. Export the data from the 5th to 20th seconds to the local, where the export path can be changed by itself (outputDirectory)

13 finally stop the test, but the process did not stop. If you start the perflog client at this time, this error may be displayed.

At this time, just go to Kill and drop this java into the city.

3.Demo simple modification

As my personal requirement 1 is that I don't want to modify the code every time I configure, and 2 is to pave the way for the subsequent establishment of a custom cloud performance test platform, I simply modified the following code: the source code is not deleted, but only commented out for comparison;
1. Extract common configuration
2. The corresponding equipment to be tested shall be configured in advance
3. The app to be tested needs to be configured in advance (the name of the configured APP)
4. Upload only one copy of data to the cloud,
5. The data saved locally will not be intercepted from the middle for a certain period of time.

# -*- coding: utf-8 -*-
import subprocess
import time
import traceback

import grpc
import perfdog_pb2_grpc
import perfdog_pb2
import threading
class Config():
    PERFDOGSERVER_PATH=r'C:UsersAdministratorDesktopperfdogPerfDogService.exe' #Path to PerfDogService
    TOKEN='XXXXXXXXXXXXXXXXXXXX '#token
    DEVICES_UID="813QEDTE228ZK" #Mobile phone serial number to be tested
    APP_NAME="weather" #app name to be tested
    TEST_TIMER=11 #Test duration required
    LABEL="this is a label" #label content
    NOTE="this is a note" #note content
    CASENAME="Weather test data" #The use case description will overwrite the data with the same use case name
    SAVEDATA_BEGINTIME=1 #Start time of export to local data (seconds)
    SAVEDATA_ENDTIME=20 #End time of export to local data (seconds)
    OUTPUT="F:perfdog_service_output" #The directory where the export file is saved,
    SAVEDATA_TYPE=perfdog_pb2.EXPORT_TO_JSON #Export the type of file saved,

# Before running the demo for the first time, you need to install grpcio(1.23.0) and protobuf(3.10.0) through pip
def run():
    try:
        # Start PerfDogService in code or manually
        print("0.start-up PerfDogService")
        # 1. ************************************** fill in the path of PerfDogService
        perfDogService = subprocess.Popen(Config.PERFDOGSERVER_PATH)
        # Wait for PerfDogService to start
        time.sleep(5)
        print("1.adopt ip And ports connected to PerfDog Service")
        options = [('grpc.max_receive_message_length', 100 * 1024 * 1024)]
        channel = grpc.insecure_channel('127.0.0.1:23456', options=options)
        print("2.Create a new one stub,Through this stub Object can call the interfaces provided by all servers")
        stub = perfdog_pb2_grpc.PerfDogServiceStub(channel)
        print("3.Through token login, the token can be applied on the official website")
        userInfo = stub.loginWithToken(perfdog_pb2.Token(token=Config.TOKEN))
        print("UserInfo:n", userInfo)
        print("4.Start the device listener to monitor the device,Each time a device is inserted and removed, a message is received DeviceEvent")
        deviceEventIterator = stub.startDeviceMonitor(perfdog_pb2.Empty())
        for deviceEvent in deviceEventIterator:
            # Get the device object from DeviceEvent, and the device object will be used in the later interface
            device = deviceEvent.device

            print("current devices:  ",device," **** ",deviceEvent)
            # time.sleep(20000)
            if deviceEvent.eventType == perfdog_pb2.ADD and device.uid==Config.DEVICES_UID:
                print("equipment[%s:%s]insert n" % (device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(device.conType)))
                # Each mobile phone will return two device objects (USB and WIFI) with different conType. If it is wired, take the USB object
                if device.conType == perfdog_pb2.USB:
                    print("5.Initialize device[%s:%s]n" % (device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(device.conType)))
                    stub.initDevice(device)
                    print("6.obtain app list")
                    appList = stub.getAppList(device)
                    #
                    apps = appList.app
                    app_index = 0
                    for app in apps:
                        print('%s: %s->%s' % (app_index, app.label, app.packageName))
                        if app.label==Config.APP_NAME:
                            app_select=app_index
                            break
                        else:
                            app_index += 1
                            app_select=None
                    if app_select is None:app_select = int(input("Input not installed APP,Please select to test App: "))

                    app = apps[app_select]

                    print("7.Get device details")
                    deviceInfo = stub.getDeviceInfo(device)
                    print("8.Open performance data item")
                    stub.enablePerfDataType(
                        perfdog_pb2.EnablePerfDataTypeReq(device=device, type=perfdog_pb2.NETWORK_USAGE))
                    stub.enablePerfDataType(
                        perfdog_pb2.EnablePerfDataTypeReq(device=device, type=perfdog_pb2.NORMALIZED_CPU_CORE_USAGE))



                    print("9.Start collection[%s:%s]Performance data for n" % (app.label, app.packageName))
                    print(stub.startTestApp(perfdog_pb2.StartTestAppReq(device=device, app=app)))

                    req = perfdog_pb2.OpenPerfDataStreamReq(device=device)
                    perfDataIterator = stub.openPerfDataStream(req)

                    def perf_data_process():
                        for perfData in perfDataIterator:
                            print(perfData)

                    threading.Thread(target=perf_data_process).start()
                    # Collect some data
                    time.sleep(Config.TEST_TIMER)
                    print("10.set up label")
                    stub.setLabel(perfdog_pb2.SetLabelReq(device=device, label="I am a label"))
                    time.sleep(3)
                    print("11.Add comments")
                    stub.addNote(perfdog_pb2.AddNoteReq(device=device, time=5000, note="I am a note"))
                    print("12.Upload and export all data")
                    saveResult = stub.saveData(perfdog_pb2.SaveDataReq(
                        device=device,
                        caseName=Config.CASENAME,  # The names of case and excel on the web
                        uploadToServer=True,  # Upload to perfdog server
                        exportToFile=True,  # Save to local
                        outputDirectory=Config.OUTPUT,
                        dataExportFormat=Config.SAVEDATA_TYPE
                    ))

                    print("Save results:n", saveResult)
                    # print("12. Upload and export data from the 5th to 20th seconds")
                    # stub.saveData(perfdog_pb2.SaveDataReq(
                    #     device=device,
                    #     beginTime=5000,  # Specify start time
                    #     endTime=20000,  # Specify end time
                    #     caseName="case2",  # The names of case and excel on the web
                    #     uploadToServer=True,  # Upload to perfdog server
                    #     exportToFile=True,  # Save to local
                    #     outputDirectory="F:perfdog_service_output",
                    #     dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL
                    # ))
                    print("13.Stop testing")
                    stub.stopTest(perfdog_pb2.StopTestReq(device=device))
                    print("over")
                    break
                elif deviceEvent.eventType == perfdog_pb2.REMOVE:
                    print("equipment[%s:%s]remove n" % (device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(device.conType)))
    except Exception as e:
                traceback.print_exc()

if __name__ == '__main__':
    run()
#

Performance test technology exchange group: 720150565

View PerfDog details: https://perfdog.qq.com/?ADTAG...

Tags: AI

Posted by mtb211 on Sun, 08 May 2022 01:57:51 +0300