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 Application 27from aw import Utils 28from aw.api import debugger_api, runtime_api, heap_profiler_api 29 30 31@pytest.mark.heap_profiler 32@pytest.mark.timeout(80) 33class TestWorkerProfilerHeapSampling: 34 """ 35 测试用例:多实例内存调优 HeapSampling 录制 36 测试步骤: 37 1. 拉起应用,attach 主线程 38 2. 连接 connect server 和主线程 debugger server 39 3. 连接 worker 线程 debugger server 40 4. 所有线程使能 Runtime(Runtime.enable) 41 5. 所有线程去使能 Debugger(Debugger.disable) 42 6. 所有线程开启内存采样(HeapProfiler.startSampling) 43 7. 等待 10 秒后,所有线程结束内存采样,获取数据(HeapProfiler.stopSampling) 44 8. 销毁 worker 线程,对应的 debugger server 连接断开 45 9. 关闭主线程 debugger server 和 connect server 连接 46 """ 47 48 def setup_method(self): 49 logging.info('Start running TestWorkerProfilerHeapSampling: setup') 50 51 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 52 self.hilog_file_name = 'test_worker_profiler_heap_sampling.hilog.txt' 53 self.id_generator = Utils.message_id_generator() 54 55 # receive the hilog before the test start 56 Utils.clear_fault_log() 57 self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, 58 file_name=self.hilog_file_name, 59 debug_on=True) 60 61 def teardown_method(self): 62 Application.uninstall(self.config['bundle_name']) 63 64 # terminate the hilog receive process after the test done 65 time.sleep(3) 66 self.hilog_process.stdout.close() 67 self.hilog_process.terminate() 68 self.hilog_process.wait() 69 self.write_thread.join() 70 71 Utils.save_fault_log(log_path=self.log_path) 72 logging.info('TestWorkerProfilerHeapSampling done') 73 74 def test(self, test_suite_worker_02): 75 logging.info('Start running TestWorkerProfilerHeapSampling: test') 76 self.config = test_suite_worker_02 77 websocket = self.config['websocket'] 78 taskpool = self.config['taskpool'] 79 pid = self.config['pid'] 80 self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) 81 self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) 82 self.heap_profiler_impl = heap_profiler_api.HeapProfilerImpl(self.id_generator, websocket) 83 84 Application.attach(self.config['bundle_name']) 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: Debugger.disable 115 ################################################################################################################ 116 await self.debugger_impl.send("Debugger.disable", main_thread) 117 ################################################################################################################ 118 # worker thread: Debugger.disable 119 ################################################################################################################ 120 await self.debugger_impl.send("Debugger.disable", worker_thread_1) 121 await self.debugger_impl.send("Debugger.disable", worker_thread_2) 122 ################################################################################################################ 123 # main thread: HeapProfiler.startSampling 124 ################################################################################################################ 125 await self.heap_profiler_impl.send("HeapProfiler.startSampling", main_thread) 126 ################################################################################################################ 127 # worker thread: HeapProfiler.startSampling 128 ################################################################################################################ 129 await self.heap_profiler_impl.send("HeapProfiler.startSampling", worker_thread_1) 130 await self.heap_profiler_impl.send("HeapProfiler.startSampling", worker_thread_2) 131 ################################################################################################################ 132 # all thread: sleep 10 seconds 133 ################################################################################################################ 134 time.sleep(10) 135 ################################################################################################################ 136 # main thread: HeapProfiler.stopSampling 137 ################################################################################################################ 138 response = await self.heap_profiler_impl.send("HeapProfiler.stopSampling", main_thread) 139 assert response['result']['profile'] is not None 140 ################################################################################################################ 141 # worker thread: HeapProfiler.stopSampling 142 ################################################################################################################ 143 response = await self.heap_profiler_impl.send("HeapProfiler.stopSampling", worker_thread_1) 144 assert response['result']['profile'] is not None 145 response = await self.heap_profiler_impl.send("HeapProfiler.stopSampling", worker_thread_2) 146 assert response['result']['profile'] is not None 147 ################################################################################################################ 148 # close the websocket connections 149 ################################################################################################################ 150 await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 151 'close') 152 await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 153 'close') 154 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 155 await websocket.send_msg_to_connect_server('close') 156 ################################################################################################################