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 subprocess 18import re 19import time 20import os 21import threading 22import sqlite3 23 24 25def get_file_size(file_path): 26 size = os.path.getsize(file_path) 27 return size 28 29 30def task(): 31 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/inputfiles/xpower_plugin/config_xpower.txt -o /data/local/tmp/test_xpower.htrace -t 30 -s -k"') 32 33 34def task_total(): 35 subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/inputfiles/xpower_plugin/config_xpower_total.txt -o /data/local/tmp/test_xpower_total.htrace -t 35 -s -k"') 36 37 38class TestHiprofilerXPowerPlugin: 39 @pytest.mark.L0 40 def test_xpowerplugin_app(self): 41 subprocess.check_output(f"hdc file send ./inputfiles/xpower_plugin/config_xpower.txt /data/local/tmp/", shell=False, 42 text=True, encoding="utf-8") 43 subprocess.check_output(f"hdc shell aa start -a com.huawei.hmos.settings.MainAbility -b com.huawei.hmos.settings", shell=False, 44 text=True, encoding="utf-8") 45 task_thread = threading.Thread(target=task, args=()) 46 task_thread.start() 47 task_thread.join() 48 subprocess.check_output(f"hdc file recv /data/local/tmp/test_xpower.htrace ./outputfiles/ ", shell=False, 49 text=True, encoding="utf-8") 50 # 检查文件大小 51 file_size = get_file_size(f"./outputfiles/test_xpower.htrace") 52 assert (file_size > 1024) 53 subprocess.check_output( 54 r"./inputfiles/trace_streamer_db.exe ./outputfiles/test_xpower.htrace -e ./outputfiles/test_xpower.db") 55 # 连接数据库文件 56 conn = sqlite3.connect(r'./outputfiles/test_xpower.db') 57 # 创建游标对象 58 cursor = conn.cursor() 59 # 执行SQL查询 60 cursor.execute("select end_ts - start_ts as time from trace_range") 61 result = cursor.fetchall() 62 #断言trace 时长27秒 63 for row in result: 64 assert (row[0] == 27 * 1000 * 1000 * 1000) 65 cursor.execute("select count(0) from xpower_measure") 66 result = cursor.fetchall() 67 for row in result: 68 assert (row[0] > 0) 69 70 cursor.execute("select * from xpower_measure where filter_id = 0 order by ts limit 0,10") 71 result = cursor.fetchall() 72 for row in result: 73 assert (row[3] > 0) 74 #电池电量 75 cursor.execute("select * from xpower_measure where filter_id = 2 order by ts limit 0,10") 76 result = cursor.fetchall() 77 for row in result: 78 assert (row[3] > 0) 79 cursor.close() 80 conn.close() 81 82 @pytest.mark.L0 83 def test_xpowerplugin_total(self): 84 subprocess.check_output(f"hdc file send ./inputfiles/xpower_plugin/config_xpower_total.txt /data/local/tmp/", shell=False, 85 text=True, encoding="utf-8") 86 task_thread = threading.Thread(target=task_total, args=()) 87 task_thread.start() 88 subprocess.check_output(f"hdc shell aa start -a com.huawei.hmos.settings.MainAbility -b com.huawei.hmos.settings", shell=False, 89 text=True, encoding="utf-8") 90 subprocess.check_output(f"hdc shell aa start -a com.huawei.hmos.photos.MainAbility -b com.huawei.hmos.photos", shell=False, 91 text=True, encoding="utf-8") 92 task_thread.join() 93 subprocess.check_output(f"hdc file recv /data/local/tmp/test_xpower_total.htrace ./outputfiles/ ", shell=False, 94 text=True, encoding="utf-8") 95 # 检查文件大小 96 file_size = get_file_size(f"./outputfiles/test_xpower_total.htrace") 97 assert (file_size > 1024) 98 subprocess.check_output( 99 r"./inputfiles/trace_streamer_db.exe ./outputfiles/test_xpower_total.htrace -e ./outputfiles/test_xpower_total.db") 100 # 连接数据库文件 101 conn = sqlite3.connect(r'./outputfiles/test_xpower_total.db') 102 # 创建游标对象 103 cursor = conn.cursor() 104 # 执行SQL查询 105 cursor.execute("select end_ts - start_ts as time from trace_range") 106 result = cursor.fetchall() 107 #断言trace 时长33秒 108 for row in result: 109 assert (row[0] == 33 * 1000 * 1000 * 1000) 110 cursor.execute("select count(0) from xpower_measure") 111 result = cursor.fetchall() 112 for row in result: 113 assert (row[0] > 0) 114 #电量百分比 115 cursor.execute("select * from xpower_measure, measure_filter where filter_id = id and name = 'Battery.Level' limit 0,10") 116 result = cursor.fetchall() 117 for row in result: 118 assert (row[3] > 0) 119 #外壳温度 120 cursor.execute("select * from xpower_measure, measure_filter where filter_id = id and name = 'ThermalReport.ShellTemp' limit 0,10") 121 result = cursor.fetchall() 122 for row in result: 123 assert (row[3] > 0) 124 #温度等级 125 cursor.execute("select * from xpower_measure, measure_filter where filter_id = id and name = 'ThermalReport.ThermalLevel' limit 0,10") 126 result = cursor.fetchall() 127 for row in result: 128 assert (row[3] >= 0) 129 cursor.close() 130 conn.close()