• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { TabPaneCpuUsage } from '../../../../../../src/trace/component/trace/sheet/cpu/TabPaneCpuUsage';
17const sqlit = require('../../../../../../src/trace/database/sql/Cpu.sql');
18jest.mock('../../../../../../src/trace/database/sql/Cpu.sql');
19jest.mock('../../../../../../src/trace/bean/NativeHook', () => {
20  return {};
21});
22
23window.ResizeObserver =
24  window.ResizeObserver ||
25  jest.fn().mockImplementation(() => ({
26    disconnect: jest.fn(),
27    observe: jest.fn(),
28    unobserve: jest.fn(),
29  }));
30
31describe('TabPaneCpuUsage Test', () => {
32  let tabPaneCpuUsage = new TabPaneCpuUsage();
33
34  let mockGetTabCpuUsage = sqlit.getTabCpuUsage;
35  let mockGetTabCpuFreq = sqlit.getTabCpuFreq;
36
37  mockGetTabCpuUsage.mockResolvedValue([]);
38  mockGetTabCpuFreq.mockResolvedValue([]);
39
40  let selectionData = {
41    cpus: [1, 0],
42    threadIds: [12, 3, 0],
43    trackIds: [3, 789, 89],
44    funTids: [5, 0],
45    heapIds: [0, 43],
46    nativeMemory: [],
47    cpuAbilityIds: [10, 45],
48    memoryAbilityIds: [],
49    diskAbilityIds: [85],
50    networkAbilityIds: [],
51    leftNs: 8540,
52    rightNs: 96440,
53    hasFps: false,
54    statisticsSelectData: undefined,
55    perfSampleIds: [1, 4, 44],
56    perfCpus: [],
57    perfProcess: [],
58    perfThread: [],
59    perfAll: false,
60  };
61
62  it('TabPaneCpuUsageTest01', function () {
63    expect(
64      tabPaneCpuUsage.sortTable(
65        [
66          [1, 2, 3, 9, 6, 4],
67          [5, 2, 1, 4, 9, 6],
68        ],
69        3,
70        true
71      )
72    ).toBeUndefined();
73  });
74
75  it('TabPaneCpuUsageTest08', function () {
76    expect(
77      tabPaneCpuUsage.sortTable(
78        [
79          [1, 2, 3, 9, 6, 4],
80          [5, 2, 1, 4, 9, 6],
81        ],
82        4,
83        false
84      )
85    ).toBeUndefined();
86  });
87
88  it('TabPaneCpuUsageTest09', function () {
89    expect(
90      tabPaneCpuUsage.sortTable(
91        [
92          [1, 2, 3, 9, 6, 4],
93          [5, 2, 1, 4, 9, 6],
94        ],
95        5,
96        true
97      )
98    ).toBeUndefined();
99  });
100
101  it('TabPaneCpuUsageTest02', function () {
102    expect(
103      tabPaneCpuUsage.sortTable(
104        [
105          [1, 2, 1, 9, 6, 4],
106          [5, 2, 1, 4, 9, 6],
107        ],
108        1,
109        true
110      )
111    ).toBeUndefined();
112  });
113
114  it('TabPaneCpuUsageTest03', function () {
115    expect(
116      tabPaneCpuUsage.sortTable(
117        [
118          [1, 2, 2, 9, 6, 4],
119          [5, 2, 1, 4, 9, 6],
120        ],
121        2,
122        false
123      )
124    ).toBeUndefined();
125  });
126  it('TabPaneCpuUsageTest04', function () {
127    let result = tabPaneCpuUsage.sortFreq([
128      {
129        cpu: 0,
130        value: 30,
131        startNs: 342,
132        dur: 341,
133      },
134      {
135        cpu: 1,
136        value: 322,
137        startNs: 762,
138        dur: 984,
139      },
140    ]);
141    expect(result[0][0]).toBe(322);
142  });
143  it('TabPaneCpuUsageTest05', function () {
144    expect(
145      tabPaneCpuUsage.getFreqTop3(
146        {
147          cpu: 0,
148          usage: 0,
149          usageStr: 'usage',
150          top1: 1,
151          top2: 2,
152          top3: 3,
153          top1Percent: 11,
154          top1PercentStr: 'Str1',
155          top2Percent: 22,
156          top2PercentStr: 'Str2',
157          top3Percent: 33,
158          top3PercentStr: 'Str3',
159        },
160        undefined,
161        undefined,
162        undefined,
163        1
164      )
165    ).toBeUndefined();
166  });
167  it('TabPaneCpuUsageTest06', function () {
168    let result = tabPaneCpuUsage.groupByCpuToMap([
169      {
170        cpu: 0,
171        value: 90,
172        startNs: 250,
173        dur: 560,
174      },
175      {
176        cpu: 1,
177        value: 782,
178        startNs: 52,
179        dur: 4,
180      },
181    ]);
182    expect(result.get(0).length).toBe(1);
183  });
184
185  it('TabPaneCpuUsageTest11', function () {
186    document.body.innerHTML = `<div id="CpuUsage"></div>`;
187    let tabPaneCpuUsage = document.querySelector('#CpuUsage') as TabPaneCpuUsage;
188    expect(tabPaneCpuUsage.sortFreq).toBe(undefined);
189  });
190  it('TabPaneCpuUsageTest12', function () {
191    document.body.innerHTML = `<div id="CpuUsage"></div>`;
192    let tabPaneCpuUsage = document.querySelector('#CpuUsage') as TabPaneCpuUsage;
193    tabPaneCpuUsage.data = [
194      {
195        cpus: [2],
196        threadIds: [],
197        trackIds: [12, 4],
198        funTids: [56, 345],
199        heapIds: [],
200        nativeMemory: [],
201        cpuAbilityIds: [10, 32, 1],
202        memoryAbilityIds: [],
203        diskAbilityIds: [12, 76],
204        networkAbilityIds: [],
205        leftNs: 77,
206        rightNs: 987,
207        hasFps: false,
208        statisticsSelectData: undefined,
209        perfSampleIds: [],
210        perfCpus: [0, 9],
211        perfProcess: [],
212        perfThread: [],
213        perfAll: true,
214      },
215    ];
216    expect(tabPaneCpuUsage).toBeTruthy();
217  });
218});
219