|
| 1 | +import pidusage from 'pidusage'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { exec } from 'node:child_process'; |
| 5 | +import { promisify } from 'util'; |
| 6 | +import { clearInterval } from 'node:timers'; |
| 7 | +import { Logger } from './logger'; |
| 8 | + |
| 9 | +const execAsync = promisify(exec); |
| 10 | + |
| 11 | +interface UsageData { |
| 12 | + timestamp: string; |
| 13 | + cpu: number; |
| 14 | + memory: number; |
| 15 | +} |
| 16 | + |
| 17 | +class PidMonitor { |
| 18 | + private static _instance: PidMonitor; |
| 19 | + |
| 20 | + private pid?: number; |
| 21 | + private readonly intervalMs: number; |
| 22 | + private _data: UsageData[] = []; |
| 23 | + private timer?: ReturnType<typeof setInterval>; |
| 24 | + |
| 25 | + private constructor(intervalMs = 1000) { |
| 26 | + this.intervalMs = intervalMs; |
| 27 | + } |
| 28 | + |
| 29 | + public static getInstance(intervalMs = 1000): PidMonitor { |
| 30 | + if (!PidMonitor._instance) { |
| 31 | + PidMonitor._instance = new PidMonitor(intervalMs); |
| 32 | + } |
| 33 | + return PidMonitor._instance; |
| 34 | + } |
| 35 | + |
| 36 | + public get data(): UsageData[] { |
| 37 | + return this._data; |
| 38 | + } |
| 39 | + |
| 40 | + public async init(): Promise<boolean> { |
| 41 | + try { |
| 42 | + const { stdout } = await execAsync( |
| 43 | + "ps aux | grep '[c]hrome.*--extension-process.*--enable-automation.*--test-type=webdriver' | awk '{print $2}'" |
| 44 | + ); |
| 45 | + const pid = Number(stdout.trim()); |
| 46 | + |
| 47 | + if (Number.isNaN(pid)) { |
| 48 | + Logger.error(`Parsed PID is NaN from stdout: "${stdout}"`); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + this.pid = pid; |
| 53 | + return true; |
| 54 | + } catch (error) { |
| 55 | + Logger.error(`Error finding PID: ${error}`); |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public start(): void { |
| 61 | + if (this.pid === undefined) { |
| 62 | + Logger.warn('PID is not set. Call init() first.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (this.timer) return; |
| 67 | + |
| 68 | + this.timer = setInterval(async () => { |
| 69 | + if (this.pid === undefined) return; |
| 70 | + |
| 71 | + try { |
| 72 | + const stats = await pidusage(this.pid); |
| 73 | + this._data.push({ |
| 74 | + timestamp: new Date().toISOString(), |
| 75 | + cpu: stats.cpu, |
| 76 | + memory: stats.memory |
| 77 | + }); |
| 78 | + } catch (error) { |
| 79 | + Logger.error(`pidusage failed: ${error}`); |
| 80 | + this.stop(); |
| 81 | + } |
| 82 | + }, this.intervalMs); |
| 83 | + } |
| 84 | + |
| 85 | + public stop(): void { |
| 86 | + if (this.timer) { |
| 87 | + clearInterval(this.timer); |
| 88 | + this.timer = undefined; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public clear(): void { |
| 93 | + this._data = []; |
| 94 | + } |
| 95 | + |
| 96 | + public saveToFile(filePath: string): void { |
| 97 | + try { |
| 98 | + const dir = path.dirname(filePath); |
| 99 | + if (!fs.existsSync(dir)) { |
| 100 | + fs.mkdirSync(dir, { recursive: true }); |
| 101 | + } |
| 102 | + fs.writeFileSync(filePath, JSON.stringify(this._data, undefined, 2), 'utf-8'); |
| 103 | + } catch (error) { |
| 104 | + Logger.error(`Failed to save data to file: ${error}`); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export default PidMonitor; |
0 commit comments