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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix(utils): advance the CPU tick baseline in systemInfoV2 getCurrentCpuTicks (#3824)
Anas Khanbb238007e340
diff --git a/packages/utils/src/internals/systemInfoV2/cpu-info.ts b/packages/utils/src/internals/systemInfoV2/cpu-info.tsindex 94f55d9b8..77304bf57 100644--- a/packages/utils/src/internals/systemInfoV2/cpu-info.ts+++ b/packages/utils/src/internals/systemInfoV2/cpu-info.ts@@ -48,8 +48,9 @@ export function getCurrentCpuTicks() {},{ idle: 0, total: 0 },);- const idleTicksDelta = ticks.idle - previousTicks!.idle;- const totalTicksDelta = ticks.total - previousTicks!.total;+ const idleTicksDelta = ticks.idle - previousTicks.idle;+ const totalTicksDelta = ticks.total - previousTicks.total;+ Object.assign(previousTicks, ticks);return totalTicksDelta ? 1 - idleTicksDelta / totalTicksDelta : 0;}diff --git a/test/core/autoscaling/snapshotter.test.ts b/test/core/autoscaling/snapshotter.test.tsindex 19427a28d..a8b7279f4 100644--- a/test/core/autoscaling/snapshotter.test.ts+++ b/test/core/autoscaling/snapshotter.test.ts@@ -144,6 +144,10 @@ describe('Snapshotter', () => {// do not initialize the event intervals as we will fire them manuallyconst spy = vitest.spyOn(LocalEventManager.prototype, 'init').mockImplementation(async () => {});const events = config.getEventManager() as LocalEventManager;++ // Establish a baseline before the Snapshotter starts listening for events.+ await events.emitSystemInfoEvent(noop);+ cpusMock.mockClear();await snapshotter.start();await events.emitSystemInfoEvent(noop);diff --git a/test/utils/cpu-infoV2.test.ts b/test/utils/cpu-infoV2.test.tsindex fbdacd511..9249cdf22 100644--- a/test/utils/cpu-infoV2.test.ts+++ b/test/utils/cpu-infoV2.test.ts@@ -32,6 +32,20 @@ vitest.mock('node:fs/promises', async (importActual) => {const getCgroupsVersionSpy = vitest.mocked(getCgroupsVersion);const readFileSpy = vitest.mocked(readFile);+// `getCurrentCpuTicks` keeps a module-level baseline of the previous tick sample.+// Zero it before each test so the singleton state cannot leak between tests.+function resetCpuTicksBaseline() {+ const cpusMock = vitest+ .spyOn(os, 'cpus')+ .mockReturnValue([{ times: { user: 0, nice: 0, sys: 0, idle: 0, irq: 0 } }] as os.CpuInfo[]);+ getCurrentCpuTicks();+ cpusMock.mockRestore();+}++beforeEach(() => {+ resetCpuTicksBaseline();+});+describe('getCurrentCpuTicks()', () => {test('calculates cpu load based on os.cpus', () => {// For two CPUs, we simulate:@@ -48,6 +62,29 @@ describe('getCurrentCpuTicks()', () => {expect(load).toBeCloseTo(0.75);cpusMock.mockRestore();});++ test('measures load between samples, not cumulatively since boot', () => {+ // First sample establishes the baseline: total = 400, idle = 200.+ const firstMock = vitest+ .spyOn(os, 'cpus')+ .mockReturnValue([{ times: { user: 100, nice: 0, sys: 100, idle: 200, irq: 0 } }] as os.CpuInfo[]);+ getCurrentCpuTicks();+ firstMock.mockRestore();++ // Second sample adds 150 busy ticks and 50 idle ticks: total = 600, idle = 250.+ // Chosen so the inter-sample delta and the cumulative-since-boot ratio differ.+ const secondMock = vitest+ .spyOn(os, 'cpus')+ .mockReturnValue([{ times: { user: 175, nice: 0, sys: 175, idle: 250, irq: 0 } }] as os.CpuInfo[]);+ const load = getCurrentCpuTicks();+ secondMock.mockRestore();++ // Delta since the previous sample: idleDelta = 50, totalDelta = 200 -> 1 - 50/200 = 0.75.+ // The cumulative-since-boot ratio would be 1 - 250/600 ≈ 0.583, which the buggy+ // (baseline-never-advanced) implementation returned instead.+ expect(load).toBeCloseTo(0.75);+ expect(load).not.toBeCloseTo(1 - 250 / 600);+ });});describe('getCpuQuota()', () => {