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 os 17import pytest 18import sqlite3 19import subprocess 20import sys 21import threading 22sys.path.append("..") 23from tools.utils import SMALL_TRACE_EXPECTED_SIZE, SMALL_TRACE_EXPECTED_SIZE_2, MID_TRACE_EXPECTED_SIZE, MID_TRACE_EXPECTED_SIZE_2 24 25 26def task(): 27 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_process.txt -o /data/local/tmp/test_process.htrace -t 10 -s -k"') 28 29 30def task2(): 31 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_process2.txt -o /data/local/tmp/test_process2.htrace -t 10 -s -k"') 32 33 34def task3(): 35 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_process3.txt -o /data/local/tmp/test_process3.htrace -t 10 -s -k"') 36 37 38def task4(): 39 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_process4.txt -o /data/local/tmp/test_process4.htrace -t 10 -s -k"') 40 41 42def task5(): 43 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_process5.txt -o /data/local/tmp/test_process5.htrace -t 10 -s -k"') 44 45 46def task6(): 47 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_process6.txt -o /data/local/tmp/test_process6.htrace -t 10 -s -k"') 48 49 50class TestHiprofilerProcessPlugin: 51 52 # 所有配置都为true,检查trace数据正确性 53 @pytest.mark.L0 54 def test_process_plugin(self): 55 subprocess.run(r'hdc file send ..\inputfiles\process_plugin\config_process.txt /data/local/tmp', 56 text=True, encoding="utf-8") 57 task_thread = threading.Thread(target=task, args=()) 58 task_thread.start() 59 task_thread.join() 60 subprocess.run(r'hdc file recv /data/local/tmp/test_process.htrace ..\outputfiles', 61 text=True, encoding="utf-8") 62 63 subprocess.run( 64 r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_process.htrace -e ..\outputfiles\test_process.db') 65 66 # 连接数据库文件 67 conn = sqlite3.connect(r'..\outputfiles\test_process.db') 68 69 # # 创建游标对象 70 cursor = conn.cursor() 71 72 # # 执行SQL查询 73 check = False 74 db_relative_path = "..\\outputfiles\\test_process.htrace" 75 absolute_path = os.path.abspath(db_relative_path) 76 db_size = os.path.getsize(absolute_path) 77 # # 判断数据库大小 78 assert db_size > MID_TRACE_EXPECTED_SIZE 79 table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')] 80 cursor.execute('SELECT * FROM live_process order by ts limit 0,10') 81 result = cursor.fetchall() 82 if len(result): 83 check = True 84 assert check 85 86 # # 判断数据库中 process_id cpu_usage pss_info disk_reads 数据 87 for row in result: 88 # process_id 89 assert (row[3] > 0) 90 # cpu_usage 91 assert (row[8] > 0) 92 # pss_info 93 assert (row[9] > 0) 94 # disk_reads 95 assert (row[12] >= 0) 96 97 # 全部参数设置为false检查trace大小及数据 98 @pytest.mark.L0 99 def test_process_plugin2(self): 100 subprocess.run(r'hdc file send ..\inputfiles\process_plugin\config_process2.txt /data/local/tmp', 101 text=True, encoding="utf-8") 102 task_thread = threading.Thread(target=task2, args=()) 103 task_thread.start() 104 task_thread.join() 105 subprocess.run(r'hdc file recv /data/local/tmp/test_process2.htrace ..\outputfiles', 106 text=True, encoding="utf-8") 107 108 subprocess.run( 109 r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_process2.htrace -e ..\outputfiles\test_process2.db') 110 111 # 连接数据库文件 112 conn = sqlite3.connect(r'..\outputfiles\test_process2.db') 113 114 # # 创建游标对象 115 cursor = conn.cursor() 116 117 # # 执行SQL查询 118 check = False 119 db_relative_path = "..\\outputfiles\\test_process2.htrace" 120 absolute_path = os.path.abspath(db_relative_path) 121 db_size = os.path.getsize(absolute_path) 122 # # 判断数据库大小 123 assert db_size > SMALL_TRACE_EXPECTED_SIZE 124 assert db_size < SMALL_TRACE_EXPECTED_SIZE_2 125 table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')] 126 cursor.execute('SELECT * FROM live_process') 127 result = cursor.fetchall() 128 if len(result) == 0: 129 check = True 130 assert check 131 132 # 未设置report_process_tree参数检查trace大小及数据 133 @pytest.mark.L0 134 def test_process_plugin3(self): 135 subprocess.run(r'hdc file send ..\inputfiles\process_plugin\config_process3.txt /data/local/tmp', 136 text=True, encoding="utf-8") 137 task_thread = threading.Thread(target=task3, args=()) 138 task_thread.start() 139 task_thread.join() 140 subprocess.run(r'hdc file recv /data/local/tmp/test_process3.htrace ..\outputfiles', 141 text=True, encoding="utf-8") 142 subprocess.run( 143 r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_process3.htrace -e ..\outputfiles\test_process3.db') 144 145 # 连接数据库文件 146 conn = sqlite3.connect(r'..\outputfiles\test_process3.db') 147 148 # # 创建游标对象 149 cursor = conn.cursor() 150 151 # # 执行SQL查询 152 check = False 153 db_relative_path = "..\\outputfiles\\test_process3.htrace" 154 absolute_path = os.path.abspath(db_relative_path) 155 db_size = os.path.getsize(absolute_path) 156 # # 判断数据库大小 157 assert db_size > SMALL_TRACE_EXPECTED_SIZE 158 assert db_size < MID_TRACE_EXPECTED_SIZE_2 159 table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')] 160 cursor.execute('SELECT * FROM live_process order by ts limit 0,10') 161 result = cursor.fetchall() 162 if len(result) == 0: 163 check = True 164 assert check 165 166 # 未设置report_diskio参数检查trace大小及数据 167 @pytest.mark.L0 168 def test_process_plugin4(self): 169 subprocess.run(r'hdc file send ..\inputfiles\process_plugin\config_process4.txt /data/local/tmp', 170 text=True, encoding="utf-8") 171 task_thread = threading.Thread(target=task4, args=()) 172 task_thread.start() 173 task_thread.join() 174 subprocess.run(r'hdc file recv /data/local/tmp/test_process4.htrace ..\outputfiles', 175 text=True, encoding="utf-8") 176 subprocess.run( 177 r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_process4.htrace -e ..\outputfiles\test_process4.db') 178 179 # 连接数据库文件 180 conn = sqlite3.connect(r'..\outputfiles\test_process4.db') 181 182 # # 创建游标对象 183 cursor = conn.cursor() 184 185 # # 执行SQL查询 186 check = False 187 db_relative_path = "..\\outputfiles\\test_process4.htrace" 188 absolute_path = os.path.abspath(db_relative_path) 189 db_size = os.path.getsize(absolute_path) 190 # # 判断数据库大小 191 assert db_size > SMALL_TRACE_EXPECTED_SIZE 192 assert db_size < MID_TRACE_EXPECTED_SIZE_2 193 table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')] 194 cursor.execute('SELECT * FROM live_process order by ts limit 0,10') 195 result = cursor.fetchall() 196 if len(result): 197 check = True 198 assert check 199 # # 判断数据库中 process_id cpu_usage pss_info disk_reads 数据 200 for row in result: 201 # process_id 202 assert (row[3] > 0) 203 # cpu_usage 204 assert (row[8] > 0) 205 # pss_info 206 assert (row[9] > 0) 207 # disk_reads 208 assert (row[12] >= 0) 209 210 # 未设置report_pss参数检查trace大小及数据 211 @pytest.mark.L0 212 def test_process_plugin5(self): 213 subprocess.run(r'hdc file send ..\inputfiles\process_plugin\config_process5.txt /data/local/tmp', 214 text=True, encoding="utf-8") 215 task_thread = threading.Thread(target=task5, args=()) 216 task_thread.start() 217 task_thread.join() 218 subprocess.run(r'hdc file recv /data/local/tmp/test_process5.htrace ..\outputfiles', 219 text=True, encoding="utf-8") 220 subprocess.run( 221 r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_process5.htrace -e ..\outputfiles\test_process5.db') 222 223 # 连接数据库文件 224 conn = sqlite3.connect(r'..\outputfiles\test_process5.db') 225 226 # # 创建游标对象 227 cursor = conn.cursor() 228 229 # # 执行SQL查询 230 check = False 231 db_relative_path = "..\\outputfiles\\test_process5.htrace" 232 absolute_path = os.path.abspath(db_relative_path) 233 db_size = os.path.getsize(absolute_path) 234 # # 判断数据库大小 235 assert db_size > SMALL_TRACE_EXPECTED_SIZE 236 assert db_size < MID_TRACE_EXPECTED_SIZE_2 237 table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')] 238 cursor.execute('SELECT * FROM live_process order by ts limit 0,10') 239 result = cursor.fetchall() 240 if len(result): 241 check = True 242 assert check 243 # # 判断数据库中 process_id cpu_usage pss_info disk_reads数据 244 for row in result: 245 # process_id 246 assert (row[3] > 0) 247 # cpu_usage 248 assert (row[8] > 0) 249 # pss_info 250 assert (row[9] == 0) 251 # disk_reads 252 assert (row[12] >= 0) 253 254 # 未设置report_cpu参数检查trace大小及数据 255 @pytest.mark.L0 256 def test_process_plugin6(self): 257 subprocess.run(r'hdc file send ..\inputfiles\process_plugin\config_process6.txt /data/local/tmp', 258 text=True, encoding="utf-8") 259 task_thread = threading.Thread(target=task6, args=()) 260 task_thread.start() 261 task_thread.join() 262 subprocess.run(r'hdc file recv /data/local/tmp/test_process6.htrace ..\outputfiles', 263 text=True, encoding="utf-8") 264 subprocess.run( 265 r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_process6.htrace -e ..\outputfiles\test_process6.db') 266 267 # 连接数据库文件 268 conn = sqlite3.connect(r'..\outputfiles\test_process6.db') 269 270 # # 创建游标对象 271 cursor = conn.cursor() 272 273 # # 执行SQL查询 274 check = False 275 db_relative_path = "..\\outputfiles\\test_process6.htrace" 276 absolute_path = os.path.abspath(db_relative_path) 277 db_size = os.path.getsize(absolute_path) 278 # # 判断数据库大小 279 assert db_size > SMALL_TRACE_EXPECTED_SIZE 280 assert db_size < MID_TRACE_EXPECTED_SIZE_2 281 table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')] 282 cursor.execute('SELECT * FROM live_process order by ts limit 0,10') 283 result = cursor.fetchall() 284 if len(result): 285 check = True 286 assert check 287 # # 判断数据库中 process_id cpu_usage pss_info disk_reads 数据 288 for row in result: 289 # process_id 290 assert (row[3] > 0) 291 # cpu_usage 292 assert (row[8] == 0) 293 # pss_info 294 assert (row[9] > 0) 295 # disk_reads 296 assert (row[12] >= 0)