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(40)
34class TestHeapProfiler01:
35    """
36    测试用例:多实例内存调优 Allocation 录制
37    测试步骤:
38        1.  拉起应用,attach 主线程
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 TestHeapProfiler01: setup')
52
53        self.log_path = rf'{os.path.dirname(__file__)}\..\log'
54        self.hilog_file_name = 'test_heap_profiler_01.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('TestHeapProfiler01 done')
75
76    def test(self, test_suite_worker_02):
77        logging.info('Start running TestHeapProfiler01: test')
78        self.config = test_suite_worker_02
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        Application.attach(self.config['bundle_name'])
87        taskpool.submit(websocket.main_task(taskpool, self.procedure, pid))
88        taskpool.await_taskpool()
89        taskpool.task_join()
90        if taskpool.task_exception:
91            raise taskpool.task_exception
92
93    async def procedure(self, websocket):
94        ################################################################################################################
95        # main thread: connect the debugger server
96        ################################################################################################################
97        main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True)
98        logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}')
99        ################################################################################################################
100        # worker thread: connect the debugger server
101        ################################################################################################################
102        worker_thread_1 = 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_1.instance_id}')
104        worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False)
105        logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}')
106        ################################################################################################################
107        # main thread: Runtime.enable
108        ################################################################################################################
109        await self.runtime_impl.send("Runtime.enable", main_thread)
110        ################################################################################################################
111        # worker thread: Runtime.enable
112        ################################################################################################################
113        await self.runtime_impl.send("Runtime.enable", worker_thread_1)
114        await self.runtime_impl.send("Runtime.enable", worker_thread_2)
115        ################################################################################################################
116        # main thread: Runtime.getHeapUsage
117        ################################################################################################################
118        await self.runtime_impl.send("Runtime.getHeapUsage", main_thread)
119        ################################################################################################################
120        # worker thread: Runtime.getHeapUsage
121        ################################################################################################################
122        await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_1)
123        await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_2)
124        ################################################################################################################
125        # main thread: HeapProfiler.startTrackingHeapObjects
126        ################################################################################################################
127        params = heap_profiler.TrackingHeapObjectsParams(False)
128        await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", main_thread, params)
129        ################################################################################################################
130        # worker thread: HeapProfiler.startTrackingHeapObjects
131        ################################################################################################################
132        params = heap_profiler.TrackingHeapObjectsParams(False)
133        await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", worker_thread_1, params)
134        await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", worker_thread_2, params)
135        ################################################################################################################
136        # main thread: Debugger.disable
137        ################################################################################################################
138        await self.debugger_impl.send("Debugger.disable", main_thread)
139        ################################################################################################################
140        # worker thread: Debugger.disable
141        ################################################################################################################
142        await self.debugger_impl.send("Debugger.disable", worker_thread_1)
143        await self.debugger_impl.send("Debugger.disable", worker_thread_2)
144        ################################################################################################################
145        # all thread: sleep 10 seconds
146        ################################################################################################################
147        time.sleep(10)
148        ################################################################################################################
149        # main thread: Runtime.getHeapUsage
150        ################################################################################################################
151        await self.runtime_impl.send("Runtime.getHeapUsage", main_thread)
152        ################################################################################################################
153        # worker thread: Runtime.getHeapUsage
154        ################################################################################################################
155        await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_1)
156        await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_2)
157        ################################################################################################################
158        # main thread: HeapProfiler.stopTrackingHeapObjects
159        ################################################################################################################
160        await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", main_thread)
161        ################################################################################################################
162        # worker thread: HeapProfiler.stopTrackingHeapObjects
163        ################################################################################################################
164        await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", worker_thread_1)
165        await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", worker_thread_2)
166        ################################################################################################################
167        # close the websocket connections
168        ################################################################################################################
169        await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue,
170                                                    'close')
171        await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue,
172                                                    'close')
173        await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close')
174        await websocket.send_msg_to_connect_server('close')
175        ################################################################################################################