Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(benchmark): await sampler root readiness
ldm0840ba602ac1d
diff --git a/moli-benchmark/browser-spider-local/bench.mjs b/moli-benchmark/browser-spider-local/bench.mjsindex 44ca10e9c..b0100e064 100644--- a/moli-benchmark/browser-spider-local/bench.mjs+++ b/moli-benchmark/browser-spider-local/bench.mjs@@ -1338,7 +1338,7 @@ async function startTargetProcess(target, args, serviceObserver, workerLabel) {detached: true,stdio: ['ignore', 'pipe', 'pipe']});- serviceObserver.registerWorker(workerLabel, child.pid);+ const rootRegistration = serviceObserver.registerWorker(workerLabel, child.pid);child.once('error', (error) => {spawnState.error = error;pushLog(logs, `spawn error: ${error.message}`);@@ -1349,6 +1349,8 @@ async function startTargetProcess(target, args, serviceObserver, workerLabel) {child.stderr.on('data', (chunk) => {pushLog(logs, `stderr: ${chunk.toString('utf8').trim()}`);});+ // Do not start benchmark phases before the process tree has a baseline.+ await rootRegistration;try {const { endpoint, websocketUrl } = target === 'moli'diff --git a/moli-benchmark/browser-spider-local/lib/observability/index.mjs b/moli-benchmark/browser-spider-local/lib/observability/index.mjsindex 3109e5b21..cb9a70cf1 100644--- a/moli-benchmark/browser-spider-local/lib/observability/index.mjs+++ b/moli-benchmark/browser-spider-local/lib/observability/index.mjs@@ -52,7 +52,7 @@ class SpiderServiceObserver {registerWorker(label, pid) {this.#assertOpen();this.mark('worker-spawn', { worker: label, pid });- this.#sampler.addRoot(label, pid);+ return this.#sampler.addRoot(label, pid);}finish() {diff --git a/moli-benchmark/browser-spider-local/lib/observability/sampler-worker.mjs b/moli-benchmark/browser-spider-local/lib/observability/sampler-worker.mjsindex e936dd192..0e6bd7834 100644--- a/moli-benchmark/browser-spider-local/lib/observability/sampler-worker.mjs+++ b/moli-benchmark/browser-spider-local/lib/observability/sampler-worker.mjs@@ -43,6 +43,10 @@ parentPort.on('message', (message) => {if (message?.type === 'add-root') {collector.addRoot(message.label, message.pid);capture(true, 'root-registered');+ parentPort.postMessage({+ type: 'root-registered',+ registrationId: message.registrationId+ });return;}if (message?.type !== 'stop' || stopped) {diff --git a/moli-benchmark/browser-spider-local/lib/observability/sampler.mjs b/moli-benchmark/browser-spider-local/lib/observability/sampler.mjsindex a1dee64ae..f13374bcf 100644--- a/moli-benchmark/browser-spider-local/lib/observability/sampler.mjs+++ b/moli-benchmark/browser-spider-local/lib/observability/sampler.mjs@@ -3,7 +3,7 @@ import { performance } from 'node:perf_hooks';import { Worker } from 'node:worker_threads';const MINIMUM_INTERVAL_MS = 100;-const STOP_TIMEOUT_MS = 5000;+const WORKER_RESPONSE_TIMEOUT_MS = 5000;function finiteValues(samples, getter) {return samples@@ -189,9 +189,27 @@ export class ProcessTreeResourceSampler {this.workerError = null;this.resultResolver = null;this.resultPromise = null;+ this.nextRootRegistrationId = 1;+ this.pendingRootRegistrations = new Map();this.stoppedArtifact = null;}+ #finishRootRegistration(registrationId, registered) {+ const pending = this.pendingRootRegistrations.get(registrationId);+ if (!pending) {+ return;+ }+ clearTimeout(pending.timeout);+ this.pendingRootRegistrations.delete(registrationId);+ pending.resolve(registered);+ }++ #resolveRootRegistrations(registered) {+ for (const registrationId of this.pendingRootRegistrations.keys()) {+ this.#finishRootRegistration(registrationId, registered);+ }+ }+start() {if (this.startedAt !== null) {throw new Error('resource sampler has already started');@@ -212,13 +230,19 @@ export class ProcessTreeResourceSampler {this.resultResolver = resolve;});this.worker.on('message', (message) => {+ if (message?.type === 'root-registered') {+ this.#finishRootRegistration(message.registrationId, true);+ return;+ }if (message?.type === 'result' && !this.workerResult) {this.workerResult = message.collector;+ this.#resolveRootRegistrations(false);this.resultResolver?.(message.collector);}});this.worker.on('error', (error) => {this.workerError = error;+ this.#resolveRootRegistrations(false);this.resultResolver?.(null);});this.worker.on('exit', (code) => {@@ -227,6 +251,7 @@ export class ProcessTreeResourceSampler {`resource sampler worker exited before returning data (code ${code})`);}+ this.#resolveRootRegistrations(false);this.resultResolver?.(this.workerResult);});} catch (error) {@@ -244,14 +269,39 @@ export class ProcessTreeResourceSampler {}addRoot(label, pid) {- if (!Number.isInteger(pid) || pid <= 0) {- return;+ if (+ !Number.isInteger(pid)+ || pid <= 0+ || !this.worker+ || this.workerError+ || this.workerResult+ ) {+ return Promise.resolve(false);}- this.worker?.postMessage({- type: 'add-root',- label,- pid++ // Resolve only after the worker has installed the root and captured its+ // forced baseline sample. Callers can then align phase markers to sampler+ // readiness instead of guessing at worker startup time.+ const registrationId = this.nextRootRegistrationId;+ this.nextRootRegistrationId += 1;+ const registration = new Promise((resolve) => {+ const timeout = setTimeout(() => {+ this.#finishRootRegistration(registrationId, false);+ }, WORKER_RESPONSE_TIMEOUT_MS);+ this.pendingRootRegistrations.set(registrationId, { resolve, timeout });});+ try {+ this.worker.postMessage({+ type: 'add-root',+ registrationId,+ label,+ pid+ });+ } catch (error) {+ this.workerError = error;+ this.#finishRootRegistration(registrationId, false);+ }+ return registration;}async stop() {@@ -302,7 +352,7 @@ export class ProcessTreeResourceSampler {const collector = await Promise.race([this.resultPromise,new Promise((resolve) => {- timeout = setTimeout(() => resolve(null), STOP_TIMEOUT_MS);+ timeout = setTimeout(() => resolve(null), WORKER_RESPONSE_TIMEOUT_MS);})]);clearTimeout(timeout);diff --git a/moli-benchmark/browser-spider-local/tests/resource-sampler.test.mjs b/moli-benchmark/browser-spider-local/tests/resource-sampler.test.mjsindex 5b54f4861..de22445f3 100644--- a/moli-benchmark/browser-spider-local/tests/resource-sampler.test.mjs+++ b/moli-benchmark/browser-spider-local/tests/resource-sampler.test.mjs@@ -33,19 +33,22 @@ test('worker-thread sampler captures the current Linux process tree', {}, async () => {const sampler = new ProcessTreeResourceSampler({ intervalMs: 100 });sampler.start();- sampler.addRoot('self', process.pid);sampler.mark({ type: 'case-start', caseName: 'fixture' });- await new Promise((resolve) => setTimeout(resolve, 240));+ const rootRegistered = await sampler.addRoot('self', process.pid);sampler.mark({ type: 'case-done', caseName: 'fixture' });const artifact = await sampler.stop();const failureDetails = samplerFailureDetails(artifact);const caseDone = artifact.markers.find((marker) => marker.type === 'case-done');assert.equal(artifact.status, 'available', failureDetails);- assert.ok(artifact.samples.length >= 3, failureDetails);+ assert.equal(rootRegistered, true, failureDetails);+ assert.ok(artifact.samples.length >= 2, failureDetails);+ assert.equal(artifact.samples[0]?.kind, 'root-registered', failureDetails);+ assert.equal(artifact.samples.at(-1)?.kind, 'final', failureDetails);assert.ok(artifact.summary.peak_rss_bytes > 0, failureDetails);assert.ok(artifact.summary.peak_process_count >= 1, failureDetails);assert.equal(artifact.summary.cases[0]?.case_name, 'fixture', failureDetails);+ assert.ok(artifact.summary.cases[0]?.sample_count >= 1, failureDetails);assert.ok(caseDone, failureDetails);assert.ok(artifact.samples.at(-1)?.elapsed_ms >= caseDone?.elapsed_ms,