Created
October 11, 2017 15:13
-
-
Save fletcherist/69a396df380355e49695ecb2fa71e84f to your computer and use it in GitHub Desktop.
Revisions
-
fletcherist created this gist
Oct 11, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ ((name, context = window, func) => { context[name] = func() }) ('CpuMeter', this, () => { const isEmptyObject = object => Object.keys(object).length === 0 const getProcessorUsage = (usage, oldUsage) => Math.floor((usage.kernel + usage.user - oldUsage.kernel - oldUsage.user) / (usage.total - oldUsage.total) * 100) class CpuMeter { constructor() { if (!chrome || !chrome.system || !chrome.system.cpu) { throw new Error(`No access to chrome.system.cpu! Please allow permission (system.cpu) in your manifest.json`) } this.cpuInfo = {} this.previousCpuInfo = {} } getInfo(callback) { this._update().then(info => { callback({ cpuUsage: this.getCpuUsage() }) }) } getCpuUsage() { return this.cpuInfo.processors .reduce((acc, processor, index) => [...acc, getProcessorUsage( processor.usage, this.previousCpuInfo.processors[index].usage)], []) .reduce((acc, cpuUsage) => acc + cpuUsage, 0) / this.cpuInfo.processors.length } _update() { return new Promise(resolve => chrome.system.cpu.getInfo(cpuInfo => { this.previousCpuInfo = this.cpuInfo if (isEmptyObject(this.previousCpuInfo)) { this.previousCpuInfo = cpuInfo } this.cpuInfo = cpuInfo resolve(cpuInfo) })) } } return CpuMeter }, this)