فهرست منبع

feat: Use performance hooks for job stats and calculate total average time

Owen Diffey 1 سال پیش
والد
کامیت
17d1c8d6b8
2فایلهای تغییر یافته به همراه24 افزوده شده و 19 حذف شده
  1. 3 3
      backend/src/Job.ts
  2. 21 16
      backend/src/JobStatistics.ts

+ 3 - 3
backend/src/Job.ts

@@ -106,7 +106,7 @@ export default class Job {
 			}
 		);
 		this._status = JobStatus.QUEUED;
-		this._createdAt = Date.now();
+		this._createdAt = performance.now();
 	}
 
 	/**
@@ -171,7 +171,7 @@ export default class Job {
 	public async execute() {
 		if (this._startedAt) throw new Error("Job has already been executed.");
 		this._setStatus(JobStatus.ACTIVE);
-		this._startedAt = Date.now();
+		this._startedAt = performance.now();
 		return (
 			this._jobFunction
 				.apply(this._module, [this._context, this._payload])
@@ -197,7 +197,7 @@ export default class Job {
 					throw err;
 				})
 				.finally(() => {
-					this._completedAt = Date.now();
+					this._completedAt = performance.now();
 					this._jobStatistics.updateStats(this.getName(), "total");
 					if (this._startedAt)
 						this._jobStatistics.updateStats(

+ 21 - 16
backend/src/JobStatistics.ts

@@ -22,24 +22,29 @@ export default class JobStatistics {
 	 * @returns Job queue statistics
 	 */
 	public getStats() {
+		const stats = Object.values(this._stats);
+
+		const total = stats.reduce(
+			(a, b) => ({
+				successful: a.successful + b.successful,
+				failed: a.failed + b.failed,
+				total: a.total + b.total,
+				added: a.added + b.added,
+				averageTime: a.averageTime + b.averageTime
+			}),
+			{
+				successful: 0,
+				failed: 0,
+				total: 0,
+				added: 0,
+				averageTime: 0
+			}
+		);
+		total.averageTime /= stats.length;
+
 		return {
 			...this._stats,
-			total: Object.values(this._stats).reduce(
-				(a, b) => ({
-					successful: a.successful + b.successful,
-					failed: a.failed + b.failed,
-					total: a.total + b.total,
-					added: a.added + b.added,
-					averageTime: -1
-				}),
-				{
-					successful: 0,
-					failed: 0,
-					total: 0,
-					added: 0,
-					averageTime: -1
-				}
-			)
+			total
 		};
 	}