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.

Attempt to increase performance of RequestQueue - prefetching queue head of concurrency*3

Marek Trunkatff6df736a932
diff --git a/src/autoscaled_pool.js b/src/autoscaled_pool.js
index f37736332..b8144537e 100644
--- a/src/autoscaled_pool.js
+++ b/src/autoscaled_pool.js
@@ -76,7 +76,7 @@ const humanReadable = bytes => `${Math.round(bytes / 1024 / 1024)} MB`;
* @param {Number} [options.maxMemoryMbytes] Maximum memory available in the system. By default the pool
* uses the `totalMemory` value provided by `Apify.getMemoryInfo()`.
* @param {Number} [options.minFreeMemoryRatio=0.2] Minimum ratio of free memory kept in the system.
- * @param {number} [options.maybeRunIntervalMillis=1000] Indicates how often should the pool try to call
+ * @param {number} [options.maybeRunIntervalMillis=500] Indicates how often should the pool try to call
* `opts.runTaskFunction` to start a new task.
* @param {Boolean} [options.ignoreMainProcess=false] If set to `true` then autoscaling doesn't consider memory consumption
* of the main NodeJS process when autoscaling the pool up/down. This is mainly useful when
@@ -258,7 +258,7 @@ export default class AutoscaledPool {
this.concurrency--;
log.debug('AutoscaledPool: scaling down', {
- oldConcurrency: this.concurrency - 1,
+ oldConcurrency: this.concurrency + 1,
newConcurrency: this.concurrency,
isMemoryOverloaded,
isCpuOverloaded,
diff --git a/src/request_queue.js b/src/request_queue.js
index 6049d612a..24fda0f33 100644
--- a/src/request_queue.js
+++ b/src/request_queue.js
@@ -16,8 +16,9 @@ export const LOCAL_EMULATION_SUBDIR = LOCAL_EMULATION_SUBDIRS.requestQueues;
const MAX_OPENED_QUEUES = 1000;
const MAX_CACHED_REQUESTS = 1000 * 1000;
-// When requesting queue head we always fetch requestsInProgressCount + QUERY_HEAD_BUFFER number of requests
-export const QUERY_HEAD_BUFFER = 50;
+// When requesting queue head we always fetch requestsInProgressCount * QUERY_HEAD_BUFFER number of requests.
+export const QUERY_HEAD_MIN_LENGTH = 100;
+export const QUERY_HEAD_BUFFER = 3;
// If queue was modified (request added/updated/deleted) before more than API_PROCESSED_REQUESTS_DELAY_MILLIS
// then we get head query to be consistent.
@@ -378,7 +379,7 @@ export class RequestQueue {
*
* @ignore
*/
- _ensureHeadIsNonEmpty(checkModifiedAt = false, limit = this.inProgressCount + QUERY_HEAD_BUFFER, iteration = 0) {
+ _ensureHeadIsNonEmpty(checkModifiedAt = false, limit = Math.max(this.concurrency * QUERY_HEAD_BUFFER, QUERY_HEAD_MIN_LENGTH), iteration = 0) {
checkParamOrThrow(checkModifiedAt, 'checkModifiedAt', 'Boolean');
checkParamOrThrow(limit, 'limit', 'Number');
checkParamOrThrow(iteration, 'iteration', 'Number');
@@ -443,7 +444,7 @@ export class RequestQueue {
}
const nextLimit = shouldRepeatWithHigherLimit
- ? prevLimit + QUERY_HEAD_BUFFER
+ ? prevLimit * 1.5
: prevLimit;
const delayMillis = shouldRepeatForConsistency
diff --git a/test/request_queue.js b/test/request_queue.js
index 38758f1e8..c449335b6 100644
--- a/test/request_queue.js
+++ b/test/request_queue.js
@@ -7,7 +7,7 @@ import { delayPromise } from 'apify-shared/utilities';
import * as Apify from '../build/index';
import { ENV_VARS } from '../build/constants';
import { apifyClient } from '../build/utils';
-import { RequestQueueLocal, RequestQueue, LOCAL_EMULATION_SUBDIR, QUERY_HEAD_BUFFER } from '../build/request_queue';
+import { RequestQueueLocal, RequestQueue, LOCAL_EMULATION_SUBDIR, QUERY_HEAD_MIN_LENGTH } from '../build/request_queue';
import { emptyLocalEmulationSubdir, LOCAL_EMULATION_DIR, expectNotLocalEmulation } from './_helper';
chai.use(chaiAsPromised);
@@ -217,7 +217,7 @@ describe('RequestQueue', () => {
.once()
.withArgs({
queueId: 'some-id',
- limit: QUERY_HEAD_BUFFER,
+ limit: QUERY_HEAD_MIN_LENGTH,
})
.returns(Promise.resolve({
items: [
@@ -330,7 +330,7 @@ describe('RequestQueue', () => {
.once()
.withArgs({
queueId: 'some-id',
- limit: QUERY_HEAD_BUFFER,
+ limit: QUERY_HEAD_MIN_LENGTH,
})
.returns(Promise.resolve({
items: [