• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 re
18from utils import *
19
20def CheckCpuUsageOutput(output):
21    ret = re.search("PID   Total Usage	   User Space    Kernel Space    Page Fault Minor    Page Fault Major    Name\n([^\n]+\n){4,}", output)
22    return ret is not None
23
24def CheckCpuUsageWithPidOutput(output):
25    ret = re.search("PID   Total Usage	   User Space    Kernel Space    Page Fault Minor    Page Fault Major    Name\n([^\n]+\n){1,}", output)
26    return ret is not None
27
28def CheckCpufreqOutput(output):
29    ret = re.search("cmd is: cat /sys/devices/system/cpu/cpu\d/cpufreq/cpuinfo_cur_freq\n\n\d+", output)
30    return ret is not None
31
32def GetCpuUsageOutput():
33    subprocess.check_call("hdc shell aa start -a EntryAbility -b com.example.jsleakwatcher", shell=True)
34    process_hilog = subprocess.Popen(['hdc', 'shell', 'hilog | grep jsLeakWatcher_app_cpuUsage > /data/local/tmp/getcpuusage_interface.txt'])
35    time.sleep(1)
36    # 单击【GetCpuUsage】按钮
37    TouchButtonByText("GetCpuUsage")
38    time.sleep(3)
39    process_hilog.terminate()
40    time.sleep(3)
41    output = subprocess.check_output(f"hdc shell cat /data/local/tmp/getcpuusage_interface.txt", text=True, encoding="utf-8")
42    return output
43
44def GetJsLeakWatcherCpuUsage(output):
45    # 按行分割数据
46    lines = output.splitlines()
47    # 初始化一个空列表来存储提取的 CPU 使用率
48    cpu_usages = []
49    # 遍历每一行
50    for line in lines:
51        # 检查行中是否包含 "jsLeakWatcher_app_cpuUsage:"
52        if "jsLeakWatcher_app_cpuUsage:" in line:
53            # 分割行并提取 CPU 使用率
54            parts = line.split(":")
55            cpu_usage = parts[-1].strip()
56            cpu_usages.append(cpu_usage)
57    return float(cpu_usages[-1])
58
59class TestHidumperCpu:
60
61    @pytest.mark.L0
62    def test_cpuusage_all(self):
63        command = "hidumper --cpuusage"
64        hidumperTmpCmd = "OPT:cpuusage SUB_OPT:"
65        # 校验命令行输出
66        CheckCmd(command, CheckCpuUsageOutput, hidumperTmpCmd)
67        # 校验命令行重定向输出
68        CheckCmdRedirect(command, CheckCpuUsageOutput, None, hidumperTmpCmd)
69        # 校验命令行输出到zip文件
70        CheckCmdZip(command, CheckCpuUsageOutput)
71
72    @pytest.mark.L0
73    def test_cpuusage_pid(self):
74        command = "hidumper --cpuusage 1"
75        hidumperTmpCmd = "OPT:cpuusage SUB_OPT:"
76        # 校验命令行输出
77        CheckCmd(command, CheckCpuUsageWithPidOutput, hidumperTmpCmd)
78        # 校验命令行重定向输出
79        CheckCmdRedirect(command, CheckCpuUsageWithPidOutput, None, hidumperTmpCmd)
80        # 校验命令行输出到zip文件
81        CheckCmdZip(command, CheckCpuUsageWithPidOutput)
82
83    @pytest.mark.L0
84    def test_getcpuusage_interface(self):
85        output = GetCpuUsageOutput()
86        assert "jsLeakWatcher_app_cpuUsage:" in output
87        jsLeakWatcher_app_cpuUsage = GetJsLeakWatcherCpuUsage(output)
88        assert jsLeakWatcher_app_cpuUsage > 0
89
90    @pytest.mark.L3
91    def test_cpuusage_error_pid(self):
92        command = f"hidumper --cpuusage 2147483647;hidumper --cpuusage -2147483647"
93        hidumperTmpCmd = "OPT:cpuusage SUB_OPT:"
94        # 校验命令行输出
95        CheckCmd(command, lambda output : "hidumper: No such process: 2147483647\nhidumper: option pid missed. 2" in output, hidumperTmpCmd)
96        command = f"hidumper --cpuusage 2147483648;hidumper --cpuusage -2147483648"
97        CheckCmd(command, lambda output : "hidumper: option pid missed. 2" in output, hidumperTmpCmd)
98
99    @pytest.mark.L0
100    def test_cpufreq(self):
101        command = "hidumper --cpufreq"
102        hidumperTmpCmd = "OPT:cpufreq SUB_OPT:"
103        # 校验命令行输出
104        CheckCmd(command, CheckCpufreqOutput, hidumperTmpCmd)
105        # 校验命令行重定向输出
106        CheckCmdRedirect(command, CheckCpufreqOutput, None, hidumperTmpCmd)
107        # 校验命令行输出到zip文件
108        CheckCmdZip(command, CheckCpufreqOutput)