Advertisement
nareshnarie

Untitled

Jan 6th, 2021
509
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 Audit = require('./audit.js');
  9. const i18n = require('../lib/i18n/i18n.js');
  10. const MainResource = require('../computed/main-resource.js');
  11.  
  12. const UIStrings = {
  13.   /** Title of a diagnostic audit that provides detail on how long it took from starting a request to when the server started responding. This descriptive title is shown to users when the amount is acceptable and no user action is required. */
  14.   title: 'Initial server response time was short',
  15.   /** Title of a diagnostic audit that provides detail on how long it took from starting a request to when the server started responding. This imperative title is shown to users when there is a significant amount of execution time that could be reduced. */
  16.   failureTitle: 'Reduce initial server response time',
  17.   /** Description of a Lighthouse audit that tells the user *why* they should reduce the amount of time it takes their server to start responding to requests. 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: 'Keep the server response time for the main document short because all other requests depend on it. [Learn more](https://web.dev/time-to-first-byte/).',
  19.   /** Used to summarize the total Server Response Time duration for the primary HTML response. The `{timeInMs}` placeholder will be replaced with the time duration, shown in milliseconds (e.g. 210 ms) */
  20.   displayValue: `Root document took {timeInMs, number, milliseconds}\xa0ms`,
  21. };
  22.  
  23. const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
  24.  
  25. // Due to the way that DevTools throttling works we cannot see if server response took less than ~570ms.
  26. // We set our failure threshold to 600ms to avoid those false positives but we want devs to shoot for 100ms.
  27. const TOO_SLOW_THRESHOLD_MS = 600;
  28. const TARGET_MS = 100;
  29.  
  30. class ServerResponseTime extends Audit {
  31.   /**
  32.    * @return {LH.Audit.Meta}
  33.    */
  34.   static get meta() {
  35.     return {
  36.       id: 'server-response-time',
  37.       title: str_(UIStrings.title),
  38.       failureTitle: str_(UIStrings.failureTitle),
  39.       description: str_(UIStrings.description),
  40.       requiredArtifacts: ['devtoolsLogs', 'URL'],
  41.     };
  42.   }
  43.  
  44.   /**
  45.    * @param {LH.Artifacts.NetworkRequest} record
  46.    */
  47.   static calculateResponseTime(record) {
  48.     const timing = record.timing;
  49.     return timing ? timing.receiveHeadersEnd - timing.sendEnd : 0;
  50.   }
  51.  
  52.   /**
  53.    * @param {LH.Artifacts} artifacts
  54.    * @param {LH.Audit.Context} context
  55.    * @return {Promise<LH.Audit.Product>}
  56.    */
  57.   static async audit(artifacts, context) {
  58.     const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
  59.     const mainResource = await MainResource.request({devtoolsLog, URL: artifacts.URL}, context);
  60.  
  61.     const responseTime = ServerResponseTime.calculateResponseTime(mainResource);
  62.     const passed = responseTime < TOO_SLOW_THRESHOLD_MS;
  63.     const displayValue = str_(UIStrings.displayValue, {timeInMs: responseTime});
  64.  
  65.     /** @type {LH.Audit.Details.Opportunity['headings']} */
  66.     const headings = [
  67.       {key: 'url', valueType: 'url', label: str_(i18n.UIStrings.columnURL)},
  68.       {key: 'responseTime', valueType: 'timespanMs', label: str_(i18n.UIStrings.columnTimeSpent)},
  69.     ];
  70.  
  71.     const details = Audit.makeOpportunityDetails(
  72.       headings,
  73.       [{url: mainResource.url, responseTime}],
  74.       responseTime - TARGET_MS
  75.     );
  76.  
  77.     return {
  78.       numericValue: responseTime,
  79.       numericUnit: 'millisecond',
  80.       score: Number(passed),
  81.       displayValue,
  82.       details,
  83.     };
  84.   }
  85. }
  86.  
  87. module.exports = ServerResponseTime;
  88. module.exports.UIStrings = UIStrings;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement