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 import heap_profiler 29from aw.api import debugger_api, runtime_api, heap_profiler_api 30 31 32@pytest.mark.heap_profiler 33@pytest.mark.timeout(80) 34class TestWorkerProfilerAllocation02: 35 """ 36 测试用例:多实例内存调优 Allocation 录制(-p dumpheap启动) 37 测试步骤: 38 1. -p dumpheap方式拉起应用 39 2. 连接 connect server 和主线程 debugger server 40 3. 连接 worker 线程 debugger server 41 4. 所有线程使能 Runtime(Runtime.enable) 42 5. 所有线程获取内存使用情况(Runtime.getHeapUsage) 43 6. 所有线程启动 Allocation 录制(HeapProfiler.startTrackingHeapObjects) 44 7. 所有线程获取内存使用情况(Runtime.getHeapUsage) 45 8. 等待 10 秒后关闭 Allocation 录制,获取数据(HeapProfiler.stopTrackingHeapObjects) 46 9. 销毁 worker 线程,对应的 debugger server 连接断开 47 10. 关闭主线程 debugger server 和 connect server 连接 48 """ 49 50 def setup_method(self): 51 logging.info('Start running TestWorkerProfilerAllocation02: setup') 52 53 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 54 self.hilog_file_name = 'test_worker_profiler_allocation_02.hilog.txt' 55 self.id_generator = Utils.message_id_generator() 56 57 # receive the hilog before the test start 58 Utils.clear_fault_log() 59 self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, 60 file_name=self.hilog_file_name, 61 debug_on=True) 62 63 def teardown_method(self): 64 Application.uninstall(self.config['bundle_name']) 65 66 # terminate the hilog receive process after the test done 67 time.sleep(3) 68 self.hilog_process.stdout.close() 69 self.hilog_process.terminate() 70 self.hilog_process.wait() 71 self.write_thread.join() 72 73 Utils.save_fault_log(log_path=self.log_path) 74 logging.info('TestWorkerProfilerAllocation02 done') 75 76 def test(self, test_suite_worker_02_dumpheap): 77 logging.info('Start running TestWorkerProfilerAllocation02: test') 78 self.config = test_suite_worker_02_dumpheap 79 websocket = self.config['websocket'] 80 taskpool = self.config['taskpool'] 81 pid = self.config['pid'] 82 self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) 83 self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) 84 self.heap_profiler_impl = heap_profiler_api.HeapProfilerImpl(self.id_generator, websocket) 85 86 taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) 87 taskpool.await_taskpool() 88 taskpool.task_join() 89 if taskpool.task_exception: 90 raise taskpool.task_exception 91 92 async def procedure(self, websocket): 93 ################################################################################################################ 94 # main thread: connect the debugger server 95 ################################################################################################################ 96 main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) 97 logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') 98 ################################################################################################################ 99 # main thread: Runtime.enable 100 ################################################################################################################ 101 await self.runtime_impl.send("Runtime.enable", main_thread) 102 ################################################################################################################ 103 # main thread: Runtime.getHeapUsage 104 ################################################################################################################ 105 await self.runtime_impl.send("Runtime.getHeapUsage", main_thread) 106 ################################################################################################################ 107 # main thread: HeapProfiler.startTrackingHeapObjects 108 ################################################################################################################ 109 params = heap_profiler.TrackingHeapObjectsParams(False) 110 await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", main_thread, params) 111 ################################################################################################################ 112 # main thread: Debugger.disable 113 ################################################################################################################ 114 await self.debugger_impl.send("Debugger.disable", main_thread) 115 ################################################################################################################ 116 # worker thread: connect the debugger server 117 ################################################################################################################ 118 worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) 119 logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') 120 worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) 121 logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') 122 ################################################################################################################ 123 # worker thread: Runtime.enable 124 ################################################################################################################ 125 await self.runtime_impl.send("Runtime.enable", worker_thread_1) 126 await self.runtime_impl.send("Runtime.enable", worker_thread_2) 127 ################################################################################################################ 128 # worker thread: Runtime.getHeapUsage 129 ################################################################################################################ 130 await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_1) 131 await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_2) 132 ################################################################################################################ 133 # worker thread: HeapProfiler.startTrackingHeapObjects 134 ################################################################################################################ 135 params = heap_profiler.TrackingHeapObjectsParams(False) 136 await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", worker_thread_1, params) 137 await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", worker_thread_2, params) 138 ################################################################################################################ 139 # worker thread: Debugger.disable 140 ################################################################################################################ 141 await self.debugger_impl.send("Debugger.disable", worker_thread_1) 142 await self.debugger_impl.send("Debugger.disable", worker_thread_2) 143 ################################################################################################################ 144 # all thread: sleep 10 seconds 145 ################################################################################################################ 146 time.sleep(10) 147 ################################################################################################################ 148 # main thread: Runtime.getHeapUsage 149 ################################################################################################################ 150 await self.runtime_impl.send("Runtime.getHeapUsage", main_thread) 151 ################################################################################################################ 152 # worker thread: Runtime.getHeapUsage 153 ################################################################################################################ 154 await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_1) 155 await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_2) 156 ################################################################################################################ 157 # main thread: HeapProfiler.stopTrackingHeapObjects 158 ################################################################################################################ 159 await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", main_thread) 160 ################################################################################################################ 161 # worker thread: HeapProfiler.stopTrackingHeapObjects 162 ################################################################################################################ 163 await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", worker_thread_1) 164 await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", worker_thread_2) 165 ################################################################################################################ 166 # close the websocket connections 167 ################################################################################################################ 168 await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 169 'close') 170 await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 171 'close') 172 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 173 await websocket.send_msg_to_connect_server('close') 174 ################################################################################################################