fix: 过程数据写入文件,压测0.1秒同时写入8个文件,写1000次,cpu最大波动5%
This commit is contained in:
		
							parent
							
								
									f0653c0c92
								
							
						
					
					
						commit
						baa6b80556
					
				
							
								
								
									
										1
									
								
								entry/src/main/ets/mock/Test.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								entry/src/main/ets/mock/Test.ets
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | export const ProcessDataMock:string="#DN_GD,$PLC,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-0_0,0,0,0,0,0,0,0,0,511,0,0,0,0,0,0,0,0,0,0,0,0,15802,0,1,0_0_0_0,0.0.0.0,V9.1_V2.1_1,65535,0,0,0/0,15802/292,0,0/0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,#END$GPS,1,16,6.068,1.301,0.000,1.07,0.64,+0.0000,+0.0000,0,091022,080109.20,3155.6715075,11851.9196718,0.012,#END" | ||||||
| @ -6,4 +6,5 @@ export * from "./Judge" | |||||||
| 
 | 
 | ||||||
| export * from "./SignDisplay" | export * from "./SignDisplay" | ||||||
| 
 | 
 | ||||||
| export * from "./CarCheck" | export * from "./CarCheck" | ||||||
|  | export * from "./Test" | ||||||
| @ -32,7 +32,7 @@ export interface LogWorkerMessage { | |||||||
|   type: WorkerMessageType; |   type: WorkerMessageType; | ||||||
|   data?: string; |   data?: string; | ||||||
|   studentDirName?: string; |   studentDirName?: string; | ||||||
|   processDataType?:ProcessDataEnumType |   processDataType?: ProcessDataEnumType | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| //枚举 | //枚举 | ||||||
| @ -49,28 +49,33 @@ export enum WorkerMessageType { | |||||||
|   ExamProcessInit = 'examProcessInit', |   ExamProcessInit = 'examProcessInit', | ||||||
|   //   写过程数据 |   //   写过程数据 | ||||||
|   WriteProcessData = 'writeProcessData', |   WriteProcessData = 'writeProcessData', | ||||||
| //   关闭过程数据 |   //   关闭过程数据 | ||||||
|   CloseProcessData='closeProcessData' |   CloseProcessData = 'closeProcessData' | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 过程数据枚举 | // 过程数据枚举 | ||||||
| export enum ProcessDataEnumType { | export enum ProcessDataEnumType { | ||||||
|   //four_one_log_byte_data |   //four_one_log_byte_data | ||||||
|   FourOneLogByteData = 0, |   FourOneLogByteData = "0", | ||||||
|   //four_one_log_data |   //four_one_log_data | ||||||
|   FourOneLogData = 1, |   FourOneLogData = "1", | ||||||
|   //judge_exam_data |   //judge_exam_data | ||||||
|   JudgeExamData = 2, |   JudgeExamData = "2", | ||||||
|   //judge_log_data |   //judge_log_data | ||||||
|   JudgeLogData = 3, |   JudgeLogData = "3", | ||||||
|   //   judge_progress_callback_data |   //   judge_progress_callback_data | ||||||
|   JudgeProgressCallbackData = 4, |   JudgeProgressCallbackData = "4", | ||||||
|   //plc_data |   //plc_data | ||||||
|   PlcData = 5, |   PlcData = "5", | ||||||
|   //wuxi_exam_data |   //wuxi_exam_data | ||||||
|   WuxiExam = 6, |   WuxiExam = "6", | ||||||
|   //wuxi_progress_data |   //wuxi_progress_data | ||||||
|   WuxiProgressData = 7 |   WuxiProgressData = "7" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export interface FileQueueType { | ||||||
|  |   type: ProcessDataEnumType, | ||||||
|  |   data: string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface LogPathType { | export interface LogPathType { | ||||||
|  | |||||||
| @ -45,7 +45,7 @@ import dayTs from '../utils/Date'; | |||||||
| import { GetCurrentTime } from '../utils/Common'; | import { GetCurrentTime } from '../utils/Common'; | ||||||
| import FileUtils from '../utils/FileUtils'; | import FileUtils from '../utils/FileUtils'; | ||||||
| import SignalTrajectoryDialog from './Judge/SignalTrajectoryDialog'; | import SignalTrajectoryDialog from './Judge/SignalTrajectoryDialog'; | ||||||
| import { DefaultJudgeConfigData } from '../mock'; | import { DefaultJudgeConfigData, ProcessDataMock } from '../mock'; | ||||||
| import { dConsole } from '../utils/LogWorker'; | import { dConsole } from '../utils/LogWorker'; | ||||||
| import ConfirmDialog from './compontents/ConfirmDialog'; | import ConfirmDialog from './compontents/ConfirmDialog'; | ||||||
| import CarLoadingDialog from './compontents/CarLoading'; | import CarLoadingDialog from './compontents/CarLoading'; | ||||||
| @ -206,7 +206,28 @@ struct JudgePage { | |||||||
|     this.startFullTime = GetCurrentTime(1); |     this.startFullTime = GetCurrentTime(1); | ||||||
|     this.startTime = dayTs().format("YYYY-MM-DD HH:mm:ss") |     this.startTime = dayTs().format("YYYY-MM-DD HH:mm:ss") | ||||||
|     this.startExamTime = dayTs().format("YYYY-MM-DD HH:mm:ss") |     this.startExamTime = dayTs().format("YYYY-MM-DD HH:mm:ss") | ||||||
| 
 |     // let count = 0; | ||||||
|  |     // const totalTimes = 1000; | ||||||
|  |     // const intervalTime = 100; // 0.1秒 = 100毫秒 | ||||||
|  |     // | ||||||
|  |     // const timer = setInterval(() => { | ||||||
|  |     //   if (count < totalTimes) { | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.FourOneLogByteData, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.FourOneLogData, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.JudgeExamData, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.JudgeLogData, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.JudgeProgressCallbackData, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.PlcData, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.WuxiExam, ProcessDataMock); | ||||||
|  |     //     dConsole.writeProcessData(ProcessDataEnumType.WuxiProgressData, ProcessDataMock); | ||||||
|  |     //     count++; | ||||||
|  |     //     console.log(`已调用 ${count} 次`); | ||||||
|  |     //   } else { | ||||||
|  |     //     dConsole.closeProcessData() | ||||||
|  |     //     clearInterval(timer); | ||||||
|  |     //     console.log('已完成1000次调用'); | ||||||
|  |     //   } | ||||||
|  |     // }, intervalTime); | ||||||
|     //初始化数据库表 |     //初始化数据库表 | ||||||
|     await this.initDb() |     await this.initDb() | ||||||
|     //断点续考 |     //断点续考 | ||||||
| @ -718,6 +739,7 @@ struct JudgePage { | |||||||
|           singlePlay: this.singlePlay, |           singlePlay: this.singlePlay, | ||||||
|           judgeConfigObj: this.judgeConfigObj, |           judgeConfigObj: this.judgeConfigObj, | ||||||
|           signalViewingClick: () => { |           signalViewingClick: () => { | ||||||
|  |             dConsole.writeProcessData(ProcessDataEnumType.FourOneLogByteData, '测试测试') | ||||||
|             this.signalTrajectoryDialogController.open() |             this.signalTrajectoryDialogController.open() | ||||||
|           }, |           }, | ||||||
|           artificialEvaluationClick: () => { |           artificialEvaluationClick: () => { | ||||||
|  | |||||||
| @ -223,11 +223,9 @@ export function CreateDir(path: string): Promise<boolean> { | |||||||
| export function EditFile(fd: number, data: string): Promise<boolean> { | export function EditFile(fd: number, data: string): Promise<boolean> { | ||||||
|   return new Promise((resolve, reject) => { |   return new Promise((resolve, reject) => { | ||||||
|     try { |     try { | ||||||
|       let result = fs.writeSync(fd, data + "\n") |       fs.writeSync(fd, data + "\n") | ||||||
|       console.log("创建文件之写文件成功", result.toString()) |  | ||||||
|       resolve(true) |       resolve(true) | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|       console.log("创建文件之写文件失败") |  | ||||||
|       reject(false) |       reject(false) | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
|  | |||||||
| @ -74,6 +74,15 @@ class logWorker { | |||||||
|       this.workerInstance?.postMessage(JSON.stringify(params)) |       this.workerInstance?.postMessage(JSON.stringify(params)) | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |   // 关闭过程数据写入 | ||||||
|  |   closeProcessData(){ | ||||||
|  |     let params: LogWorkerMessage = { | ||||||
|  |       type: WorkerMessageType.CloseProcessData, | ||||||
|  |     } | ||||||
|  |     if (this.isLogEnabled === "1") { | ||||||
|  |       this.workerInstance?.postMessage(JSON.stringify(params)) | ||||||
|  |     } | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   // 通用日志方法 |   // 通用日志方法 | ||||||
|   private logWithLevel(level: 'log' | 'info' | 'error', ...args: ESObject[]): void { |   private logWithLevel(level: 'log' | 'info' | 'error', ...args: ESObject[]): void { | ||||||
|  | |||||||
| @ -1,13 +1,22 @@ | |||||||
| import worker, { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker'; | import worker, { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker'; | ||||||
| import { GlobalConfig } from '../config'; | import { GlobalConfig } from '../config'; | ||||||
| import { LogFileFd, LogPathType, LogWorkerMessage, ProcessDataEnumType, WorkerMessageType } from '../model'; | import { | ||||||
|  |   FileQueueType, | ||||||
|  |   LogFileFd, | ||||||
|  |   LogPathType, | ||||||
|  |   LogWorkerMessage, | ||||||
|  |   ProcessDataEnumType, | ||||||
|  |   WorkerMessageType | ||||||
|  | } from '../model'; | ||||||
| import { CreateDir, CreateFile, EditFile, IsExit } from '../utils/Common'; | import { CreateDir, CreateFile, EditFile, IsExit } from '../utils/Common'; | ||||||
| import dayTs from '../utils/Date'; | import dayTs from '../utils/Date'; | ||||||
| import fs from '@ohos.file.fs'; | import fs from '@ohos.file.fs'; | ||||||
| 
 | 
 | ||||||
| const workerPort: ThreadWorkerGlobalScope = worker.workerPort; | const workerPort: ThreadWorkerGlobalScope = worker.workerPort; | ||||||
| const fileNameArr: string[] = ["four_one_log_byte_data.text", "four_one_log_data.txt", "judge_exam_data.txt", "judge_log_data.txt", "judge_progress_callback_data.txt", "plc_data.txt", "wuxi_exam_data.txt", "wuxi_progress_data.txt"] | const fileNameArr: string[] = ["four_one_log_byte_data.txt", "four_one_log_data.txt", "judge_exam_data.txt", "judge_log_data.txt", "judge_progress_callback_data.txt", "plc_data.txt", "wuxi_exam_data.txt", "wuxi_progress_data.txt"] | ||||||
| let fileFdArr: number[] = [] | let fileFdArr: number[] = [] | ||||||
|  | let writeQueue: Array<FileQueueType> = []; | ||||||
|  | let isProcessing = false; | ||||||
| workerPort.onmessage = (e: MessageEvents) => { | workerPort.onmessage = (e: MessageEvents) => { | ||||||
|   console.log("日志系统worker收到消息") |   console.log("日志系统worker收到消息") | ||||||
|   // 日志文件目录 |   // 日志文件目录 | ||||||
| @ -15,7 +24,6 @@ workerPort.onmessage = (e: MessageEvents) => { | |||||||
|     log: `${GlobalConfig.commonFileWriteAddress}/logs/${dayTs().format("YYYY_MM_DD")}/log/log.log`, |     log: `${GlobalConfig.commonFileWriteAddress}/logs/${dayTs().format("YYYY_MM_DD")}/log/log.log`, | ||||||
|     error: `${GlobalConfig.commonFileWriteAddress}/logs/${dayTs().format("YYYY_MM_DD")}/log/error.log`, |     error: `${GlobalConfig.commonFileWriteAddress}/logs/${dayTs().format("YYYY_MM_DD")}/log/error.log`, | ||||||
|   } |   } | ||||||
|   let logFileFd: LogFileFd = {} |  | ||||||
|   const result: LogWorkerMessage = JSON.parse(e.data); |   const result: LogWorkerMessage = JSON.parse(e.data); | ||||||
|   if (result.type === WorkerMessageType.Init) { |   if (result.type === WorkerMessageType.Init) { | ||||||
|     InitLog() |     InitLog() | ||||||
| @ -25,8 +33,8 @@ workerPort.onmessage = (e: MessageEvents) => { | |||||||
|     InitExam(result.studentDirName) |     InitExam(result.studentDirName) | ||||||
|   } |   } | ||||||
|   // 写过程数据 |   // 写过程数据 | ||||||
|   if (result.type === WorkerMessageType.WriteProcessData&&result.processDataType) { |   if (result.type === WorkerMessageType.WriteProcessData && result.processDataType) { | ||||||
|     WriteProcessData(result.processDataType, result.data||"") |     WriteProcessData(result.processDataType, result.data || "") | ||||||
|   } |   } | ||||||
|   //   关闭过程数据 |   //   关闭过程数据 | ||||||
|   if (result.type === WorkerMessageType.CloseProcessData) { |   if (result.type === WorkerMessageType.CloseProcessData) { | ||||||
| @ -70,7 +78,7 @@ async function InitLog() { | |||||||
| 
 | 
 | ||||||
| // 初始化学员过程数据目录 | // 初始化学员过程数据目录 | ||||||
| async function InitExam(dirName: string) { | async function InitExam(dirName: string) { | ||||||
|   console.log("初始化过程",dirName) |   console.log("初始化过程", dirName) | ||||||
|   let date = dayTs().format("YYYY_MM_DD") |   let date = dayTs().format("YYYY_MM_DD") | ||||||
|   const path = `${GlobalConfig.commonFileWriteAddress}/logs/${date}/${dirName}` |   const path = `${GlobalConfig.commonFileWriteAddress}/logs/${date}/${dirName}` | ||||||
|   const nameDirIsExit = await IsExit(path) |   const nameDirIsExit = await IsExit(path) | ||||||
| @ -88,21 +96,29 @@ async function InitExam(dirName: string) { | |||||||
| 
 | 
 | ||||||
| // 写过程数据 | // 写过程数据 | ||||||
| async function WriteProcessData(type: ProcessDataEnumType, data: string) { | async function WriteProcessData(type: ProcessDataEnumType, data: string) { | ||||||
|   let index = Number(type) |   writeQueue.push({ type, data }); | ||||||
|   console.log("创建文件",type,index.toString(),data) | 
 | ||||||
|   let result=await EditFile(fileFdArr[index], data) |   if (!isProcessing) { | ||||||
|   if(result){ |     processQueue(); | ||||||
|   //   写文件成功 |  | ||||||
|   }else{ |  | ||||||
|   //   写文件失败 |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | async function processQueue() { | ||||||
|  |   isProcessing = true; | ||||||
|  |   while (writeQueue.length > 0) { | ||||||
|  |     const item = writeQueue.shift()!; | ||||||
|  |     const index = Number(item.type); | ||||||
|  |     await EditFile(fileFdArr[index], item.data); | ||||||
|  |   } | ||||||
|  |   isProcessing = false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // 关闭过程数据 | // 关闭过程数据 | ||||||
| function CloseProcessData() { | function CloseProcessData() { | ||||||
|   fileFdArr.forEach((item: number) => { |   fileFdArr.forEach((item: number) => { | ||||||
|     fs.closeSync(item) |     fs.closeSync(item) | ||||||
|   }) |   }) | ||||||
|  |   console.log("关闭过程数据") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| workerPort.onmessageerror = (e: MessageEvents) => { | workerPort.onmessageerror = (e: MessageEvents) => { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user