Skip to content

Commit

Permalink
feat: add cumulativeCPUUsage to AppMetrics and CPUUsage
Browse files Browse the repository at this point in the history
This allows apps to measure their CPU usage over any given period
without worrying about other calls affecting the output,
as they would with `percentCPUUsage`.
  • Loading branch information
rcombs committed Apr 6, 2024
1 parent 42164d7 commit 3e93a90
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/structures/cpu-usage.md
Expand Up @@ -2,6 +2,7 @@

* `percentCPUUsage` number - Percentage of CPU used since the last call to getCPUUsage.
First call returns 0.
* `cumulativeCPUUsage` number - Total seconds of CPU time used since process startup.
* `idleWakeupsPerSecond` number - The number of average idle CPU wakeups per second
since the last call to getCPUUsage. First call returns 0. Will always return 0 on
Windows.
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_app.cc
Expand Up @@ -1280,6 +1280,7 @@ std::vector<gin_helper::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) {
std::optional<double> usage =
process_metric.second->metrics->GetPlatformIndependentCPUUsage();
cpu_dict.Set("percentCPUUsage", usage.value_or(0) / processor_count);
cpu_dict.Set("cumulativeCPUUsage", process_metric.second->metrics->GetCumulativeCPUUsage().InSecondsF());

#if !BUILDFLAG(IS_WIN)
cpu_dict.Set("idleWakeupsPerSecond",
Expand Down
2 changes: 2 additions & 0 deletions shell/common/api/electron_bindings.cc
Expand Up @@ -280,6 +280,8 @@ v8::Local<v8::Value> ElectronBindings::GetCPUUsage(
std::optional<double> usage = metrics->GetPlatformIndependentCPUUsage();
dict.Set("percentCPUUsage", usage.value_or(0) / processor_count);

dict.Set("cumulativeCPUUsage", metrics->GetCumulativeCPUUsage().InSecondsF());

// NB: This will throw NOTIMPLEMENTED() on Windows
// For backwards compatibility, we'll return 0
#if !BUILDFLAG(IS_WIN)
Expand Down
1 change: 1 addition & 0 deletions spec/api-app-spec.ts
Expand Up @@ -1442,6 +1442,7 @@ describe('app module', () => {

types.push(entry.type);
expect(entry.cpu).to.have.ownProperty('percentCPUUsage').that.is.a('number');
expect(entry.cpu).to.have.ownProperty('cumulativeCPUUsage').that.is.a('number');
expect(entry.cpu).to.have.ownProperty('idleWakeupsPerSecond').that.is.a('number');

expect(entry.memory).to.have.property('workingSetSize').that.is.greaterThan(0);
Expand Down
2 changes: 2 additions & 0 deletions spec/api-process-spec.ts
Expand Up @@ -26,6 +26,7 @@ describe('process module', () => {
it('returns a cpu usage object', async () => {
const cpuUsage = await w.webContents.executeJavaScript('process.getCPUUsage()');
expect(cpuUsage.percentCPUUsage).to.be.a('number');
expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
});
});
Expand Down Expand Up @@ -124,6 +125,7 @@ describe('process module', () => {
it('returns a cpu usage object', () => {
const cpuUsage = process.getCPUUsage();
expect(cpuUsage.percentCPUUsage).to.be.a('number');
expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
});
});
Expand Down

0 comments on commit 3e93a90

Please sign in to comment.