From 0a9df0b8fdb8d4bf198ecb7e360d492948ac6497 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Fri, 22 Dec 2023 00:47:00 +0530 Subject: [PATCH 1/3] perf: Async task in concurrent --- core/audits/script-treemap-data.js | 6 ++++-- core/computed/metrics/time-to-first-byte.js | 6 ++++-- core/computed/tbt-impact-tasks.js | 6 ++++-- core/gather/base-artifacts.js | 6 ++++-- core/gather/gatherers/link-elements.js | 6 ++++-- core/gather/gatherers/service-worker.js | 6 ++++-- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/audits/script-treemap-data.js b/core/audits/script-treemap-data.js index b8b647fc403d..bbdd58dfc607 100644 --- a/core/audits/script-treemap-data.js +++ b/core/audits/script-treemap-data.js @@ -167,8 +167,10 @@ class ScriptTreemapDataAudit extends Audit { const nodes = []; /** @type {Map} */ const htmlNodesByFrameId = new Map(); - const bundles = await JSBundles.request(artifacts, context); - const duplicationByPath = await ModuleDuplication.request(artifacts, context); + const [bundles, duplicationByPath] = await Promise.all([ + JSBundles.request(artifacts, context), + ModuleDuplication.request(artifacts, context), + ]); for (const script of artifacts.Scripts) { if (script.scriptLanguage !== 'JavaScript') continue; diff --git a/core/computed/metrics/time-to-first-byte.js b/core/computed/metrics/time-to-first-byte.js index 6b19c428a1ca..5c26444739f8 100644 --- a/core/computed/metrics/time-to-first-byte.js +++ b/core/computed/metrics/time-to-first-byte.js @@ -16,8 +16,10 @@ class TimeToFirstByte extends NavigationMetric { * @return {Promise} */ static async computeSimulatedMetric(data, context) { - const mainResource = await MainResource.request(data, context); - const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context); + const [mainResource, networkAnalysis] = await Promise.all([ + MainResource.request(data, context), + NetworkAnalysis.request(data.devtoolsLog, context), + ]); const observedTTFB = (await this.computeObservedMetric(data, context)).timing; const observedResponseTime = diff --git a/core/computed/tbt-impact-tasks.js b/core/computed/tbt-impact-tasks.js index a1eab94e7637..0bb710adebea 100644 --- a/core/computed/tbt-impact-tasks.js +++ b/core/computed/tbt-impact-tasks.js @@ -39,8 +39,10 @@ class TBTImpactTasks { }; } - const fcpResult = await FirstContentfulPaint.request(metricComputationData, context); - const ttiResult = await Interactive.request(metricComputationData, context); + const [fcpResult, ttiResult] = await Promise.all([ + FirstContentfulPaint.request(metricComputationData, context), + Interactive.request(metricComputationData, context), + ]); let startTimeMs = fcpResult.timing; let endTimeMs = ttiResult.timing; diff --git a/core/gather/base-artifacts.js b/core/gather/base-artifacts.js index 5bb2a639cda4..2fe3757d5b90 100644 --- a/core/gather/base-artifacts.js +++ b/core/gather/base-artifacts.js @@ -18,8 +18,10 @@ import { * @return {Promise} */ async function getBaseArtifacts(resolvedConfig, driver, context) { - const BenchmarkIndex = await getBenchmarkIndex(driver.executionContext); - const {userAgent, product} = await getBrowserVersion(driver.defaultSession); + const [BenchmarkIndex, {userAgent, product}] = await Promise.all([ + getBenchmarkIndex(driver.executionContext), + getBrowserVersion(driver.defaultSession), + ]); return { // Meta artifacts. diff --git a/core/gather/gatherers/link-elements.js b/core/gather/gatherers/link-elements.js index 9e4c86cf5c09..41e210b9ed2d 100644 --- a/core/gather/gatherers/link-elements.js +++ b/core/gather/gatherers/link-elements.js @@ -177,8 +177,10 @@ class LinkElements extends BaseGatherer { */ async getArtifact(context) { const devtoolsLog = context.dependencies.DevtoolsLog; - const fromDOM = await LinkElements.getLinkElementsInDOM(context); - const fromHeaders = await LinkElements.getLinkElementsInHeaders(context, devtoolsLog); + const [fromDOM, fromHeaders] = await Promise.all([ + LinkElements.getLinkElementsInDOM(context), + LinkElements.getLinkElementsInHeaders(context, devtoolsLog), + ]); const linkElements = fromDOM.concat(fromHeaders); for (const link of linkElements) { diff --git a/core/gather/gatherers/service-worker.js b/core/gather/gatherers/service-worker.js index 1c147af33c6f..270af6c2e164 100644 --- a/core/gather/gatherers/service-worker.js +++ b/core/gather/gatherers/service-worker.js @@ -19,8 +19,10 @@ class ServiceWorker extends BaseGatherer { */ async getArtifact(context) { const session = context.driver.defaultSession; - const {versions} = await serviceWorkers.getServiceWorkerVersions(session); - const {registrations} = await serviceWorkers.getServiceWorkerRegistrations(session); + const [{versions}, {registrations}] = await Promise.all([ + serviceWorkers.getServiceWorkerVersions(session), + serviceWorkers.getServiceWorkerRegistrations(session), + ]); return { versions, From 576302c1041b84d2e3814a354ea50eb6d54a07c2 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Fri, 22 Dec 2023 00:57:45 +0530 Subject: [PATCH 2/3] Update time-to-first-byte.js Lint fix --- core/computed/metrics/time-to-first-byte.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/computed/metrics/time-to-first-byte.js b/core/computed/metrics/time-to-first-byte.js index 5c26444739f8..0ade2a43c81e 100644 --- a/core/computed/metrics/time-to-first-byte.js +++ b/core/computed/metrics/time-to-first-byte.js @@ -16,7 +16,7 @@ class TimeToFirstByte extends NavigationMetric { * @return {Promise} */ static async computeSimulatedMetric(data, context) { - const [mainResource, networkAnalysis] = await Promise.all([ + const [mainResource, networkAnalysis] = await Promise.all([ MainResource.request(data, context), NetworkAnalysis.request(data.devtoolsLog, context), ]); From eb437da7038c00dd449d876f5244686049f138b7 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Fri, 22 Dec 2023 11:57:06 +0530 Subject: [PATCH 3/3] fix: revert some changes --- core/audits/script-treemap-data.js | 6 ++---- core/computed/metrics/time-to-first-byte.js | 6 ++---- core/computed/tbt-impact-tasks.js | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/core/audits/script-treemap-data.js b/core/audits/script-treemap-data.js index bbdd58dfc607..b8b647fc403d 100644 --- a/core/audits/script-treemap-data.js +++ b/core/audits/script-treemap-data.js @@ -167,10 +167,8 @@ class ScriptTreemapDataAudit extends Audit { const nodes = []; /** @type {Map} */ const htmlNodesByFrameId = new Map(); - const [bundles, duplicationByPath] = await Promise.all([ - JSBundles.request(artifacts, context), - ModuleDuplication.request(artifacts, context), - ]); + const bundles = await JSBundles.request(artifacts, context); + const duplicationByPath = await ModuleDuplication.request(artifacts, context); for (const script of artifacts.Scripts) { if (script.scriptLanguage !== 'JavaScript') continue; diff --git a/core/computed/metrics/time-to-first-byte.js b/core/computed/metrics/time-to-first-byte.js index 0ade2a43c81e..6b19c428a1ca 100644 --- a/core/computed/metrics/time-to-first-byte.js +++ b/core/computed/metrics/time-to-first-byte.js @@ -16,10 +16,8 @@ class TimeToFirstByte extends NavigationMetric { * @return {Promise} */ static async computeSimulatedMetric(data, context) { - const [mainResource, networkAnalysis] = await Promise.all([ - MainResource.request(data, context), - NetworkAnalysis.request(data.devtoolsLog, context), - ]); + const mainResource = await MainResource.request(data, context); + const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context); const observedTTFB = (await this.computeObservedMetric(data, context)).timing; const observedResponseTime = diff --git a/core/computed/tbt-impact-tasks.js b/core/computed/tbt-impact-tasks.js index 0bb710adebea..a1eab94e7637 100644 --- a/core/computed/tbt-impact-tasks.js +++ b/core/computed/tbt-impact-tasks.js @@ -39,10 +39,8 @@ class TBTImpactTasks { }; } - const [fcpResult, ttiResult] = await Promise.all([ - FirstContentfulPaint.request(metricComputationData, context), - Interactive.request(metricComputationData, context), - ]); + const fcpResult = await FirstContentfulPaint.request(metricComputationData, context); + const ttiResult = await Interactive.request(metricComputationData, context); let startTimeMs = fcpResult.timing; let endTimeMs = ttiResult.timing;