Bug 1554913 - Remove eslint-disable-next-line complexity from _setupHarTimings r=Honza
authorKsenia Berezina <kberezina@mozilla.com>
Fri, 03 Jan 2020 09:58:08 +0000
changeset 508754 48295741798b373e2b0132b5c4873bd521a12961
parent 508753 3dd2d1099c2a33ed8b7b91fb4cd146bc77141b12
child 508755 421a3f911d735003927de1511adbfca3bddf5075
push id104178
push userjodvarko@mozilla.com
push dateFri, 03 Jan 2020 12:19:00 +0000
treeherderautoland@48295741798b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersHonza
bugs1554913
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1554913 - Remove eslint-disable-next-line complexity from _setupHarTimings r=Honza Differential Revision: https://phabricator.services.mozilla.com/D57877
devtools/server/actors/network-monitor/network-observer.js
--- a/devtools/server/actors/network-monitor/network-observer.js
+++ b/devtools/server/actors/network-monitor/network-observer.js
@@ -1043,33 +1043,177 @@ NetworkObserver.prototype = {
         result.timings,
         result.offsets,
         serverTimings
       );
     }
     this.openRequests.delete(httpActivity.channel);
   },
 
+  _getBlockedTiming: function(timings) {
+    if (timings.STATUS_RESOLVING && timings.STATUS_CONNECTING_TO) {
+      return timings.STATUS_RESOLVING.first - timings.REQUEST_HEADER.first;
+    } else if (timings.STATUS_SENDING_TO) {
+      return timings.STATUS_SENDING_TO.first - timings.REQUEST_HEADER.first;
+    }
+
+    return -1;
+  },
+
+  _getDnsTiming: function(timings) {
+    if (timings.STATUS_RESOLVING && timings.STATUS_RESOLVED) {
+      return timings.STATUS_RESOLVED.last - timings.STATUS_RESOLVING.first;
+    }
+
+    return -1;
+  },
+
+  _getConnectTiming: function(timings) {
+    if (timings.STATUS_CONNECTING_TO && timings.STATUS_CONNECTED_TO) {
+      return (
+        timings.STATUS_CONNECTED_TO.last - timings.STATUS_CONNECTING_TO.first
+      );
+    }
+
+    return -1;
+  },
+
+  _getReceiveTiming: function(timings) {
+    if (timings.RESPONSE_START && timings.RESPONSE_COMPLETE) {
+      return timings.RESPONSE_COMPLETE.last - timings.RESPONSE_START.first;
+    }
+
+    return -1;
+  },
+
+  _getWaitTiming: function(timings) {
+    if (timings.RESPONSE_START) {
+      return (
+        timings.RESPONSE_START.first -
+        (timings.REQUEST_BODY_SENT || timings.STATUS_SENDING_TO).last
+      );
+    }
+
+    return -1;
+  },
+
+  _getSslTiming: function(timings) {
+    if (timings.STATUS_TLS_STARTING && timings.STATUS_TLS_ENDING) {
+      return timings.STATUS_TLS_ENDING.last - timings.STATUS_TLS_STARTING.first;
+    }
+
+    return -1;
+  },
+
+  _getSendTiming: function(timings) {
+    if (timings.STATUS_SENDING_TO) {
+      return timings.STATUS_SENDING_TO.last - timings.STATUS_SENDING_TO.first;
+    } else if (timings.REQUEST_HEADER && timings.REQUEST_BODY_SENT) {
+      return timings.REQUEST_BODY_SENT.last - timings.REQUEST_HEADER.first;
+    }
+
+    return -1;
+  },
+
+  _getDataFromTimedChannel: function(timedChannel) {
+    const lookUpArr = [
+      "tcpConnectEndTime",
+      "connectStartTime",
+      "connectEndTime",
+      "secureConnectionStartTime",
+      "domainLookupEndTime",
+      "domainLookupStartTime",
+    ];
+
+    return lookUpArr.reduce((prev, prop) => {
+      const propName = prop + "Tc";
+      return {
+        ...prev,
+        [propName]: (() => {
+          if (!timedChannel) {
+            return 0;
+          }
+
+          const value = timedChannel[prop];
+
+          if (
+            value != 0 &&
+            timedChannel.asyncOpenTime &&
+            value < timedChannel.asyncOpenTime
+          ) {
+            return 0;
+          }
+
+          return value;
+        })(),
+      };
+    }, {});
+  },
+
+  _getSecureConnectionStartTimeInfo: function(timings) {
+    let secureConnectionStartTime = 0;
+    let secureConnectionStartTimeRelative = false;
+
+    if (timings.STATUS_TLS_STARTING && timings.STATUS_TLS_ENDING) {
+      if (timings.STATUS_CONNECTING_TO) {
+        secureConnectionStartTime =
+          timings.STATUS_TLS_STARTING.first -
+          timings.STATUS_CONNECTING_TO.first;
+      }
+
+      if (secureConnectionStartTime < 0) {
+        secureConnectionStartTime = 0;
+      }
+      secureConnectionStartTimeRelative = true;
+    }
+
+    return {
+      secureConnectionStartTime,
+      secureConnectionStartTimeRelative,
+    };
+  },
+
+  _getStartSendingTimeInfo: function(timings, connectStartTimeTc) {
+    let startSendingTime = 0;
+    let startSendingTimeRelative = false;
+
+    if (timings.STATUS_SENDING_TO) {
+      if (timings.STATUS_CONNECTING_TO) {
+        startSendingTime =
+          timings.STATUS_SENDING_TO.first - timings.STATUS_CONNECTING_TO.first;
+        startSendingTimeRelative = true;
+      } else if (connectStartTimeTc != 0) {
+        startSendingTime = timings.STATUS_SENDING_TO.first - connectStartTimeTc;
+        startSendingTimeRelative = true;
+      }
+
+      if (startSendingTime < 0) {
+        startSendingTime = 0;
+      }
+    }
+    return { startSendingTime, startSendingTimeRelative };
+  },
+
   /**
    * Update the HTTP activity object to include timing information as in the HAR
    * spec. The HTTP activity object holds the raw timing information in
    * |timings| - these are timings stored for each activity notification. The
    * HAR timing information is constructed based on these lower level
    * data.
    *
    * @param object httpActivity
    *        The HTTP activity object we are working with.
    * @param boolean fromCache
    *        Indicates that the result was returned from the browser cache
    * @return object
    *         This object holds two properties:
    *         - total - the total time for all of the request and response.
    *         - timings - the HAR timings object.
    */
