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