Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Use systemInfo event in Snapshotter

Ondra Urbanc28642abc0e0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3194c025..579d0531c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+xxx
+===================
+- Replace `cpuInfo` Apify event with new `systemInfo` event in `Snapshotter`.
+
0.14.8 / 2019-05-14
===================
- Bump `apify-client` to 0.5.17
diff --git a/src/autoscaling/snapshotter.js b/src/autoscaling/snapshotter.js
index cbcbb8254..dc616b606 100644
--- a/src/autoscaling/snapshotter.js
+++ b/src/autoscaling/snapshotter.js
@@ -1,3 +1,4 @@
+import os from 'os';
import _ from 'underscore';
import { betterSetInterval, betterClearInterval } from 'apify-shared/utilities';
import log from 'apify-shared/log';
@@ -11,7 +12,9 @@ const DEFAULT_OPTIONS = {
maxBlockedMillis: 50, // 0.05
memorySnapshotIntervalSecs: 1,
clientSnapshotIntervalSecs: 1,
+ cpuSnapshotIntervalSecs: 1,
maxUsedMemoryRatio: 0.7,
+ maxUsedCpuRatio: 0.95,
snapshotHistorySecs: 30,
maxClientErrors: 3,
};
@@ -23,21 +26,23 @@ const RESERVE_MEMORY_RATIO = 0.5;
* It tracks the following resources: Memory, EventLoop, API and CPU.
* The class is used by the {@link AutoscaledPool} class.
*
- * There are differences in behavior when running locally and on the Apify platform,
- * but those differences are handled internally by the class and do not affect its interface.
+ * When running on the Apify platform, the CPU and memory statistics are provided by the platform,
+ * as collected from the running Docker container. When running locally, `Snapshotter`
+ * makes its own statistics by querying the OS.
+ *
+ * CPU becomes overloaded locally when its current use exceeds the `maxUsedCpuRatio` option or
+ * when Apify platform marks it as overloaded.
*
* Memory becomes overloaded if its current use exceeds the `maxUsedMemoryRatio` option.
* It's computed using the total memory available to the container when running on
* the Apify platform and a quarter of total system memory when running locally.
- * Max total memory may be overridden by using the `APIFY_MEMORY_MBYTES` environment variable.
+ * Max total memory when running locally may be overridden by using the `APIFY_MEMORY_MBYTES`
+ * environment variable.
*
* Event loop becomes overloaded if it slows down by more than the `maxBlockedMillis` option.
*
* Client becomes overloaded when rate limit errors (429 - Too Many Requests),
- * typically received from the request queue exceed the set limit within the set interval.
- *
- * CPU tracking is available only on the Apify platform and the CPU overloaded event is read
- * directly off the container and is not configurable.
+ * typically received from the request queue, exceed the set limit within the set interval.
*
* @param {Object} [options] All `Snapshotter` parameters are passed
* via an options object with the following keys:
@@ -49,8 +54,17 @@ const RESERVE_MEMORY_RATIO = 0.5;
* @param {Number} [options.maxBlockedMillis=50]
* Maximum allowed delay of the event loop in milliseconds.
* Exceeding this limit overloads the event loop.
+ * @param {Number} [options.cpuSnapshotIntervalSecs=1]
+ * Defines the interval of measuring CPU usage.
+ * This is only used when running locally. On the Apify platform,
+ * the statistics are provided externally at a fixed interval.
+ * @param {Number} [options.maxUsedCpuRatio=0.95]
+ * Defines the maximum usage of CPU.
+ * Exceeding this limit overloads the memory.
* @param {Number} [options.memorySnapshotIntervalSecs=1]
* Defines the interval of measuring memory consumption.
+ * This is only used when running locally. On the Apify platform,
+ * the statistics are provided externally at a fixed interval.
* The measurement itself is resource intensive (25 - 50ms async).
* Therefore, setting this interval below 1 second is not recommended.
* @param {Number} [options.maxUsedMemoryRatio=0.7]
@@ -67,29 +81,35 @@ class Snapshotter {
constructor(options = {}) {
const {
eventLoopSnapshotIntervalSecs,
+ cpuSnapshotIntervalSecs,
memorySnapshotIntervalSecs,
clientSnapshotIntervalSecs,
snapshotHistorySecs,
maxBlockedMillis,
maxUsedMemoryRatio,
+ maxUsedCpuRatio,
maxClientErrors,
} = _.defaults({}, options, DEFAULT_OPTIONS);
checkParamOrThrow(eventLoopSnapshotIntervalSecs, 'options.eventLoopSnapshotIntervalSecs', 'Number');
checkParamOrThrow(memorySnapshotIntervalSecs, 'options.memorySnapshotIntervalSecs', 'Number');
+ checkParamOrThrow(cpuSnapshotIntervalSecs, 'options.cpuSnapshotIntervalSecs', 'Number');
checkParamOrThrow(snapshotHistorySecs, 'options.snapshotHistorySecs', 'Number');
checkParamOrThrow(clientSnapshotIntervalSecs, 'options.clientSnapshotIntervalSecs', 'Number');
checkParamOrThrow(maxBlockedMillis, 'options.maxBlockedMillis', 'Number');
checkParamOrThrow(maxUsedMemoryRatio, 'options.maxUsedMemoryRatio', 'Number');
+ checkParamOrThrow(maxUsedCpuRatio, 'options.maxUsedCpuRatio', 'Number');
checkParamOrThrow(maxClientErrors, 'options.maxClientErrors', 'Number');
this.eventLoopSnapshotIntervalMillis = eventLoopSnapshotIntervalSecs * 1000;
this.memorySnapshotIntervalMillis = memorySnapshotIntervalSecs * 1000;
this.clientSnapshotIntervalMillis = clientSnapshotIntervalSecs * 1000;
+ this.cpuSnapshotIntervalMillis = cpuSnapshotIntervalSecs * 1000;
this.snapshotHistoryMillis = snapshotHistorySecs * 1000;
this.maxBlockedMillis = maxBlockedMillis;
this.maxUsedMemoryRatio = maxUsedMemoryRatio;
+ this.maxUsedCpuRatio = maxUsedCpuRatio;
this.maxClientErrors = maxClientErrors;
this.maxMemoryBytes = (parseInt(process.env[ENV_VARS.MEMORY_MBYTES], 10) * 1024 * 1024) || null;
@@ -101,6 +121,7 @@ class Snapshotter {
this.eventLoopInterval = null;
this.memoryInterval = null;
this.clientInterval = null;
+ this.cpuInterval = null;
}
/**
@@ -108,23 +129,18 @@ class Snapshotter {
* @return {Promise}
*/
async start() {
- // Ensure max memory is correctly computed.
- if (!this.maxMemoryBytes) {
- const { totalBytes } = await getMemoryInfo();
- if (isAtHome()) {
- this.maxMemoryBytes = totalBytes;
- } else {
- this.maxMemoryBytes = Math.ceil(totalBytes / 4);
- // NOTE: Log as AutoscaledPool, so that users are not confused what "Snapshotter" is
- log.info(`AutoscaledPool: Setting max memory of this run to ${Math.round(this.maxMemoryBytes / 1024 / 1024)} MB. Use the ${ENV_VARS.MEMORY_MBYTES} environment variable to override it.`); // eslint-disable-line max-len
- }
- }
+ await this._ensureCorrectMaxMemory();
// Start snapshotting.
this.eventLoopInterval = betterSetInterval(this._snapshotEventLoop.bind(this), this.eventLoopSnapshotIntervalMillis);
- this.memoryInterval = betterSetInterval(this._snapshotMemory.bind(this), this.memorySnapshotIntervalMillis);
this.clientInterval = betterSetInterval(this._snapshotClient.bind(this), this.clientSnapshotIntervalMillis);
- events.on(ACTOR_EVENT_NAMES.CPU_INFO, this._snapshotCpu.bind(this));
+ if (isAtHome()) {
+ events.on(ACTOR_EVENT_NAMES.SYSTEM_INFO, this._snapshotCpuOnPlatform.bind(this));
+ events.on(ACTOR_EVENT_NAMES.SYSTEM_INFO, this._snapshotMemoryOnPlatform.bind(this));
+ } else {
+ this.cpuInterval = betterSetInterval(this._snapshotCpuOnLocal.bind(this), this.cpuSnapshotIntervalMillis);
+ this.memoryInterval = betterSetInterval(this._snapshotMemoryOnLocal.bind(this), this.memorySnapshotIntervalMillis);
+ }
}
/**
@@ -134,8 +150,10 @@ class Snapshotter {
async stop() {
betterClearInterval(this.eventLoopInterval);
betterClearInterval(this.memoryInterval);
+ betterClearInterval(this.cpuInterval);
betterClearInterval(this.clientInterval);
- events.removeListener(ACTOR_EVENT_NAMES.CPU_INFO, this._snapshotCpu);
+ events.removeListener(ACTOR_EVENT_NAMES.SYSTEM_INFO, this._snapshotCpuOnPlatform);
+ events.removeListener(ACTOR_EVENT_NAMES.SYSTEM_INFO, this._snapshotMemoryOnPlatform);
// Allow microtask queue to unwind before stop returns.
await new Promise(resolve => setImmediate(resolve));
}
@@ -204,12 +222,33 @@ class Snapshotter {
}
/**
- * Creates a snapshot of current memory usage.
+ * Creates a snapshot of current memory usage
+ * using the Apify platform `systemInfo` event.
+ * @param {Object} systemInfo
+ * @ignore
+ */
+ _snapshotMemoryOnPlatform(systemInfo) {
+ const now = new Date();
+ this._pruneSnapshots(this.memorySnapshots, now);
+ const { memCurrentBytes } = systemInfo;
+ const snapshot = {
+ createdAt: now,
+ isOverloaded: memCurrentBytes / this.maxMemoryBytes > this.maxUsedMemoryRatio,
+ usedBytes: memCurrentBytes,
+ };
+
+ this.memorySnapshots.push(snapshot);
+ this._memoryOverloadWarning(systemInfo);
+ }
+
+ /**
+ * Creates a snapshot of current memory usage
+ * using the Apify platform `systemInfo` event.
* @param {Function} intervalCallback
* @return {Promise}
* @ignore
*/
- async _snapshotMemory(intervalCallback) {
+ async _snapshotMemoryOnLocal(intervalCallback) {
try {
const now = new Date();
const memInfo = await getMemoryInfo();
@@ -217,15 +256,13 @@ class Snapshotter {
this._pruneSnapshots(this.memorySnapshots, now);
const usedBytes = mainProcessBytes + childProcessesBytes;
- const snapshot = Object.assign(memInfo, {
+ const snapshot = {
createdAt: now,
isOverloaded: usedBytes / this.maxMemoryBytes > this.maxUsedMemoryRatio,
- });
+ usedBytes,
+ };
this.memorySnapshots.push(snapshot);
- // TODO revisit this once we add memory readings from platform.
- // It only scares people and provides no value, because it's always wrong about memory.
- // this._memoryOverloadWarning(memInfo);
} catch (err) {
log.exception(err, 'Snapshotter: Memory snapshot failed.');
} finally {
@@ -236,23 +273,18 @@ class Snapshotter {
/**
* Checks for critical memory overload and logs it to the console.
* @ignore
- * @param {Object} memoryInfo - Memory information
+ * @param {Object} systemInfo
*/
- _memoryOverloadWarning(memoryInfo) {
- try {
- const maxDesiredMemoryBytes = this.maxUsedMemoryRatio * this.maxMemoryBytes;
- const reserveMemory = this.maxMemoryBytes * (1 - this.maxUsedMemoryRatio) * RESERVE_MEMORY_RATIO;
- const criticalOverloadBytes = maxDesiredMemoryBytes + reserveMemory;
- const { mainProcessBytes, childProcessesBytes } = memoryInfo;
- const usedBytes = mainProcessBytes + childProcessesBytes;
- const isCriticalOverload = usedBytes > criticalOverloadBytes;
- if (isCriticalOverload) {
- const usedPercentage = usedBytes / this.maxMemoryBytes * 100;
- const toMb = bytes => Math.round(bytes / (1024 ** 2));
- log.warning(`Memory is critically overloaded. Used ${toMb(usedBytes)} MB of ${toMb(this.maxMemoryBytes)} MB (${usedPercentage}%)`);
- }
- } catch (e) {
- log.exception(e, 'Snapshotter: Memory leak warning failed.');
+ _memoryOverloadWarning({ memCurrentBytes }) {
+ const maxDesiredMemoryBytes = this.maxUsedMemoryRatio * this.maxMemoryBytes;
+ const reserveMemory = this.maxMemoryBytes * (1 - this.maxUsedMemoryRatio) * RESERVE_MEMORY_RATIO;
+ const criticalOverloadBytes = maxDesiredMemoryBytes + reserveMemory;
+ const isCriticalOverload = memCurrentBytes > criticalOverloadBytes;
+ if (isCriticalOverload) {
+ const usedPercentage = memCurrentBytes / this.maxMemoryBytes * 100;
+ const toMb = bytes => Math.round(bytes / (1024 ** 2));
+ log.warning('Memory is critically overloaded. '
+ + `Used ${toMb(memCurrentBytes)} MB of ${toMb(this.maxMemoryBytes)} MB (${usedPercentage}%)`);
}
}
@@ -262,47 +294,90 @@ class Snapshotter {
* @ignore
*/
_snapshotEventLoop(intervalCallback) {
- try {
- const now = new Date();
- this._pruneSnapshots(this.eventLoopSnapshots, now);
+ const now = new Date();
+ this._pruneSnapshots(this.eventLoopSnapshots, now);
- // Handle empty snapshots array
- const snapshot = {
- createdAt: now,
- isOverloaded: false,
- exceededMillis: 0,
- };
- const previousSnapshot = this.eventLoopSnapshots[this.eventLoopSnapshots.length - 1];
- if (!previousSnapshot) return this.eventLoopSnapshots.push(snapshot);
+ const snapshot = {
+ createdAt: now,
+ isOverloaded: false,
+ exceededMillis: 0,
+ };
- // Compare with previous snapshot
+ const previousSnapshot = this.eventLoopSnapshots[this.eventLoopSnapshots.length - 1];
+ if (previousSnapshot) {
const { createdAt } = previousSnapshot;
const delta = now - createdAt - this.eventLoopSnapshotIntervalMillis;
- snapshot.exceededMillis = Math.max(delta - this.maxBlockedMillis, 0);
+
if (delta > this.maxBlockedMillis) snapshot.isOverloaded = true;
- this.eventLoopSnapshots.push(snapshot);
- } catch (err) {
- log.exception(err, 'Snapshotter: Event loop snapshot failed.');
- } finally {
- intervalCallback();
+ snapshot.exceededMillis = Math.max(delta - this.maxBlockedMillis, 0);
}
+
+ this.eventLoopSnapshots.push(snapshot);
+ intervalCallback();
}
/**
- * Creates a snapshot of current CPU usage.
- * @param {Object} cpuInfoEvent
- * @return {Promise}
+ * Creates a snapshot of current CPU usage
+ * using the Apify platform `systemInfo` event.
+ * @param {Object} systemInfo
* @ignore
*/
- _snapshotCpu(cpuInfoEvent) {
- const createdAt = (new Date(cpuInfoEvent.createdAt));
+ _snapshotCpuOnPlatform(systemInfo) {
+ const { cpuCurrentUsage, isCpuOverloaded } = systemInfo;
+ const createdAt = (new Date(systemInfo.createdAt));
this._pruneSnapshots(this.cpuSnapshots, createdAt);
+
this.cpuSnapshots.push({
createdAt,
- isOverloaded: cpuInfoEvent.isCpuOverloaded,
+ isOverloaded: isCpuOverloaded || cpuCurrentUsage > (this.maxUsedCpuRatio * 100),
+ usedRatio: Math.ceil(cpuCurrentUsage / 100),
});
}
+ /**
+ * Creates a snapshot of current CPU usage
+ * using OS provided metrics.
+ * @param {Function} intervalCallback
+ * @ignore
+ */
+ _snapshotCpuOnLocal(intervalCallback) {
+ const now = new Date();
+ this._pruneSnapshots(this.eventLoopSnapshots, now);
+
+ const ticks = this._getCurrentCpuTicks();
+ const snapshot = {
+ createdAt: now,
+ isOverloaded: false,
+ ticks,
+ usedRatio: 0,
+ };
+
+ const previousSnapshot = this.cpuSnapshots[this.cpuSnapshots.length - 1];
+ if (previousSnapshot) {
+ const { ticks: prevTicks } = previousSnapshot;
+ const idleTicksDelta = ticks.idle - prevTicks.idle;
+ const totalTicksDelta = ticks.total - prevTicks.total;
+ const usedCpuRatio = 1 - (idleTicksDelta / totalTicksDelta);
+
+ if (usedCpuRatio > this.maxUsedCpuRatio) snapshot.isOverloaded = true;
+ snapshot.usedRatio = Math.ceil(usedCpuRatio);
+ }
+
+ this.cpuSnapshots.push(snapshot);
+ intervalCallback();
+ }
+
+ _getCurrentCpuTicks() { // eslint-disable-line class-methods-use-this
+ const cpus = os.cpus();
+ return cpus.reduce((acc, cpu) => {
+ const cpuTimes = Object.values(cpu.times);
+ return {
+ idle: acc.idle + cpu.times.idle,
+ total: acc.total + cpuTimes.reduce((sum, num) => sum + num),
+ };
+ }, { idle: 0, total: 0 });
+ }
+
/**
* Creates a snapshot of current API state by checking for
* rate limit errors. Only errors produced by a 2nd retry
@@ -316,32 +391,27 @@ class Snapshotter {
* @private
*/
_snapshotClient(intervalCallback) {
- try {
- const now = new Date();
- this._pruneSnapshots(this.clientSnapshots, now);
-
- const allErrorCounts = apifyClient.stats.rateLimitErrors;
- const currentErrCount = allErrorCounts[2]; // 2nd retry.
+ const now = new Date();
+ this._pruneSnapshots(this.clientSnapshots, now);
- // Handle empty snapshots array
- const snapshot = {
- createdAt: now,
- isOverloaded: false,
- rateLimitErrorCount: currentErrCount,
- };
- const previousSnapshot = this.clientSnapshots[this.clientSnapshots.length - 1];
- if (!previousSnapshot) return this.clientSnapshots.push(snapshot);
+ const allErrorCounts = apifyClient.stats.rateLimitErrors;
+ const currentErrCount = allErrorCounts[2] || 0; // 2nd retry.
- // Compare with previous snapshot
+ // Handle empty snapshots array
+ const snapshot = {
+ createdAt: now,
+ isOverloaded: false,
+ rateLimitErrorCount: currentErrCount,
+ };
+ const previousSnapshot = this.clientSnapshots[this.clientSnapshots.length - 1];
+ if (previousSnapshot) {
const { rateLimitErrorCount } = previousSnapshot;
const delta = currentErrCount - rateLimitErrorCount;
if (delta > this.maxClientErrors) snapshot.isOverloaded = true;
- this.clientSnapshots.push(snapshot);
- } catch (err) {
- log.exception(err, 'Snapshotter: Client snapshot failed.');
- } finally {
- intervalCallback();
}
+
+ this.clientSnapshots.push(snapshot);
+ intervalCallback();
}
/**
@@ -360,6 +430,23 @@ class Snapshotter {
}
snapshots.splice(0, oldCount);
}
+
+ /**
+ * Calculate max memory for platform or local usage.
+ * @ignore
+ */
+ async _ensureCorrectMaxMemory() {
+ if (!this.maxMemoryBytes) {
+ const { totalBytes } = await getMemoryInfo();
+ if (isAtHome()) {
+ this.maxMemoryBytes = totalBytes;
+ } else {
+ this.maxMemoryBytes = Math.ceil(totalBytes / 4);
+ // NOTE: Log as AutoscaledPool, so that users are not confused what "Snapshotter" is
+ log.info(`AutoscaledPool: Setting max memory of this run to ${Math.round(this.maxMemoryBytes / 1024 / 1024)} MB. Use the ${ENV_VARS.MEMORY_MBYTES} environment variable to override it.`); // eslint-disable-line max-len
+ }
+ }
+ }
}
export default Snapshotter;
diff --git a/test/autoscaling/snapshotter.js b/test/autoscaling/snapshotter.js
index 1264e617b..3ade2f604 100644
--- a/test/autoscaling/snapshotter.js
+++ b/test/autoscaling/snapshotter.js
@@ -1,3 +1,6 @@
+/* eslint-disable no-underscore-dangle */
+
+import os from 'os';
import { expect } from 'chai';
import sinon from 'sinon';
import log from 'apify-shared/log';
@@ -40,18 +43,19 @@ describe('Snapshotter', () => {
const clientSnapshots = snapshotter.getClientSample();
expect(cpuSnapshots).to.be.an('array');
- expect(cpuSnapshots).to.have.lengthOf(0);
+ expect(cpuSnapshots).to.have.lengthOf(2);
+ cpuSnapshots.forEach((ss) => {
+ expect(ss.createdAt).to.be.a('date');
+ expect(ss.isOverloaded).to.be.a('boolean');
+ expect(ss.usedRatio).to.be.a('number');
+ });
expect(memorySnapshots).to.be.an('array');
expect(memorySnapshots).to.have.lengthOf(2);
memorySnapshots.forEach((ss) => {
expect(ss.createdAt).to.be.a('date');
expect(ss.isOverloaded).to.be.a('boolean');
- expect(ss.totalBytes).to.be.a('number');
expect(ss.usedBytes).to.be.a('number');
- expect(ss.freeBytes).to.be.a('number');
- expect(ss.mainProcessBytes).to.be.a('number');
- expect(ss.childProcessesBytes).to.be.a('number');
});
expect(eventLoopSnapshots).to.be.an('array');
@@ -77,7 +81,7 @@ describe('Snapshotter', () => {
const options = {
eventLoopSnapshotIntervalSecs: 0.05,
memorySnapshotIntervalSecs: 0.1,
- snapshotHistorySecs: 0.5,
+ cpuSnapshotIntervalSecs: 0.1,
};
const snapshotter = new Snapshotter(options);
await snapshotter.start();
@@ -87,18 +91,19 @@ describe('Snapshotter', () => {
const eventLoopSnapshots = snapshotter.getEventLoopSample();
const cpuSnapshots = snapshotter.getCpuSample();
- expect(cpuSnapshots).to.have.lengthOf(0);
- expect(memorySnapshots.length).to.be.above(1);
- expect(eventLoopSnapshots.length).to.be.above(9);
+ expect(cpuSnapshots.length).to.be.above(2);
+ expect(memorySnapshots.length).to.be.above(2);
+ expect(eventLoopSnapshots.length).to.be.above(4);
});
- it('should collect CPU events on Platform', async () => {
+ it('correctly marks CPU overloaded using Platform event', async () => {
process.env[ENV_VARS.IS_AT_HOME] = '1';
let count = 0;
const emitAndWait = async (delay) => {
- events.emit(ACTOR_EVENT_NAMES.CPU_INFO, {
+ events.emit(ACTOR_EVENT_NAMES.SYSTEM_INFO, {
isCpuOverloaded: count % 2 === 0,
createdAt: (new Date()).toISOString(),
+ cpuCurrentUsage: 66.6,
});
count++;
await Apify.utils.sleep(delay);
@@ -125,7 +130,49 @@ describe('Snapshotter', () => {
}
});
- it('correctly marks eventLoopOverloaded', () => { /* eslint-disable no-underscore-dangle */
+ it('correctly marks CPU overloaded using OS metrics', () => {
+ const mock = sinon.mock(os);
+ const fakeCpu = [{
+ times: {
+ idle: 0,
+ other: 0,
+ },
+ }];
+ const { times } = fakeCpu[0];
+
+ mock.expects('cpus').exactly(5).returns(fakeCpu);
+
+ const noop = () => {};
+ const snapshotter = new Snapshotter({ maxUsedCpuRatio: 0.5 });
+
+ snapshotter._snapshotCpuOnLocal(noop);
+
+ times.idle++;
+ times.other++;
+ snapshotter._snapshotCpuOnLocal(noop);
+
+ times.other += 2;
+ snapshotter._snapshotCpuOnLocal(noop);
+
+ times.idle += 2;
+ snapshotter._snapshotCpuOnLocal(noop);
+
+ times.other += 4;
+ snapshotter._snapshotCpuOnLocal(noop);
+
+ const loopSnapshots = snapshotter.getCpuSample();
+
+ expect(loopSnapshots.length).to.be.eql(5);
+ expect(loopSnapshots[0].isOverloaded).to.be.eql(false);
+ expect(loopSnapshots[1].isOverloaded).to.be.eql(false);
+ expect(loopSnapshots[2].isOverloaded).to.be.eql(true);
+ expect(loopSnapshots[3].isOverloaded).to.be.eql(false);
+ expect(loopSnapshots[4].isOverloaded).to.be.eql(true);
+
+ mock.verify();
+ });
+
+ it('correctly marks eventLoopOverloaded', () => {
const clock = sinon.useFakeTimers();
try {
const noop = () => {};
@@ -152,7 +199,7 @@ describe('Snapshotter', () => {
}
});
- it('correctly marks memoryOverloaded', async () => { /* eslint-disable no-underscore-dangle */
+ it('correctly marks memoryOverloaded using OS metrics', async () => { /* eslint-disable no-underscore-dangle */
const noop = () => {};
const memoryData = {
mainProcessBytes: toBytes(1000),
@@ -165,15 +212,15 @@ describe('Snapshotter', () => {
process.env[ENV_VARS.MEMORY_MBYTES] = '10000';
const snapshotter = new Snapshotter({ maxUsedMemoryRatio: 0.5 });
- await snapshotter._snapshotMemory(noop);
+ await snapshotter._snapshotMemoryOnLocal(noop);
memoryData.mainProcessBytes = toBytes(2000);
- await snapshotter._snapshotMemory(noop);
+ await snapshotter._snapshotMemoryOnLocal(noop);
memoryData.childProcessesBytes = toBytes(2000);
- await snapshotter._snapshotMemory(noop);
+ await snapshotter._snapshotMemoryOnLocal(noop);
memoryData.mainProcessBytes = toBytes(3001);
- await snapshotter._snapshotMemory(noop);
+ await snapshotter._snapshotMemoryOnLocal(noop);
memoryData.childProcessesBytes = toBytes(1999);
- await snapshotter._snapshotMemory(noop);
+ await snapshotter._snapshotMemoryOnLocal(noop);
const memorySnapshots = snapshotter.getMemorySample();
expect(memorySnapshots.length).to.be.eql(5);
@@ -188,12 +235,10 @@ describe('Snapshotter', () => {
it('correctly logs critical memory overload', () => {
const memoryDataOverloaded = {
- mainProcessBytes: toBytes(4600),
- childProcessesBytes: toBytes(3000),
+ memCurrentBytes: toBytes(7600),
};
const memoryDataNotOverloaded = {
- mainProcessBytes: toBytes(3750),
- childProcessesBytes: toBytes(3750),
+ memCurrentBytes: toBytes(7500),
};
let logged = false;
const warning = () => { logged = true; };