-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Reporting] Add logging of CPU usage and memory consumption by Chromium in the reporting. #99109
Changes from all commits
c2c1838
665e37d
920f864
e45faa5
3f8ce24
4d7f9f2
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
jest.mock('os'); | ||
|
||
import { cpus } from 'os'; | ||
import { Metrics, getMetrics } from './metrics'; | ||
|
||
describe('getMetrics', () => { | ||
const start = { | ||
metrics: [ | ||
{ name: 'ProcessTime', value: 10 }, | ||
{ name: 'Timestamp', value: 1000 }, | ||
{ name: 'JSHeapTotalSize', value: 1 * 1024 * 1024 }, | ||
], | ||
} as Metrics; | ||
const end = { | ||
metrics: [ | ||
{ name: 'ProcessTime', value: 110 }, | ||
{ name: 'Timestamp', value: 2000 }, | ||
{ name: 'JSHeapTotalSize', value: 5 * 1024 * 1024 }, | ||
], | ||
} as Metrics; | ||
|
||
beforeEach(() => { | ||
(cpus as jest.MockedFunction<typeof cpus>).mockReturnValue([{} as any]); | ||
}); | ||
|
||
describe('cpu', () => { | ||
it('should evaluate CPU usage during the runtime', () => { | ||
const { cpu } = getMetrics(start, end); | ||
|
||
expect(cpu).toBe(0.1); | ||
}); | ||
|
||
it('should respect a number of virtual cores available', () => { | ||
(cpus as jest.MockedFunction<typeof cpus>).mockReturnValue([{} as any, {} as any]); | ||
const { cpu } = getMetrics(start, end); | ||
|
||
expect(cpu).toBe(0.05); | ||
}); | ||
|
||
it('should return CPU usage in percentage', () => { | ||
const { cpuInPercentage } = getMetrics(start, end); | ||
|
||
expect(cpuInPercentage).toBe(10); | ||
}); | ||
}); | ||
|
||
describe('memory', () => { | ||
it('should evaluate memory consumption during the runtime', () => { | ||
const { memory } = getMetrics(start, end); | ||
|
||
expect(memory).toBe(5 * 1024 * 1024); | ||
}); | ||
|
||
it('should return memory consumption in megabytes', () => { | ||
const { memoryInMegabytes } = getMetrics(start, end); | ||
|
||
expect(memoryInMegabytes).toBe(5); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { Metrics as PuppeteerMetrics } from 'puppeteer'; | ||
import { cpus } from 'os'; | ||
|
||
declare module 'puppeteer' { | ||
interface CDPSession { | ||
send(command: 'Performance.getMetrics'): Promise<RawMetrics>; | ||
} | ||
} | ||
|
||
type RawMetrics = Metrics; | ||
|
||
export interface Metrics { | ||
metrics: Metric[]; | ||
} | ||
|
||
interface Metric { | ||
name: keyof NormalizedMetrics; | ||
value: unknown; | ||
} | ||
|
||
interface NormalizedMetrics extends Required<PuppeteerMetrics> { | ||
ProcessTime: number; | ||
} | ||
|
||
interface PerformanceMetrics { | ||
cpu: number; | ||
cpuInPercentage: number; | ||
memory: number; | ||
memoryInMegabytes: number; | ||
} | ||
|
||
function normalizeMetrics({ metrics }: Metrics) { | ||
return (Object.fromEntries( | ||
metrics.map(({ name, value }) => [name, value]) | ||
) as unknown) as NormalizedMetrics; | ||
} | ||
|
||
function getCpuUsage(start: NormalizedMetrics, end: NormalizedMetrics) { | ||
return (end.ProcessTime - start.ProcessTime) / (end.Timestamp - start.Timestamp) / cpus().length; | ||
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. What is the difference between 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.
Sometimes when the process was in the idle state, the |
||
} | ||
|
||
function toPercentage(value: number) { | ||
return Math.round((value + Number.EPSILON) * 10000) / 100; | ||
} | ||
|
||
function toMegabytes(value: number) { | ||
return Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100; | ||
} | ||
|
||
export function getMetrics(start: Metrics, end: Metrics): PerformanceMetrics { | ||
const startMetrics = normalizeMetrics(start); | ||
const endMetrics = normalizeMetrics(end); | ||
|
||
const cpu = getCpuUsage(startMetrics, endMetrics); | ||
const cpuInPercentage = toPercentage(cpu); | ||
const { JSHeapTotalSize: memory } = endMetrics; | ||
const memoryInMegabytes = toMegabytes(memory); | ||
|
||
return { | ||
cpu, | ||
cpuInPercentage, | ||
memory, | ||
memoryInMegabytes, | ||
}; | ||
} |
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.
Was this added because the code returns a different object than is defined by the TS type?
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.
In the
puppeter
' type definitions they returnobject
for thesend
method which is too generic and doesn't provide any type safety. The declaration here doesn't change the original signature but adds another override for that method.