-
Notifications
You must be signed in to change notification settings - Fork 544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix!: host-metrics system.cpu.utilization
calculation fix
#1741
Changes from 9 commits
5a65dd0
69b8bbf
b1b240e
b1163ac
685788b
dec0119
44961f5
e3c4668
8d403e5
46bf903
c60197d
cea9d6c
9dc836b
1c92ae9
7603e56
768d62d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,10 @@ const mockedOS = { | |
totalmem: function () { | ||
return 1024 * 1024; | ||
}, | ||
cpusIdx: 0, | ||
cpus: function () { | ||
return cpuJson[this.cpusIdx++ % 2]; | ||
}, | ||
}; | ||
|
||
const INTERVAL = 3000; | ||
|
@@ -112,7 +116,7 @@ describe('Host Metrics', () => { | |
return mockedOS.freemem(); | ||
}); | ||
sandbox.stub(os, 'totalmem').returns(mockedOS.totalmem()); | ||
sandbox.stub(os, 'cpus').returns(cpuJson); | ||
sandbox.stub(os, 'cpus').callsFake(() => mockedOS.cpus()); | ||
sandbox.stub(process, 'uptime').returns(0); | ||
sandbox.stub(SI, 'networkStats').callsFake(() => { | ||
return mockedSI.networkStats(); | ||
|
@@ -146,6 +150,9 @@ describe('Host Metrics', () => { | |
await reader.collect(); | ||
dateStub.returns(process.uptime() * 1000 + INTERVAL); | ||
|
||
// advance the clock for the next collection | ||
sandbox.clock.tick(1000); | ||
|
||
// invalidates throttles | ||
countSI = 0; | ||
}); | ||
|
@@ -156,31 +163,31 @@ describe('Host Metrics', () => { | |
it('should export CPU time metrics', async () => { | ||
const metric = await getRecords(reader, 'system.cpu.time'); | ||
|
||
ensureValue(metric, { state: 'user', cpu: '0' }, 90713.56); | ||
ensureValue(metric, { state: 'system', cpu: '0' }, 63192.630000000005); | ||
ensureValue(metric, { state: 'idle', cpu: '0' }, 374870.7); | ||
ensureValue(metric, { state: 'user', cpu: '0' }, 90714.26); | ||
dyladan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ensureValue(metric, { state: 'system', cpu: '0' }, 63192.83); | ||
ensureValue(metric, { state: 'idle', cpu: '0' }, 374870.8); | ||
ensureValue(metric, { state: 'interrupt', cpu: '0' }, 0); | ||
ensureValue(metric, { state: 'nice', cpu: '0' }, 0); | ||
|
||
ensureValue(metric, { state: 'user', cpu: '1' }, 11005.42); | ||
ensureValue(metric, { state: 'system', cpu: '1' }, 7678.12); | ||
ensureValue(metric, { state: 'idle', cpu: '1' }, 510034.8); | ||
ensureValue(metric, { state: 'user', cpu: '1' }, 11005.72); | ||
ensureValue(metric, { state: 'system', cpu: '1' }, 7678.62); | ||
ensureValue(metric, { state: 'idle', cpu: '1' }, 510035); | ||
ensureValue(metric, { state: 'interrupt', cpu: '1' }, 0); | ||
ensureValue(metric, { state: 'nice', cpu: '1' }, 0); | ||
}); | ||
|
||
it('should export CPU utilization metrics', async () => { | ||
const metric = await getRecords(reader, 'system.cpu.utilization'); | ||
|
||
ensureValue(metric, { state: 'user', cpu: '0' }, 30247.935978659552); | ||
ensureValue(metric, { state: 'system', cpu: '0' }, 21071.23374458153); | ||
ensureValue(metric, { state: 'idle', cpu: '0' }, 124998.56618872957); | ||
ensureValue(metric, { state: 'user', cpu: '0' }, 0.7); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the value rounded or is this just a coincidence that they're all 1 significant figure? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a coincidence. The data is mocked with |
||
ensureValue(metric, { state: 'system', cpu: '0' }, 0.2); | ||
ensureValue(metric, { state: 'idle', cpu: '0' }, 0.1); | ||
ensureValue(metric, { state: 'interrupt', cpu: '0' }, 0); | ||
ensureValue(metric, { state: 'nice', cpu: '0' }, 0); | ||
|
||
ensureValue(metric, { state: 'user', cpu: '1' }, 3669.6965655218405); | ||
ensureValue(metric, { state: 'system', cpu: '1' }, 2560.2267422474156); | ||
ensureValue(metric, { state: 'idle', cpu: '1' }, 170068.28942980993); | ||
ensureValue(metric, { state: 'user', cpu: '1' }, 0.3); | ||
ensureValue(metric, { state: 'system', cpu: '1' }, 0.5); | ||
ensureValue(metric, { state: 'idle', cpu: '1' }, 0.2); | ||
ensureValue(metric, { state: 'interrupt', cpu: '1' }, 0); | ||
ensureValue(metric, { state: 'nice', cpu: '1' }, 0); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,54 @@ | ||
[{ | ||
"model": "CPU @ 2.60GHz", | ||
"speed": 2600, | ||
"times": { | ||
"user": 90713560, | ||
"nice": 0, | ||
"sys": 63192630, | ||
"idle": 374870700, | ||
"irq": 0 | ||
} | ||
}, { | ||
"model": "CPU @ 2.60GHz", | ||
"speed": 2600, | ||
"times": { | ||
"user": 11005420, | ||
"nice": 0, | ||
"sys": 7678120, | ||
"idle": 510034800, | ||
"irq": 0 | ||
} | ||
} | ||
] | ||
[ | ||
[ | ||
{ | ||
"model": "CPU @ 2.60GHz", | ||
"speed": 2600, | ||
"// times": "this is the 1st collect of CPU data which will be used to calc utilization", | ||
"times": { | ||
"user": 90713560, | ||
"nice": 0, | ||
"sys": 63192630, | ||
"idle": 374870700, | ||
"irq": 0 | ||
} | ||
}, | ||
{ | ||
"model": "CPU @ 2.60GHz", | ||
"speed": 2600, | ||
"// times": "this is the 1st collect of CPU data which will be used to calc utilization", | ||
"times": { | ||
"user": 11005420, | ||
"nice": 0, | ||
"sys": 7678120, | ||
"idle": 510034800, | ||
"irq": 0 | ||
} | ||
} | ||
], | ||
[ | ||
{ | ||
"model": "CPU @ 2.60GHz", | ||
"speed": 2600, | ||
"// times": "assuming a time delta of 1000ms we distribute 70% to user, 20% to sys and 10% to idle", | ||
"times": { | ||
"user": 90714260, | ||
"nice": 0, | ||
"sys": 63192830, | ||
"idle": 374870800, | ||
"irq": 0 | ||
} | ||
}, | ||
{ | ||
"model": "CPU @ 2.60GHz", | ||
"speed": 2600, | ||
"// times": "assuming a time delta of 1000ms we distribute 30% to user, 50% to sys and 20% to idle", | ||
"times": { | ||
"user": 11005720, | ||
"nice": 0, | ||
"sys": 7678620, | ||
"idle": 510035000, | ||
"irq": 0 | ||
} | ||
} | ||
] | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be possible to load cpus at startup and calculate the load from then? It would miss the first few milliseconds, but would avoid having a
0
measurement on each program restart.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would make testing harder but it is possible. I'll give it a try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Luckily the test is setup in a way that is not affected by this data preload. The changes are pushed :)