forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reporting] Add logging of CPU usage and memory consumption by Chromi…
…um (elastic#99109) * Add logging of CPU usage by chromium * Add logging of memory consumption by chromium * Add PDF report byte length logging * Add PNG report byte length logging # Conflicts: # x-pack/plugins/reporting/server/browsers/chromium/driver_factory/index.ts
- Loading branch information
Showing
8 changed files
with
222 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 characters
67 changes: 67 additions & 0 deletions
67
x-pack/plugins/reporting/server/browsers/chromium/driver_factory/metrics.test.ts
This file contains 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 characters
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); | ||
}); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/reporting/server/browsers/chromium/driver_factory/metrics.ts
This file contains 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 characters
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; | ||
} | ||
|
||
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, | ||
}; | ||
} |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters