Bug 1344893 - Part 2: Add time to first byte metric. r=smaug, data-review=bsmedberg
authorWei-Cheng Pan <wpan@mozilla.com>
Wed, 19 Apr 2017 02:00:00 -0400
changeset 353819 3e22ac1beeda05bb7954434d37241438859fb557
parent 353818 3559898682b966f7e0110639c3e5c7da2382fdd3
child 353820 c72f2f5feaae70bd74219f80ea415ce811905af8
push id89336
push userryanvm@gmail.com
push dateWed, 19 Apr 2017 11:32:13 +0000
treeherdermozilla-inbound@51df9eb41148 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1344893
milestone55.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 1344893 - Part 2: Add time to first byte metric. r=smaug, data-review=bsmedberg MozReview-Commit-ID: 6a30Xofr6p1
dom/performance/PerformanceTiming.cpp
dom/performance/PerformanceTiming.h
toolkit/components/telemetry/Histograms.json
--- a/dom/performance/PerformanceTiming.cpp
+++ b/dom/performance/PerformanceTiming.cpp
@@ -1,16 +1,21 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "PerformanceTiming.h"
 #include "mozilla/dom/PerformanceTimingBinding.h"
+#include "mozilla/Telemetry.h"
+#include "nsIDocShell.h"
+#include "nsIDocShellTreeItem.h"
+#include "nsIDocument.h"
+#include "nsITimedChannel.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PerformanceTiming, mPerformance)
 
 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(PerformanceTiming, AddRef)
 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(PerformanceTiming, Release)
@@ -42,16 +47,25 @@ PerformanceTiming::PerformanceTiming(Per
   if (aHttpChannel) {
     mTimingAllowed = CheckAllowedOrigin(aHttpChannel, aChannel);
     bool redirectsPassCheck = false;
     aChannel->GetAllRedirectsPassTimingAllowCheck(&redirectsPassCheck);
     mReportCrossOriginRedirect = mTimingAllowed && redirectsPassCheck;
   }
 
   InitializeTimingInfo(aChannel);
+
+  // Non-null aHttpChannel implies that this PerformanceTiming object is being
+  // used for subresources, which is irrelevant to this probe.
+  if (!aHttpChannel &&
+      nsContentUtils::IsPerformanceTimingEnabled() &&
+      IsTopLevelContentDocument()) {
+    Telemetry::Accumulate(Telemetry::TIME_TO_RESPONSE_START_MS,
+                          ResponseStartHighRes() - mZeroTime);
+  }
 }
 
 // Copy the timing info from the channel so we don't need to keep the channel
 // alive just to get the timestamps.
 void
 PerformanceTiming::InitializeTimingInfo(nsITimedChannel* aChannel)
 {
   if (aChannel) {
@@ -352,10 +366,29 @@ PerformanceTiming::IsInitialized() const
 }
 
 JSObject*
 PerformanceTiming::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
 {
   return PerformanceTimingBinding::Wrap(cx, this, aGivenProto);
 }
 
+bool
+PerformanceTiming::IsTopLevelContentDocument() const
+{
+  nsCOMPtr<nsIDocument> document = mPerformance->GetDocumentIfCurrent();
+  if (!document) {
+    return false;
+  }
+  nsCOMPtr<nsIDocShell> docShell = document->GetDocShell();
+  if (!docShell) {
+    return false;
+  }
+  nsCOMPtr<nsIDocShellTreeItem> rootItem;
+  Unused << docShell->GetSameTypeRootTreeItem(getter_AddRefs(rootItem));
+  if (rootItem.get() != static_cast<nsIDocShellTreeItem*>(docShell.get())) {
+    return false;
+  }
+  return rootItem->ItemType() == nsIDocShellTreeItem::typeContent;
+}
+
 } // dom namespace
 } // mozilla namespace
--- a/dom/performance/PerformanceTiming.h
+++ b/dom/performance/PerformanceTiming.h
@@ -235,16 +235,18 @@ public:
   }
 
 private:
   ~PerformanceTiming();
 
   bool IsInitialized() const;
   void InitializeTimingInfo(nsITimedChannel* aChannel);
 
+  bool IsTopLevelContentDocument() const;
+
   RefPtr<Performance> mPerformance;
   DOMHighResTimeStamp mFetchStart;
 
   // This is an offset that will be added to each timing ([ms] resolution).
   // There are only 2 possible values: (1) logicaly equal to navigationStart
   // TimeStamp (results are absolute timstamps - wallclock); (2) "0" (results
   // are relative to the navigation start).
   DOMHighResTimeStamp mZeroTime;
--- a/toolkit/components/telemetry/Histograms.json
+++ b/toolkit/components/telemetry/Histograms.json
@@ -11406,10 +11406,19 @@
   "TIME_TO_LOAD_EVENT_END_MS": {
     "alert_emails": ["wpan@mozilla.com"],
     "expires_in_version": "60",
     "kind": "exponential",
     "high": 50000,
     "n_buckets": 100,
     "bug_numbers": [1344893],
     "description": "Time in milliseconds from navigationStart to loadEventEnd."
+  },
+  "TIME_TO_RESPONSE_START_MS": {
+    "alert_emails": ["wpan@mozilla.com"],
+    "expires_in_version": "60",
+    "kind": "exponential",
+    "high": 50000,
+    "n_buckets": 100,
+    "bug_numbers": [1344893],
+    "description": "Time in milliseconds from navigationStart to responseStart."
   }
 }