1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { SpHiPerf } from '../../../../src/trace/component/chart/SpHiPerf'; 17jest.mock('../../../../src/trace/component/SpSystemTrace', () => { 18 return {}; 19}); 20import { TraceRow } from '../../../../src/trace/component/trace/base/TraceRow'; 21import { HiPerfStruct } from '../../../../src/trace/database/ui-worker/ProcedureWorkerCommon'; 22import { HiPerfCallChartStruct } from '../../../../src/trace/database/ui-worker/hiperf/ProcedureWorkerHiPerfCallChart'; 23jest.mock('../../../../src/js-heap/model/DatabaseStruct'); 24const sqlit = require('../../../../src/trace/database/sql/Perf.sql'); 25jest.mock('../../../../src/trace/database/sql/Perf.sql'); 26jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => { 27 return {}; 28}); 29jest.mock('../../../../src/trace/component/chart/PerfDataQuery', () => { 30 return {}; 31}); 32const intersectionObserverMock = () => ({ 33 observe: () => null, 34}); 35window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock); 36 37window.ResizeObserver = 38 window.ResizeObserver || 39 jest.fn().mockImplementation(() => ({ 40 disconnect: jest.fn(), 41 observe: jest.fn(), 42 unobserve: jest.fn(), 43 })); 44 45describe('SpHiPerf Test', () => { 46 let perfDataQuery = sqlit.perfDataQuery; 47 let queryPerfCmdline = sqlit.queryPerfCmdline; 48 queryPerfCmdline.mockResolvedValue([ 49 { 50 report_value: 51 'hiperf record --control prepare -o /data/local/tmp…e sched:sched_waking -a -s dwarf -f 1000 --offcpu', 52 }, 53 ]); 54 55 let queryPerfThread = sqlit.queryPerfThread; 56 queryPerfThread.mockResolvedValue([ 57 { 58 tid: 11, 59 threadName: 'ksoftirqd/0', 60 pid: 11, 61 processName: 'ksoftirqd/0', 62 }, 63 { 64 tid: 1, 65 threadName: 'threadName111', 66 pid: 1, 67 processName: 'processNam111e', 68 }, 69 ]); 70 71 let queryHiPerfEventList = sqlit.queryHiPerfEventList; 72 queryHiPerfEventList.mockResolvedValue([ 73 { 74 id: 0, 75 report_value: 'sched:sched_waking', 76 }, 77 { 78 id: 1, 79 report_value: 'sched:sched_switch', 80 }, 81 ]); 82 83 let queryHiPerfCpuMergeData2 = sqlit.queryHiPerfCpuMergeData2; 84 queryHiPerfCpuMergeData2.mockResolvedValue([ 85 { 86 id: 0, 87 callchain_id: 1, 88 timestamp: 3468360924674, 89 thread_id: 2469, 90 event_count: 1, 91 event_type_id: 0, 92 timestamp_trace: 3468360965799, 93 cpu_id: 2, 94 thread_state: 'Running', 95 startNS: 0, 96 }, 97 { 98 id: 4, 99 callchain_id: 1, 100 timestamp: 3468361000799, 101 thread_id: 2469, 102 event_count: 1, 103 event_type_id: 0, 104 timestamp_trace: 3468361041924, 105 cpu_id: 2, 106 thread_state: 'Running', 107 startNS: 76125, 108 }, 109 { 110 id: 8, 111 callchain_id: 1, 112 timestamp: 3468361045716, 113 thread_id: 2469, 114 event_count: 1, 115 event_type_id: 0, 116 timestamp_trace: 3468361086841, 117 cpu_id: 2, 118 thread_state: 'Running', 119 startNS: 121042, 120 }, 121 { 122 id: 9, 123 callchain_id: 4, 124 timestamp: 3468361054466, 125 thread_id: 1336, 126 event_count: 1, 127 event_type_id: 1, 128 timestamp_trace: 3468361095591, 129 cpu_id: 3, 130 thread_state: 'Suspend', 131 startNS: 129792, 132 }, 133 ]); 134 let getPerfEventType = sqlit.queryPerfEventType; 135 getPerfEventType.mockResolvedValue([ 136 { 137 id: 1, 138 report_value: 'sched:sched_waking', 139 }, 140 ]); 141 let htmlElement: any = document.createElement('sp-system-trace'); 142 let spHiPerf = new SpHiPerf(htmlElement); 143 it('SpHiPerf01', function () { 144 spHiPerf.init(); 145 expect(spHiPerf).toBeDefined(); 146 }); 147 it('SpHiPerf02', function () { 148 let cpuData = [ 149 { 150 cpu_id: 0, 151 }, 152 ]; 153 let threadData = [ 154 { 155 tid: 11, 156 threadName: 'ksoftirqd/0', 157 pid: 11, 158 processName: 'ksoftirqd/0', 159 }, 160 ]; 161 expect(spHiPerf.setCallTotalRow(new TraceRow<any>(), cpuData, threadData)).not.toBeUndefined(); 162 }); 163 it('SpHiPerf03', function () { 164 let traceRow = new TraceRow<HiPerfCallChartStruct>(); 165 let cpuData = [ 166 { 167 cpu_id: 0, 168 }, 169 { 170 cpu_id: 1, 171 }, 172 ]; 173 let map = new Map(); 174 expect(spHiPerf.setCallChartRowSetting(traceRow, cpuData, map)).toBeUndefined(); 175 }); 176 it('SpHiPerf04', function () { 177 let traceRow = new TraceRow<HiPerfStruct>(); 178 let arr = [ 179 { 180 tid: 0, 181 pid: 1, 182 threadName: 'threadName', 183 processName: 'processName', 184 }, 185 ]; 186 expect(spHiPerf.addHiPerfThreadRow(arr, traceRow)).toBeUndefined(); 187 }); 188 it('SpHiPerf05', function () { 189 let traceRow = new TraceRow<HiPerfStruct>(); 190 expect(spHiPerf.resetChartData(traceRow)).toBeUndefined(); 191 }); 192 it('SpHiPerf06', function () { 193 expect(spHiPerf.resetAllChartData()).toBeUndefined(); 194 }); 195}); 196