From 4cd94674d98ba54f1af6634aab530753379983c6 Mon Sep 17 00:00:00 2001 From: Ben Jervis Date: Fri, 12 Apr 2024 15:05:52 +1000 Subject: [PATCH] Collect request stats (#9633) Co-authored-by: mattcompiles --- packages/core/core/src/Parcel.js | 3 +++ packages/core/core/src/RequestTracker.js | 24 +++++++++++++++++++ .../core/src/requests/AssetGraphRequest.js | 15 ++++++++++++ packages/core/types-internal/src/index.js | 2 ++ .../reporters/tracer/src/TracerReporter.js | 2 +- 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/core/core/src/Parcel.js b/packages/core/core/src/Parcel.js index 55325000a5b..dbb6bde7c3d 100644 --- a/packages/core/core/src/Parcel.js +++ b/packages/core/core/src/Parcel.js @@ -360,6 +360,7 @@ export default class Parcel { bundleGraph: event.bundleGraph, buildTime: 0, requestBundle: event.requestBundle, + unstable_requestStats: {}, }; } @@ -383,6 +384,7 @@ export default class Parcel { return result; }, + unstable_requestStats: this.#requestTracker.flushStats(), }; await this.#reporterRunner.report(event); @@ -400,6 +402,7 @@ export default class Parcel { let event = { type: 'buildFailure', diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic], + unstable_requestStats: this.#requestTracker.flushStats(), }; await this.#reporterRunner.report(event); diff --git a/packages/core/core/src/RequestTracker.js b/packages/core/core/src/RequestTracker.js index 623fcbe864b..689f9a390c7 100644 --- a/packages/core/core/src/RequestTracker.js +++ b/packages/core/core/src/RequestTracker.js @@ -164,6 +164,7 @@ export const requestTypes = { }; type RequestType = $Values; +type RequestTypeName = $Keys; type RequestGraphNode = | RequestNode @@ -1027,6 +1028,7 @@ export default class RequestTracker { farm: WorkerFarm; options: ParcelOptions; signal: ?AbortSignal; + stats: Map = new Map(); constructor({ graph, @@ -1205,6 +1207,9 @@ export default class RequestTracker { try { let node = this.graph.getRequestNode(requestNodeId); + + this.stats.set(request.type, (this.stats.get(request.type) ?? 0) + 1); + let result = await request.run({ input: request.input, api, @@ -1227,6 +1232,25 @@ export default class RequestTracker { } } + flushStats(): {[requestType: string]: number} { + let requestTypeEntries = {}; + + for (let key of (Object.keys(requestTypes): RequestTypeName[])) { + requestTypeEntries[requestTypes[key]] = key; + } + + let formattedStats = {}; + + for (let [requestType, count] of this.stats.entries()) { + let requestTypeName = requestTypeEntries[requestType]; + formattedStats[requestTypeName] = count; + } + + this.stats = new Map(); + + return formattedStats; + } + createAPI( requestId: NodeId, previousInvalidations: Array, diff --git a/packages/core/core/src/requests/AssetGraphRequest.js b/packages/core/core/src/requests/AssetGraphRequest.js index 98c601faaad..9c79e87da3f 100644 --- a/packages/core/core/src/requests/AssetGraphRequest.js +++ b/packages/core/core/src/requests/AssetGraphRequest.js @@ -16,6 +16,7 @@ import type {StaticRunOpts, RunAPI} from '../RequestTracker'; import type {EntryResult} from './EntryRequest'; import type {PathRequestInput} from './PathRequest'; import type {Diagnostic} from '@parcel/diagnostic'; +import logger from '@parcel/logger'; import invariant from 'assert'; import nullthrows from 'nullthrows'; @@ -200,7 +201,9 @@ export class AssetGraphBuilder { 'A root node is required to traverse', ); + let visitedAssetGroups = new Set(); let visited = new Set([rootNodeId]); + const visit = (nodeId: NodeId) => { if (errors.length > 0) { return; @@ -223,6 +226,10 @@ export class AssetGraphBuilder { (!visited.has(childNodeId) || child.hasDeferred) && this.shouldVisitChild(nodeId, childNodeId) ) { + if (child.type === 'asset_group') { + visitedAssetGroups.add(childNodeId); + } + visited.add(childNodeId); visit(childNodeId); } @@ -232,6 +239,14 @@ export class AssetGraphBuilder { visit(rootNodeId); await this.queue.run(); + logger.verbose({ + origin: '@parcel/core', + message: 'Asset graph walked', + meta: { + visitedAssetGroupsCount: visitedAssetGroups.size, + }, + }); + if (this.prevChangedAssetsPropagation) { // Add any previously seen Assets that have not been propagated yet to // 'this.changedAssetsPropagation', but only if they still remain in the graph diff --git a/packages/core/types-internal/src/index.js b/packages/core/types-internal/src/index.js index 756e1357117..39b4e9bf9f1 100644 --- a/packages/core/types-internal/src/index.js +++ b/packages/core/types-internal/src/index.js @@ -1976,6 +1976,7 @@ export type BuildSuccessEvent = {| +buildTime: number, +changedAssets: Map, +requestBundle: (bundle: NamedBundle) => Promise, + +unstable_requestStats: {[requestType: string]: number}, |}; /** @@ -1985,6 +1986,7 @@ export type BuildSuccessEvent = {| export type BuildFailureEvent = {| +type: 'buildFailure', +diagnostics: Array, + +unstable_requestStats: {[requestType: string]: number}, |}; /** diff --git a/packages/reporters/tracer/src/TracerReporter.js b/packages/reporters/tracer/src/TracerReporter.js index c04d78b0238..5a5b0023497 100644 --- a/packages/reporters/tracer/src/TracerReporter.js +++ b/packages/reporters/tracer/src/TracerReporter.js @@ -66,6 +66,7 @@ export default (new Reporter({ case 'buildSuccess': case 'buildFailure': nullthrows(tracer).flush(); + tracer = null; // We explicitly trigger `end` on the writeStream for the trace, then we need to wait for // the `close` event before resolving the promise this report function returns to ensure // that the file has been properly closed and moved from it's temp location before Parcel @@ -73,7 +74,6 @@ export default (new Reporter({ return new Promise((resolve, reject) => { nullthrows(writeStream).once('close', err => { writeStream = null; - tracer = null; if (err) { reject(err); } else {