-  // eslint-disable-next-line complexity
+
   _setupHarTimings: function(httpActivity, fromCache) {
     if (fromCache) {
       // If it came from the browser cache, we have no timing
       // information and these should all be 0
       return {
         total: 0,
         timings: {
           blocked: 0,
@@ -1095,198 +1239,91 @@ NetworkObserver.prototype = {
     const timings = httpActivity.timings;
     const harTimings = {};
     // If the TCP Fast Open option or tls1.3 0RTT is used tls and data can
     // be dispatched in SYN packet and not after tcp socket is connected.
     // To demostrate this properly we will calculated TLS and send start time
     // relative to CONNECTING_TO.
     // Similary if 0RTT is used, data can be sent as soon as a TLS handshake
     // starts.
-    let secureConnectionStartTime = 0;
-    let secureConnectionStartTimeRelative = false;
-    let startSendingTime = 0;
-    let startSendingTimeRelative = false;
 
-    if (timings.STATUS_RESOLVING && timings.STATUS_CONNECTING_TO) {
-      harTimings.blocked =
-        timings.STATUS_RESOLVING.first - timings.REQUEST_HEADER.first;
-    } else if (timings.STATUS_SENDING_TO) {
-      harTimings.blocked =
-        timings.STATUS_SENDING_TO.first - timings.REQUEST_HEADER.first;
-    } else {
-      harTimings.blocked = -1;
-    }
-
+    harTimings.blocked = this._getBlockedTiming(timings);
     // DNS timing information is available only in when the DNS record is not
     // cached.
-    harTimings.dns =
-      timings.STATUS_RESOLVING && timings.STATUS_RESOLVED
-        ? timings.STATUS_RESOLVED.last - timings.STATUS_RESOLVING.first
-        : -1;
-
-    if (timings.STATUS_CONNECTING_TO && timings.STATUS_CONNECTED_TO) {
-      harTimings.connect =
-        timings.STATUS_CONNECTED_TO.last - timings.STATUS_CONNECTING_TO.first;
-    } else {
-      harTimings.connect = -1;
-    }
+    harTimings.dns = this._getDnsTiming(timings);
+    harTimings.connect = this._getConnectTiming(timings);
+    harTimings.ssl = this._getSslTiming(timings);
 
-    if (timings.STATUS_TLS_STARTING && timings.STATUS_TLS_ENDING) {
-      harTimings.ssl =
-        timings.STATUS_TLS_ENDING.last - timings.STATUS_TLS_STARTING.first;
-      if (timings.STATUS_CONNECTING_TO) {
-        secureConnectionStartTime =
-          timings.STATUS_TLS_STARTING.first -
-          timings.STATUS_CONNECTING_TO.first;
-      }
-      if (secureConnectionStartTime < 0) {
-        secureConnectionStartTime = 0;
-      }
-      secureConnectionStartTimeRelative = true;
-    } else {
-      harTimings.ssl = -1;
-    }
+    let {
+      secureConnectionStartTime,
+      secureConnectionStartTimeRelative,
+    } = this._getSecureConnectionStartTimeInfo(timings);
 
     // sometimes the connection information events are attached to a speculative
     // channel instead of this one, but necko might glue them back together in the
     // nsITimedChannel interface used by Resource and Navigation Timing
     const timedChannel = httpActivity.channel.QueryInterface(
       Ci.nsITimedChannel
     );
 
-    let tcTcpConnectEndTime = 0;
-    let tcConnectStartTime = 0;
-    let tcConnectEndTime = 0;
-    let tcSecureConnectionStartTime = 0;
-    let tcDomainLookupEndTime = 0;
-    let tcDomainLookupStartTime = 0;
-
-    if (timedChannel) {
-      tcTcpConnectEndTime = timedChannel.tcpConnectEndTime;
-      tcConnectStartTime = timedChannel.connectStartTime;
-      tcConnectEndTime = timedChannel.connectEndTime;
-      tcSecureConnectionStartTime = timedChannel.secureConnectionStartTime;
-      tcDomainLookupEndTime = timedChannel.domainLookupEndTime;
-      tcDomainLookupStartTime = timedChannel.domainLookupStartTime;
-    }
-
-    // Make sure the above values are at least timedChannel.asyncOpenTime.
-    if (timedChannel && timedChannel.asyncOpenTime) {
-      if (
-        tcTcpConnectEndTime != 0 &&
-        tcTcpConnectEndTime < timedChannel.asyncOpenTime
-      ) {
-        tcTcpConnectEndTime = 0;
-      }
-      if (
-        tcConnectStartTime != 0 &&
-        tcConnectStartTime < timedChannel.asyncOpenTime
-      ) {
-        tcConnectStartTime = 0;
-      }
-      if (
-        tcConnectEndTime != 0 &&
-        tcConnectEndTime < timedChannel.asyncOpenTime
-      ) {
-        tcConnectEndTime = 0;
-      }
-      if (
-        tcSecureConnectionStartTime != 0 &&
-        tcSecureConnectionStartTime < timedChannel.asyncOpenTime
-      ) {
-        tcSecureConnectionStartTime = 0;
-      }
-      if (
-        tcDomainLookupEndTime != 0 &&
-        tcDomainLookupEndTime < timedChannel.asyncOpenTime
-      ) {
-        tcDomainLookupEndTime = 0;
-      }
-      if (
-        tcDomainLookupStartTime != 0 &&
-        tcDomainLookupStartTime < timedChannel.asyncOpenTime
-      ) {
-        tcDomainLookupStartTime = 0;
-      }
-    }
+    const {
+      tcpConnectEndTimeTc,
+      connectStartTimeTc,
+      connectEndTimeTc,
+      secureConnectionStartTimeTc,
+      domainLookupEndTimeTc,
+      domainLookupStartTimeTc,
+    } = this._getDataFromTimedChannel(timedChannel);
 
     if (
       harTimings.connect <= 0 &&
       timedChannel &&
-      tcTcpConnectEndTime != 0 &&
-      tcConnectStartTime != 0
+      tcpConnectEndTimeTc != 0 &&
+      connectStartTimeTc != 0
     ) {
-      harTimings.connect = tcTcpConnectEndTime - tcConnectStartTime;
-      if (tcSecureConnectionStartTime != 0) {
-        harTimings.ssl = tcConnectEndTime - tcSecureConnectionStartTime;
+      harTimings.connect = tcpConnectEndTimeTc - connectStartTimeTc;
+      if (secureConnectionStartTimeTc != 0) {
+        harTimings.ssl = connectEndTimeTc - secureConnectionStartTimeTc;
         secureConnectionStartTime =
-          tcSecureConnectionStartTime - tcConnectStartTime;
+          secureConnectionStartTimeTc - connectStartTimeTc;
         secureConnectionStartTimeRelative = true;
       } else {
         harTimings.ssl = -1;
       }
     } else if (
       timedChannel &&
       timings.STATUS_TLS_STARTING &&
-      tcSecureConnectionStartTime != 0
+      secureConnectionStartTimeTc != 0
     ) {
       // It can happen that TCP Fast Open actually have not sent any data and
       // timings.STATUS_TLS_STARTING.first value will be corrected in
       // timedChannel.secureConnectionStartTime
-      if (tcSecureConnectionStartTime > timings.STATUS_TLS_STARTING.first) {
+      if (secureConnectionStartTimeTc > timings.STATUS_TLS_STARTING.first) {
         // TCP Fast Open actually did not sent any data.
-        harTimings.ssl = tcConnectEndTime - tcSecureConnectionStartTime;
+        harTimings.ssl = connectEndTimeTc - secureConnectionStartTimeTc;
         secureConnectionStartTimeRelative = false;
       }
     }
 
     if (
       harTimings.dns <= 0 &&
       timedChannel &&
-      tcDomainLookupEndTime != 0 &&
-      tcDomainLookupStartTime != 0
+      domainLookupEndTimeTc != 0 &&
+      domainLookupStartTimeTc != 0
     ) {
-      harTimings.dns = tcDomainLookupEndTime - tcDomainLookupStartTime;
+      harTimings.dns = domainLookupEndTimeTc - domainLookupStartTimeTc;
     }
 
-    if (timings.STATUS_SENDING_TO) {
-      harTimings.send =
-        timings.STATUS_SENDING_TO.last - timings.STATUS_SENDING_TO.first;
-      if (timings.STATUS_CONNECTING_TO) {
-        startSendingTime =
-          timings.STATUS_SENDING_TO.first - timings.STATUS_CONNECTING_TO.first;
-        startSendingTimeRelative = true;
-      } else if (tcConnectStartTime != 0) {
-        startSendingTime = timings.STATUS_SENDING_TO.first - tcConnectStartTime;
-        startSendingTimeRelative = true;
-      }
-      if (startSendingTime < 0) {
-        startSendingTime = 0;
-      }
-    } else if (timings.REQUEST_HEADER && timings.REQUEST_BODY_SENT) {
-      harTimings.send =
-        timings.REQUEST_BODY_SENT.last - timings.REQUEST_HEADER.first;
-    } else {
-      harTimings.send = -1;
-    }
-
-    if (timings.RESPONSE_START) {
-      harTimings.wait =
-        timings.RESPONSE_START.first -
-        (timings.REQUEST_BODY_SENT || timings.STATUS_SENDING_TO).last;
-    } else {
-      harTimings.wait = -1;
-    }
-
-    if (timings.RESPONSE_START && timings.RESPONSE_COMPLETE) {
-      harTimings.receive =
-        timings.RESPONSE_COMPLETE.last - timings.RESPONSE_START.first;
-    } else {
-      harTimings.receive = -1;
-    }
+    harTimings.send = this._getSendTiming(timings);
+    harTimings.wait = this._getWaitTiming(timings);
+    harTimings.receive = this._getReceiveTiming(timings);
+    let {
+      startSendingTime,
+      startSendingTimeRelative,
+    } = this._getStartSendingTimeInfo(timings, connectStartTimeTc);
 
     if (secureConnectionStartTimeRelative) {
       const time = Math.max(Math.round(secureConnectionStartTime / 1000), -1);
       secureConnectionStartTime = time;
     }
     if (startSendingTimeRelative) {
       const time = Math.max(Math.round(startSendingTime / 1000), -1);
       startSendingTime = time;