• 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 */
15import deviceInfo from '@ohos.deviceInfo';
16import CommonEvent from '@ohos.commonEvent';
17import database from '../database/DatabaseUtils';
18import { CPU } from './item/CPU';
19import { Power } from './item/Power';
20import { dateFormat } from '../utils/TimeUtils';
21import { TPowerAppInfo } from '../entity/DatabaseEntity';
22import { csvGeneralInfo, csvTIndexInfo } from '../utils/CSVUtils';
23import { createFilePath } from '../utils/IOUtils';
24import CheckEmptyUtils from '../utils/CheckEmptyUtils';
25import { GPData, TIndexInfo, TGeneralInfo, TPowerSensorInfo } from '../entity/DatabaseEntity';
26import { ProfilerFactory } from './base/ProfilerFactory';
27import { CollectorType } from './base/ProfilerConstant';
28import SPLogger from '../../common/utils/SPLogger';
29import { initDbIndex } from '../../common/database/LocalRepository';
30
31export class ProfilerTask {
32  private collectItems: Array<string> = [];
33  private static instance: ProfilerTask;
34
35  public static getInstance(): ProfilerTask {
36    if (this.instance == null) {
37      this.instance = new ProfilerTask();
38    }
39    return ProfilerTask.instance;
40  }
41
42  initModule() {
43    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask initModule configs: ' + JSON.stringify(globalThis.collectConfigs));
44    const keys: any[] = Object.keys(globalThis.collectConfigs);
45    for (var key of keys) {
46      if (globalThis.collectConfigs[key]) {
47        let typeCollect = ProfilerFactory.getProfilerByConfig(key.toString());
48        if (typeCollect == null) {
49          continue;
50        } else {
51          this.collectItems.push(typeCollect.init());
52        }
53      }
54    }
55  }
56
57  getSupports(keys: string[]) {
58    let supports: Map<string, Boolean> = new Map<string, Boolean>();
59    for (var key of keys) {
60      let typeCollect = ProfilerFactory.getProfilerByConfig(key);
61      let isSupport = typeCollect.isSupport();
62      supports.set(key.toString(), isSupport);
63    }
64    return supports;
65  }
66
67  taskInit() {
68    SPLogger.INFO(ProfilerTask.name, 'ProfilerUtils taskInit call');
69    var now = new Date();
70    //数据库时间戳标记
71    globalThis.dbTime = now.getTime();
72    //开始时间
73    globalThis.startTime = now.getTime();
74    //结束时间
75    globalThis.endTime = -1;
76    //时间计数器
77    globalThis.collectIntervalNum = -1;
78    //入库数据
79    globalThis.tTndexs = [];
80    //实时数据
81    globalThis.tTndex = new TIndexInfo();
82    // ram队列
83    globalThis.ramArr = [];
84    // fps队列
85    globalThis.fpsArr = [];
86    // fpsJitter队列
87    globalThis.fpsJitterArr = [];
88
89    // fps队列
90    globalThis.fpsArr = [];
91    // power 电流队列
92    globalThis.powerCurArr = [];
93
94    // power 电压队列
95    globalThis.powerVoltArr = [];
96    globalThis.taskId = -1;
97    initDbIndex();
98    //初始化数据库  2022-02-23 dbName改为时间戳
99    database.createTable(globalThis.dbTime);
100    SPLogger.INFO(ProfilerTask.name, 'ProfilerUtils taskInit called');
101  }
102
103  taskStart() {
104    var gpDataArr: GPData[] = [];
105    this.collectItems.forEach((moduleName) => {
106      let typeCollect = ProfilerFactory.getProfilerByConfig(moduleName.toString());
107      if (typeCollect != null) {
108        let gpData: GPData = typeCollect.readData();
109        if (typeCollect instanceof CPU) {
110          gpDataArr.push(typeCollect.readCPULoad());
111        }
112        gpDataArr.push(gpData);
113      }
114    });
115
116    //防止cpuLoad -1
117    if (globalThis.collectIntervalNum > 0) {
118      let tTndex = database.gpArray2Index(gpDataArr);
119      globalThis.tTndexs.push(tTndex);
120      globalThis.tTndex = tTndex;
121      CommonEvent.publish('event', { code: 0, data: JSON.stringify(tTndex) }, (err) => {});
122    }
123    globalThis.collectIntervalNum++;
124  }
125
126  taskSingleItemStart(collectorType: CollectorType) {
127    let typeCollect = ProfilerFactory.getProfilerByConfig(collectorType.toString());
128    if (typeCollect instanceof Power) {
129      typeCollect.readFourTimesData();
130    }
131  }
132
133  taskStop() {
134    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask taskStop call');
135    // t_index_info  入库
136    if (globalThis.tTndexs.length > 2) {
137      for (var index = 2; index < globalThis.tTndexs.length; index++) {
138        const tTndex = globalThis.tTndexs[index];
139        database.insertData('t_index_info', globalThis.dbTime, tTndex);
140      }
141    }
142    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask insertData success');
143    createFilePath(
144      globalThis.abilityContext.getApplicationContext().filesDir + '/' + globalThis.dbTime + '/t_index_info.csv',
145    csvTIndexInfo(globalThis.tTndexs)
146    );
147    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask createFilePath index_info success');
148    // t_general_info  入库
149    globalThis.endTime = new Date().getTime();
150    let tGeneralInfo = new TGeneralInfo(
151    globalThis.dbTime.toString(),
152      'NA',
153    globalThis.appName,
154    globalThis.appVersion,
155    globalThis.packageName,
156    globalThis.startTime,
157    globalThis.endTime,
158    globalThis.collectIntervalNum,
159    globalThis.testTaskName
160    );
161    tGeneralInfo.board = deviceInfo.brand;
162    tGeneralInfo.deviceTypeName = deviceInfo.productModel;
163    tGeneralInfo.brand = deviceInfo.brand;
164    tGeneralInfo.version = deviceInfo.displayVersion;
165    tGeneralInfo.sn = deviceInfo.serial;
166    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask insertGeneraData');
167    database.insertGeneraData('t_general_info', tGeneralInfo);
168    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask insertGeneraData success');
169    createFilePath(
170      globalThis.abilityContext.getApplicationContext().filesDir + '/' + globalThis.dbTime + '/t_general_info.csv',
171    csvGeneralInfo(tGeneralInfo)
172    );
173
174    SPLogger.DEBUG(ProfilerTask.name, 'ProfilerTask taskStop called');
175  }
176  taskGetDubai() {
177    let tPowers: TPowerSensorInfo[] = [];
178    SPLogger.INFO(
179      'TAG',
180      'resultSet query_applications_display-----startTime' + JSON.stringify(dateFormat(globalThis.startTime))
181    );
182    SPLogger.INFO(
183      'TAG',
184      'resultSet query_applications_display-----endTime' + JSON.stringify(dateFormat(globalThis.endTime))
185    );
186    SPLogger.INFO('TAG', 'resultSet query_applications_display-----dbTime' + JSON.stringify(globalThis.dbTime));
187    database
188      .query_applications_display(
189      dateFormat(globalThis.startTime),
190      dateFormat(globalThis.endTime),
191      globalThis.packageName
192      )
193      .then((data) => {
194        if (data.length != 0) {
195          tPowers.push(data[0]);
196        }
197        database
198          .query_applications_cpu(
199          dateFormat(globalThis.startTime),
200          dateFormat(globalThis.endTime),
201          globalThis.packageName
202          )
203          .then((data) => {
204            if (data.length != 0) {
205              tPowers.push(data[0]);
206            }
207            database
208              .query_applications_gpu(
209              dateFormat(globalThis.startTime),
210              dateFormat(globalThis.endTime),
211              globalThis.packageName
212              )
213              .then((data) => {
214                if (data.length != 0) {
215                  tPowers.push(data[0]);
216                }
217                database
218                  .query_applications_audio(
219                  dateFormat(globalThis.startTime),
220                  dateFormat(globalThis.endTime),
221                  globalThis.packageName
222                  )
223                  .then((data) => {
224                    if (data.length != 0) {
225                      tPowers.push(data[0]);
226                    }
227                    database
228                      .query_applications_ddr(
229                      dateFormat(globalThis.startTime),
230                      dateFormat(globalThis.endTime),
231                      globalThis.packageName
232                      )
233                      .then((data) => {
234                        if (data.length != 0) {
235                          tPowers.push(data[0]);
236                        }
237                        database
238                          .query_applications_dss(
239                          dateFormat(globalThis.startTime),
240                          dateFormat(globalThis.endTime),
241                          globalThis.packageName
242                          )
243                          .then((data) => {
244                            if (data.length != 0) {
245                              tPowers.push(data[0]);
246                            }
247                            database
248                              .query_applications_system_idle(
249                              dateFormat(globalThis.startTime),
250                              dateFormat(globalThis.endTime),
251                              globalThis.packageName
252                              )
253                              .then((data) => {
254                                if (data.length != 0) {
255                                  tPowers.push(data[0]);
256                                }
257                                database
258                                  .query_applications_wifi_data(
259                                  dateFormat(globalThis.startTime),
260                                  dateFormat(globalThis.endTime),
261                                  globalThis.packageName
262                                  )
263                                  .then((data) => {
264                                    if (data.length != 0) {
265                                      tPowers.push(data[0]);
266                                    }
267                                    for (var i = 0; i < tPowers.length; i++) {
268                                      database.insertPowerSensor(
269                                        'task_powersensor_info',
270                                      globalThis.dbTime,
271                                      tPowers[i]
272                                      );
273                                    }
274                                  })
275                                  .then(() => {
276                                    return database.query_applications_power_info(
277                                    dateFormat(globalThis.startTime),
278                                    dateFormat(globalThis.endTime)
279                                    );
280                                  })
281                                  .then((tArr: Array<TPowerAppInfo>) => {
282                                    tArr.forEach((t) => {
283                                      database.insertPowerAppInfo('task_powerapp_info', globalThis.dbTime, t);
284                                    });
285                                  });
286                              });
287                          });
288                      });
289                  });
290              });
291          });
292      });
293  }
294  taskDestroy() {
295    //数据库时间戳标记
296    globalThis.dbTime = -1;
297    //开始时间
298    globalThis.startTime = -1;
299    //结束时间
300    globalThis.endTime = -1;
301    //入库数据
302    globalThis.tTndexs = [];
303
304    //采集配置恢复fileOpen
305    globalThis.collectConfigs = -1;
306    globalThis.collectPkg = -1;
307
308    //采集定时器 配置
309    globalThis.collectInterval = -1;
310    globalThis.collectIntervalCollect = -1;
311    globalThis.collectPowerCollect = -1;
312    globalThis.collectIntervalNum = -1;
313
314    globalThis.powerValue = 0;
315
316    //socket采集队列 fps ram
317    globalThis.fpsArr = [];
318
319    // power 电流队列
320    globalThis.powerCurArr = [];
321
322    // power 电压队列
323    globalThis.powerVoltArr = [];
324    globalThis.fpsJitterArr = [];
325    globalThis.ramArr = [];
326  }
327}
328