• 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'
29
30export class ProfilerTask {
31  private collectItems:Array<string> = []
32  private static instance: ProfilerTask
33
34  public static getInstance(): ProfilerTask {
35    if (this.instance == null) {
36      this.instance = new ProfilerTask
37    }
38    return ProfilerTask.instance
39  }
40
41  initModule() {
42    SPLogger.DEBUG(ProfilerTask.name,'ProfilerTask initModule configs: ' + JSON.stringify(globalThis.collectConfigs))
43    const keys: any[] = Object.keys(globalThis.collectConfigs)
44    for (var key of keys) {
45      if (globalThis.collectConfigs[key]) {
46        let typeCollect = ProfilerFactory.getProfilerByConfig(key.toString())
47        SPLogger.DEBUG(ProfilerTask.name,'ProfilerTask initModule: ' + typeCollect)
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 = new Array<TIndexInfo>()
80    //实时数据
81    globalThis.tTndex = new TIndexInfo()
82    // ram队列
83    globalThis.ramArr = new Array<String>()
84    // fps队列
85    globalThis.fpsArr = new Array<String>()
86    // fpsJitter队列
87    globalThis.fpsJitterArr = new Array<String>()
88
89    // fps队列
90    globalThis.fpsArr = new Array<String>()
91    // power 电流队列
92    globalThis.powerCurArr = new Array<String>()
93
94    // power 电压队列
95    globalThis.powerVoltArr = new Array<String>()
96
97
98    //初始化数据库  2022-02-23 dbName改为时间戳
99    database.createTable(globalThis.dbTime)
100    SPLogger.INFO(ProfilerTask.name,'ProfilerUtils taskInit called');
101  }
102
103  taskStart() {
104    SPLogger.DEBUG(ProfilerTask.name,'ProfilerTask taskStart call');
105    var gpDataArr: Array<GPData> = new Array<GPData>()
106    this.collectItems.forEach(
107      (moduleName)=>{
108        let typeCollect = ProfilerFactory.getProfilerByConfig(moduleName.toString())
109        if (typeCollect != null) {
110          let gpData:GPData = typeCollect.readData()
111          if(typeCollect instanceof CPU){
112            gpDataArr.push(typeCollect.readCPULoad())
113          }
114          gpDataArr.push(gpData)
115          SPLogger.DEBUG(ProfilerTask.name,"profiler ProfilerTask:curData:"+gpData);
116        }
117      }
118    )
119
120    //防止cpuLoad -1
121    if (globalThis.collectIntervalNum > 0) {
122      let tTndex = database.gpArray2Index(gpDataArr)
123      globalThis.tTndexs.push(tTndex)
124      globalThis.tTndex = tTndex;
125      CommonEvent.publish("event", { code: 0, data: JSON.stringify(tTndex), }, (err)=>{});
126    }
127    globalThis.collectIntervalNum++
128    SPLogger.DEBUG(ProfilerTask.name,"profiler globalThis.collectIntervalNum " + globalThis.collectIntervalNum)
129
130    SPLogger.DEBUG(ProfilerTask.name,'ProfilerTask taskStart called');
131  }
132
133  taskSingleItemStart(collectorType : CollectorType) {
134    SPLogger.ERROR("ProfilerTaskTest",'ProfilerTask taskStart call collectorType' + collectorType.toString());
135    let typeCollect = ProfilerFactory.getProfilerByConfig(collectorType.toString())
136    if(typeCollect instanceof Power) {
137        typeCollect.readFourTimesData()
138    }
139  }
140
141  taskStop() {
142    SPLogger.DEBUG(ProfilerTask.name,'ProfilerTask taskStop call');
143    // t_index_info  入库
144    if (globalThis.tTndexs.length > 2) {
145      for (var index = 2; index < globalThis.tTndexs.length; index++) {
146        const tTndex = globalThis.tTndexs[index];
147        database.insertData("t_index_info", globalThis.dbTime, tTndex)
148      }
149    }
150    createFilePath(globalThis.abilityContext.getApplicationContext().filesDir + "/" + globalThis.dbTime + "/t_index_info.csv", csvTIndexInfo(globalThis.tTndexs))
151    // t_general_info  入库
152    globalThis.endTime = new Date().getTime()
153    let tGeneralInfo = new TGeneralInfo(
154      globalThis.dbTime.toString(),
155      "NA",
156      globalThis.appName,
157      globalThis.appVersion,
158      globalThis.packageName,
159      globalThis.startTime,
160      globalThis.endTime,
161      globalThis.collectIntervalNum,
162      globalThis.testTaskName
163    )
164    tGeneralInfo.board = deviceInfo.brand
165    tGeneralInfo.deviceTypeName = deviceInfo.deviceType
166    tGeneralInfo.brand = deviceInfo.brand
167    tGeneralInfo.version = deviceInfo.displayVersion
168    tGeneralInfo.sn = deviceInfo.serial
169    database.insertGeneraData("t_general_info", tGeneralInfo)
170    createFilePath(globalThis.abilityContext.getApplicationContext().filesDir + "/" + globalThis.dbTime + "/t_general_info.csv", csvGeneralInfo(tGeneralInfo))
171
172    SPLogger.DEBUG(ProfilerTask.name,'ProfilerTask taskStop called');
173  }
174  taskGetDubai(){
175    let  tPowers:Array<TPowerSensorInfo> =new Array()
176    SPLogger.INFO("TAG","resultSet query_applications_display-----startTime" + JSON.stringify(dateFormat(globalThis.startTime) ))
177    SPLogger.INFO("TAG","resultSet query_applications_display-----endTime" + JSON.stringify(dateFormat(globalThis.endTime)))
178    SPLogger.INFO("TAG","resultSet query_applications_display-----dbTime" + JSON.stringify(globalThis.dbTime))
179   database.query_applications_display(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
180     if(data.length!=0)
181      tPowers.push(data[0])
182      database.query_applications_cpu(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
183        if(data.length!=0)
184        tPowers.push(data[0])
185        database.query_applications_gpu(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
186          if(data.length!=0)
187          tPowers.push(data[0])
188          database.query_applications_audio(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
189            if(data.length!=0)
190            tPowers.push(data[0])
191            database.query_applications_ddr(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
192              if(data.length!=0)
193              tPowers.push(data[0])
194              database.query_applications_dss(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
195                if(data.length!=0)
196                tPowers.push(data[0])
197                database.query_applications_system_idle(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
198                  if(data.length!=0)
199                  tPowers.push(data[0])
200                  database.query_applications_wifi_data(dateFormat(globalThis.startTime) , dateFormat(globalThis.endTime),globalThis.packageName).then(data => {
201                    if(data.length!=0)
202                    tPowers.push(data[0])
203                    for (var i = 0; i < tPowers.length; i++) {
204                      database.insertPowerSensor("task_powersensor_info",globalThis.dbTime,tPowers[i])
205                    }
206                  }).then(()=>{
207                     return database.query_applications_power_info(dateFormat(globalThis.startTime), dateFormat(globalThis.endTime))
208                  }).then((tArr:Array<TPowerAppInfo>)=>{
209                    tArr.forEach(t=>{
210                      database.insertPowerAppInfo("task_powerapp_info",globalThis.dbTime,t)
211                    })
212                  })
213                })
214              })
215            })
216          })
217        })
218      })
219    })
220  }
221  taskDestroy() {
222    //数据库时间戳标记
223    globalThis.dbTime = -1
224    //开始时间
225    globalThis.startTime = -1
226    //结束时间
227    globalThis.endTime = -1
228    //入库数据
229    globalThis.tTndexs = new Array<TIndexInfo>()
230
231    //采集配置恢复fileOpen
232    globalThis.collectConfigs = -1
233    globalThis.collectPkg = -1
234
235    //采集定时器 配置
236    globalThis.collectInterval = -1
237    globalThis.collectIntervalCollect = -1
238    globalThis.collectPowerCollect = -1
239    globalThis.collectIntervalNum = -1
240
241    globalThis.powerValue = 0
242
243    //socket采集队列 fps ram
244    globalThis.fpsArr = new Array<String>()
245
246    // power 电流队列
247    globalThis.powerCurArr = new Array<String>()
248
249    // power 电压队列
250    globalThis.powerVoltArr = new Array<String>()
251    globalThis.fpsJitterArr = new Array<String>()
252    globalThis.ramArr = new Array<String>()
253  }
254}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270