1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import pytest 17import sys 18import subprocess 19import threading 20import time 21sys.path.append("..") 22from tools.utils import touch_button 23 24 25SLEEP_TWO = 2 26SLEEP_THREE = 3 27SLEEP_FIVE = 5 28MULTIPLE_RESULT = 3 29ONCE_RESULT = 2 30CONFIG_INDEX = 10 31THRESH = 25000000000 32 33 34def task(index): 35 indexstr = str(index) 36 subprocess.check_output("hdc shell hiprofiler_cmd -c /data/local/tmp/config10.txt -o /data/local/tmp/test" + indexstr + ".htrace -t 30 -s -k") 37 38 39class TestHiprofilerNetworkProfiler: 40 @pytest.mark.L0 41 def test_network_profiler_multiple_times(self): 42 subprocess.check_output(r"hdc file send .\..\inputfiles\network_profiler\config10.txt /data/local/tmp/", text=True, encoding="utf-8") 43 subprocess.check_output("hdc shell killall com.example.myapplication523") 44 task_thread = threading.Thread(target=task, args=(CONFIG_INDEX, )) 45 time.sleep(SLEEP_TWO) 46 task_thread.start() 47 time.sleep(SLEEP_THREE) 48 subprocess.check_output("hdc shell aa start -a EntryAbility -b com.example.myapplication523") 49 time.sleep(SLEEP_FIVE) 50 touch_button("http_request") 51 time.sleep(SLEEP_TWO) 52 touch_button("http_request") 53 time.sleep(SLEEP_TWO) 54 touch_button("http_request") 55 task_thread.join() 56 57 subprocess.check_output("hdc shell chmod 777 /data/local/tmp/hookDecoder") 58 subprocess.check_output("hdc shell ./data/local/tmp/hookDecoder -f /data/local/tmp/test10.htrace > /data/local/tmp/test10_result.txt") 59 subprocess.check_output(f"hdc file recv /data/local/tmp/test10.htrace .\..\outputfiles\ ", text=True, encoding="utf-8") 60 subprocess.check_output(f"hdc file recv /data/local/tmp/test10_result.txt .\..\outputfiles\ ", text=True, encoding="utf-8") 61 count = 0 62 with open(r'.\..\outputfiles\test10_result.txt', 'r') as file: 63 lines = file.readlines() 64 for line in lines: 65 if "tv_nsec" in line: 66 count += 1 67 assert count == MULTIPLE_RESULT 68 # 第二次请求和第三次请求一起被写入trace文件 69 70 @pytest.mark.L0 71 def test_network_profiler_one_time(self): 72 subprocess.check_output("hdc shell rm /data/local/tmp/test10.htrace") 73 subprocess.check_output("hdc shell rm /data/local/tmp/test10_result.txt") 74 subprocess.check_output("hdc shell killall com.example.myapplication523") 75 subprocess.check_output(r"hdc file send .\..\inputfiles\network_profiler\config10.txt /data/local/tmp/", text=True, encoding="utf-8") 76 subprocess.check_output("hdc shell killall com.example.myapplication523") 77 task_thread = threading.Thread(target=task, args=(CONFIG_INDEX, )) 78 time.sleep(SLEEP_TWO) 79 task_thread.start() 80 time.sleep(SLEEP_THREE) 81 subprocess.check_output("hdc shell aa start -a EntryAbility -b com.example.myapplication523") 82 time.sleep(SLEEP_FIVE) 83 touch_button("http_request") 84 time.sleep(SLEEP_TWO) 85 task_thread.join() 86 87 subprocess.check_output("hdc shell chmod 777 /data/local/tmp/hookDecoder") 88 subprocess.check_output("hdc shell ./data/local/tmp/hookDecoder -f /data/local/tmp/test10.htrace > /data/local/tmp/test10_result.txt") 89 subprocess.check_output(f"hdc file recv /data/local/tmp/test10.htrace .\..\outputfiles\ ", text=True, encoding="utf-8") 90 subprocess.check_output(f"hdc file recv /data/local/tmp/test10_result.txt .\..\outputfiles\ ", text=True, encoding="utf-8") 91 count = 0 92 with open(r'.\..\outputfiles\test10_result.txt', 'r') as file: 93 lines = file.readlines() 94 for line in lines: 95 if "tv_nsec" in line: 96 count += 1 97 assert count == ONCE_RESULT 98 # 包括文件头