1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Scenario test case. 18""" 19 20import logging 21import os 22import time 23 24import pytest 25 26from aw import Utils 27from aw import Application 28from aw import profiler 29from aw.api import debugger_api, runtime_api, profiler_api 30 31 32@pytest.mark.cpu_profiler 33@pytest.mark.timeout(40) 34class TestCpuProfiler01: 35 """ 36 测试用例:多实例 CPU 调优 Time 录制 37 测试步骤: 38 1. 连接 connect server 和主线程 debugger server 39 2. 连接 worker 线程 debugger server 40 3. 所有线程使能 Runtime(Runtime.enable) 41 4. 所有线程设置采样间隔(Profiler.setSamplingInterval) 42 5. 所有线程启动 CPU 调试(Profiler.start) 43 6. 所有线程去使能 Debugger(Debugger.disable) 44 7. 等待 10 秒后关闭 CPU 调优,获取调优数据(Profiler.stop) 45 8. 销毁 worker 线程,对应的 debugger server 连接断开 46 9. 关闭主线程 debugger server 和 connect server 连接 47 """ 48 49 def setup_method(self): 50 logging.info('Start running TestCpuProfiler01: setup') 51 52 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 53 self.hilog_file_name = 'test_cpu_profiler_01.hilog.txt' 54 self.id_generator = Utils.message_id_generator() 55 56 # receive the hilog before the test start 57 Utils.clear_fault_log() 58 self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, 59 file_name=self.hilog_file_name, 60 debug_on=True) 61 62 def teardown_method(self): 63 Application.uninstall(self.config['bundle_name']) 64 65 # terminate the hilog receive process after the test done 66 time.sleep(3) 67 self.hilog_process.stdout.close() 68 self.hilog_process.terminate() 69 self.hilog_process.wait() 70 self.write_thread.join() 71 72 Utils.save_fault_log(log_path=self.log_path) 73 logging.info('TestCpuProfiler01 done') 74 75 def test(self, test_suite_worker_02): 76 logging.info('Start running TestCpuProfiler01: test') 77 self.config = test_suite_worker_02 78 websocket = self.config['websocket'] 79 taskpool = self.config['taskpool'] 80 pid = self.config['pid'] 81 self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) 82 self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) 83 self.profiler_impl = profiler_api.ProfilerImpl(self.id_generator, websocket) 84 85 taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) 86 taskpool.await_taskpool() 87 taskpool.task_join() 88 if taskpool.task_exception: 89 raise taskpool.task_exception 90 91 async def procedure(self, websocket): 92 ################################################################################################################ 93 # main thread: connect the debugger server 94 ################################################################################################################ 95 main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) 96 logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') 97 ################################################################################################################ 98 # worker thread: connect the debugger server 99 ################################################################################################################ 100 worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) 101 logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') 102 worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) 103 logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') 104 ################################################################################################################ 105 # main thread: Runtime.enable 106 ################################################################################################################ 107 await self.runtime_impl.send("Runtime.enable", main_thread) 108 ################################################################################################################ 109 # worker thread: Runtime.enable 110 ################################################################################################################ 111 await self.runtime_impl.send("Runtime.enable", worker_thread_1) 112 await self.runtime_impl.send("Runtime.enable", worker_thread_2) 113 ################################################################################################################ 114 # main thread: Profiler.setSamplingInterval 115 ################################################################################################################ 116 params = profiler.SamplingInterval(500) 117 await self.profiler_impl.send("Profiler.setSamplingInterval", main_thread, params) 118 ################################################################################################################ 119 # worker thread: Profiler.setSamplingInterval 120 ################################################################################################################ 121 params = profiler.SamplingInterval(500) 122 await self.profiler_impl.send("Profiler.setSamplingInterval", worker_thread_1, params) 123 await self.profiler_impl.send("Profiler.setSamplingInterval", worker_thread_2, params) 124 ################################################################################################################ 125 # main thread: Profiler.start 126 ################################################################################################################ 127 await self.profiler_impl.send("Profiler.start", main_thread) 128 ################################################################################################################ 129 # worker thread: Profiler.start 130 ################################################################################################################ 131 await self.profiler_impl.send("Profiler.start", worker_thread_1) 132 await self.profiler_impl.send("Profiler.start", worker_thread_2) 133 ################################################################################################################ 134 # main thread: Debugger.disable 135 ################################################################################################################ 136 await self.debugger_impl.send("Debugger.disable", main_thread) 137 ################################################################################################################ 138 # worker thread: Debugger.disable 139 ################################################################################################################ 140 await self.debugger_impl.send("Debugger.disable", worker_thread_1) 141 await self.debugger_impl.send("Debugger.disable", worker_thread_2) 142 ################################################################################################################ 143 # all thread: sleep 10 seconds 144 ################################################################################################################ 145 time.sleep(10) 146 ################################################################################################################ 147 # main thread: Profiler.stop 148 ################################################################################################################ 149 await self.profiler_impl.send("Profiler.stop", main_thread) 150 ################################################################################################################ 151 # worker thread: Profiler.stop 152 ################################################################################################################ 153 await self.profiler_impl.send("Profiler.stop", worker_thread_1) 154 await self.profiler_impl.send("Profiler.stop", worker_thread_2) 155 ################################################################################################################ 156 # close the websocket connections 157 ################################################################################################################ 158 await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 'close') 159 await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 'close') 160 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 161 await websocket.send_msg_to_connect_server('close') 162 ################################################################################################################