Advertisement
nareshnarie

Untitled

Jan 5th, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @license Copyright 2017 The Lighthouse Authors. All Rights Reserved.
  3.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  4.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  5.  */
  6. 'use strict';
  7.  
  8. const ByteEfficiencyAudit = require('./byte-efficiency-audit.js');
  9. const i18n = require('../../lib/i18n/i18n.js');
  10. const NetworkRecords = require('../../computed/network-records.js');
  11.  
  12. const UIStrings = {
  13.   /** Title of a diagnostic audit that provides detail on large network resources required during page load. 'Payloads' is roughly equivalent to 'resources'. This descriptive title is shown to users when the amount is acceptable and no user action is required. */
  14.   title: 'Avoids enormous network payloads',
  15.   /** Title of a diagnostic audit that provides detail on large network resources required during page load. 'Payloads' is roughly equivalent to 'resources'. This imperative title is shown to users when there is a significant amount of execution time that could be reduced. */
  16.   failureTitle: 'Avoid enormous network payloads',
  17.   /** Description of a Lighthouse audit that tells the user *why* they should reduce the size of the network resources required by the page. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */
  18.   description:
  19.   'Large network payloads cost users real money and are highly correlated with ' +
  20.   'long load times. [Learn ' +
  21.   'more](https://web.dev/total-byte-weight/).',
  22.   /** Used to summarize the total byte size of the page and all its network requests. The `{totalBytes}` placeholder will be replaced with the total byte sizes, shown in kibibytes (e.g. 142 KiB) */
  23.   displayValue: 'Total size was {totalBytes, number, bytes}\xa0KiB',
  24. };
  25.  
  26. const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
  27.  
  28. class TotalByteWeight extends ByteEfficiencyAudit {
  29.   /**
  30.    * @return {LH.Audit.Meta}
  31.    */
  32.   static get meta() {
  33.     return {
  34.       id: 'total-byte-weight',
  35.       title: str_(UIStrings.title),
  36.       failureTitle: str_(UIStrings.failureTitle),
  37.       description: str_(UIStrings.description),
  38.       scoreDisplayMode: ByteEfficiencyAudit.SCORING_MODES.NUMERIC,
  39.       requiredArtifacts: ['devtoolsLogs', 'traces'],
  40.     };
  41.   }
  42.  
  43.   /**
  44.    * @return {LH.Audit.ScoreOptions}
  45.    */
  46.   static get defaultOptions() {
  47.     return {
  48.       // see https://www.desmos.com/calculator/h7kfv68jre
  49.       // ~25th and ~10th percentiles, with resulting p10 computed.
  50.       // http://httparchive.org/interesting.php?a=All&l=Feb%201%202017&s=All#bytesTotal
  51.       p10: 2667 * 1024,
  52.       median: 4000 * 1024,
  53.     };
  54.   }
  55.  
  56.   /**
  57.    * @param {LH.Artifacts} artifacts
  58.    * @param {LH.Audit.Context} context
  59.    * @return {Promise<LH.Audit.Product>}
  60.    */
  61.   static async audit(artifacts, context) {
  62.     const devtoolsLog = artifacts.devtoolsLogs[ByteEfficiencyAudit.DEFAULT_PASS];
  63.     const records = await NetworkRecords.request(devtoolsLog, context);
  64.  
  65.     let totalBytes = 0;
  66.     /** @type {Array<{url: string, totalBytes: number}>} */
  67.     let results = [];
  68.     records.forEach(record => {
  69.       // exclude data URIs since their size is reflected in other resources
  70.       // exclude unfinished requests since they won't have transfer size information
  71.       if (record.parsedURL.scheme === 'data' || !record.finished) return;
  72.  
  73.       const result = {
  74.         url: record.url,
  75.         totalBytes: record.transferSize,
  76.       };
  77.  
  78.       totalBytes += result.totalBytes;
  79.       results.push(result);
  80.     });
  81.     results = results.sort((itemA, itemB) => {
  82.       return itemB.totalBytes - itemA.totalBytes ||
  83.         itemA.url.localeCompare(itemB.url);
  84.     }).slice(0, 10);
  85.  
  86.     const score = ByteEfficiencyAudit.computeLogNormalScore(
  87.       {p10: context.options.p10, median: context.options.median},
  88.       totalBytes
  89.     );
  90.  
  91.     /** @type {LH.Audit.Details.Table['headings']} */
  92.     const headings = [
  93.       {key: 'url', itemType: 'url', text: str_(i18n.UIStrings.columnURL)},
  94.       {key: 'totalBytes', itemType: 'bytes', text: str_(i18n.UIStrings.columnTransferSize)},
  95.     ];
  96.  
  97.     const tableDetails = ByteEfficiencyAudit.makeTableDetails(headings, results);
  98.  
  99.     return {
  100.       score,
  101.       numericValue: totalBytes,
  102.       numericUnit: 'byte',
  103.       displayValue: str_(UIStrings.displayValue, {totalBytes}),
  104.       details: tableDetails,
  105.     };
  106.   }
  107. }
  108.  
  109. module.exports = TotalByteWeight;
  110. module.exports.UIStrings = UIStrings;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement