-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for event loop utilization to the core metrics service (#…
- Loading branch information
1 parent
0d6ad68
commit 9517d06
Showing
18 changed files
with
305 additions
and
67 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
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
19 changes: 19 additions & 0 deletions
19
.../core-metrics-collectors-server-internal/src/event_loop_utilization_monitor.test.mocks.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,19 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const eventLoopUtilizationMock = jest.fn().mockImplementation(() => ({ | ||
active: 1, | ||
idle: 1, | ||
utilization: 1, | ||
})); | ||
|
||
jest.doMock('perf_hooks', () => ({ | ||
performance: { | ||
eventLoopUtilization: eventLoopUtilizationMock, | ||
}, | ||
})); |
79 changes: 79 additions & 0 deletions
79
...etrics/core-metrics-collectors-server-internal/src/event_loop_utilization_monitor.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,79 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { eventLoopUtilizationMock } from './event_loop_utilization_monitor.test.mocks'; | ||
import { EventLoopUtilizationMonitor } from './event_loop_utilization_monitor'; | ||
|
||
describe('EventLoopUtilizationMonitor', () => { | ||
afterEach(() => jest.clearAllMocks()); | ||
|
||
describe('#constructor', () => { | ||
test('#constructor collects utilization', () => { | ||
new EventLoopUtilizationMonitor(); | ||
expect(eventLoopUtilizationMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('#reset', () => { | ||
test('collects utilization', () => { | ||
const monitor = new EventLoopUtilizationMonitor(); | ||
monitor.reset(); | ||
expect(eventLoopUtilizationMock).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe('#collect', () => { | ||
test('collects utilization', () => { | ||
const monitor = new EventLoopUtilizationMonitor(); | ||
monitor.collect(); | ||
expect(eventLoopUtilizationMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test('returns values from call to performance.eventLoopUtilization', () => { | ||
const monitor = new EventLoopUtilizationMonitor(); | ||
expect(monitor.collect()).toMatchInlineSnapshot(` | ||
Object { | ||
"active": 1, | ||
"idle": 1, | ||
"utilization": 1, | ||
} | ||
`); | ||
}); | ||
|
||
test('passes last ELU value from constructor to calculate diff', () => { | ||
const mockInitialData = { | ||
active: 0, | ||
idle: 0, | ||
utilization: 0, | ||
}; | ||
eventLoopUtilizationMock.mockImplementationOnce(() => mockInitialData); | ||
|
||
const monitor = new EventLoopUtilizationMonitor(); | ||
monitor.collect(); | ||
|
||
expect(eventLoopUtilizationMock).toHaveBeenCalledTimes(2); | ||
expect(eventLoopUtilizationMock.mock.calls[1][0]).toEqual(mockInitialData); | ||
}); | ||
|
||
test('passes last ELU value from reset to calculate diff', () => { | ||
const monitor = new EventLoopUtilizationMonitor(); | ||
const mockInitialData = { | ||
active: 0, | ||
idle: 0, | ||
utilization: 0, | ||
}; | ||
eventLoopUtilizationMock.mockImplementationOnce(() => mockInitialData); | ||
|
||
monitor.reset(); | ||
monitor.collect(); | ||
|
||
expect(eventLoopUtilizationMock).toHaveBeenCalledTimes(3); | ||
expect(eventLoopUtilizationMock.mock.calls[2][0]).toEqual(mockInitialData); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
...ore/metrics/core-metrics-collectors-server-internal/src/event_loop_utilization_monitor.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,43 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { EventLoopUtilization } from 'perf_hooks'; | ||
import { performance } from 'perf_hooks'; | ||
|
||
export class EventLoopUtilizationMonitor { | ||
private elu: EventLoopUtilization; | ||
|
||
/** | ||
* Creating a new instance of EventLoopUtilizationMonitor will capture the | ||
* current ELU to use as a point of comparison against the first call to | ||
* `collect`. | ||
*/ | ||
constructor() { | ||
this.elu = performance.eventLoopUtilization(); | ||
} | ||
|
||
/** | ||
* Get ELU between now and last time the ELU was reset. | ||
*/ | ||
public collect(): EventLoopUtilization { | ||
const { active, idle, utilization } = performance.eventLoopUtilization(this.elu); | ||
|
||
return { | ||
active, | ||
idle, | ||
utilization, | ||
}; | ||
} | ||
|
||
/** | ||
* Resets the ELU to now. Will be used to calculate the diff on the next call to `collect`. | ||
*/ | ||
public reset() { | ||
this.elu = performance.eventLoopUtilization(); | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
packages/core/metrics/core-metrics-collectors-server-internal/src/process.test.mocks.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,19 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { collectorMock } from './mocks_internal'; | ||
|
||
export const mockEventLoopDelayMonitor = collectorMock.create(); | ||
jest.doMock('./event_loop_delays_monitor', () => ({ | ||
EventLoopDelaysMonitor: jest.fn().mockImplementation(() => mockEventLoopDelayMonitor), | ||
})); | ||
|
||
export const mockEventLoopUtilizationMonitor = collectorMock.create(); | ||
jest.doMock('./event_loop_utilization_monitor', () => ({ | ||
EventLoopUtilizationMonitor: jest.fn().mockImplementation(() => mockEventLoopUtilizationMonitor), | ||
})); |
Oops, something went wrong.