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';
21jest.mock('../../../../src/js-heap/model/DatabaseStruct');
22const sqlit = require('../../../../src/trace/database/sql/Perf.sql');
23jest.mock('../../../../src/trace/database/sql/Perf.sql');
24jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => {
25  return {};
26});
27jest.mock('../../../../src/trace/component/chart/PerfDataQuery',()=>{
28  return {}
29})
30const intersectionObserverMock = () => ({
31  observe: () => null,
32});
33window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock);
34
35window.ResizeObserver =
36  window.ResizeObserver ||
37  jest.fn().mockImplementation(() => ({
38    disconnect: jest.fn(),
39    observe: jest.fn(),
40    unobserve: jest.fn(),
41  }));
42
43describe('SpHiPerf Test', () => {
44  let perfDataQuery = sqlit.perfDataQuery
45  let queryPerfCmdline = sqlit.queryPerfCmdline;
46  queryPerfCmdline.mockResolvedValue([
47    {
48      report_value:
49        'hiperf record --control prepare -o /data/local/tmp…e sched:sched_waking -a -s dwarf -f 1000 --offcpu',
50    },
51  ]);
52
53  let queryPerfThread = sqlit.queryPerfThread;
54  queryPerfThread.mockResolvedValue([
55    {
56      tid: 11,
57      threadName: "ksoftirqd/0",
58      pid: 11,
59      processName: "ksoftirqd/0"
60    },
61    {
62      tid: 1,
63      threadName: 'threadName111',
64      pid: 1,
65      processName: 'processNam111e',
66    },
67  ]);
68
69  let queryHiPerfEventList = sqlit.queryHiPerfEventList;
70  queryHiPerfEventList.mockResolvedValue([
71    {
72      id: 0,
73      report_value: 'sched:sched_waking',
74    },
75    {
76      id: 1,
77      report_value: 'sched:sched_switch',
78    },
79  ]);
80
81  let queryHiPerfCpuMergeData2 = sqlit.queryHiPerfCpuMergeData2;
82  queryHiPerfCpuMergeData2.mockResolvedValue([
83    {
84      id: 0,
85      callchain_id: 1,
86      timestamp: 3468360924674,
87      thread_id: 2469,
88      event_count: 1,
89      event_type_id: 0,
90      timestamp_trace: 3468360965799,
91      cpu_id: 2,
92      thread_state: 'Running',
93      startNS: 0,
94    },
95    {
96      id: 4,
97      callchain_id: 1,
98      timestamp: 3468361000799,
99      thread_id: 2469,
100      event_count: 1,
101      event_type_id: 0,
102      timestamp_trace: 3468361041924,
103      cpu_id: 2,
104      thread_state: 'Running',
105      startNS: 76125,
106    },
107    {
108      id: 8,
109      callchain_id: 1,
110      timestamp: 3468361045716,
111      thread_id: 2469,
112      event_count: 1,
113      event_type_id: 0,
114      timestamp_trace: 3468361086841,
115      cpu_id: 2,
116      thread_state: 'Running',
117      startNS: 121042,
118    },
119    {
120      id: 9,
121      callchain_id: 4,
122      timestamp: 3468361054466,
123      thread_id: 1336,
124      event_count: 1,
125      event_type_id: 1,
126      timestamp_trace: 3468361095591,
127      cpu_id: 3,
128      thread_state: 'Suspend',
129      startNS: 129792,
130    },
131  ]);
132  let getPerfEventType = sqlit.queryPerfEventType;
133  getPerfEventType.mockResolvedValue([{
134    id:1,
135    report_value:'sched:sched_waking',
136  }])
137  let htmlElement: any = document.createElement('sp-system-trace');
138  let spHiPerf = new SpHiPerf(htmlElement);
139  it('SpHiPerf01', function () {
140    spHiPerf.init();
141    expect(spHiPerf).toBeDefined();
142  });
143  it('SpHiPerf02', function () {
144    let cpuData = [
145      {
146        cpu_id: 0
147      }
148    ]
149    let threadData = [
150      {
151        tid: 11,
152        threadName: "ksoftirqd/0",
153        pid: 11,
154        processName: "ksoftirqd/0"
155      }
156    ]
157    expect(spHiPerf.setCallTotalRow(new TraceRow<any>(),cpuData,threadData)).not.toBeUndefined()
158  });
159});
160