diff options
Diffstat (limited to 'dom/performance')
25 files changed, 694 insertions, 292 deletions
diff --git a/dom/performance/Performance.cpp b/dom/performance/Performance.cpp index 8dc239b05..a1e28bc55 100755 --- a/dom/performance/Performance.cpp +++ b/dom/performance/Performance.cpp @@ -13,23 +13,21 @@ #include "PerformanceMeasure.h" #include "PerformanceObserver.h" #include "PerformanceResourceTiming.h" +#include "PerformanceService.h" #include "PerformanceWorker.h" #include "mozilla/ErrorResult.h" #include "mozilla/dom/PerformanceBinding.h" #include "mozilla/dom/PerformanceEntryEvent.h" #include "mozilla/dom/PerformanceNavigationBinding.h" #include "mozilla/dom/PerformanceObserverBinding.h" +#include "mozilla/dom/PerformanceNavigationTiming.h" #include "mozilla/IntegerPrintfMacros.h" #include "mozilla/Preferences.h" #include "mozilla/TimerClamping.h" #include "WorkerPrivate.h" #include "WorkerRunnable.h" -#ifdef MOZ_WIDGET_GONK -#define PERFLOG(msg, ...) __android_log_print(ANDROID_LOG_INFO, "PerformanceTiming", msg, ##__VA_ARGS__) -#else #define PERFLOG(msg, ...) printf_stderr(msg, ##__VA_ARGS__) -#endif namespace mozilla { namespace dom { @@ -38,27 +36,6 @@ using namespace workers; namespace { -// Helper classes -class MOZ_STACK_CLASS PerformanceEntryComparator final -{ -public: - bool Equals(const PerformanceEntry* aElem1, - const PerformanceEntry* aElem2) const - { - MOZ_ASSERT(aElem1 && aElem2, - "Trying to compare null performance entries"); - return aElem1->StartTime() == aElem2->StartTime(); - } - - bool LessThan(const PerformanceEntry* aElem1, - const PerformanceEntry* aElem2) const - { - MOZ_ASSERT(aElem1 && aElem2, - "Trying to compare null performance entries"); - return aElem1->StartTime() < aElem2->StartTime(); - } -}; - class PrefEnabledRunnable final : public WorkerCheckAPIExposureOnMainThreadRunnable { @@ -103,14 +80,12 @@ NS_IMPL_RELEASE_INHERITED(Performance, DOMEventTargetHelper) /* static */ already_AddRefed<Performance> Performance::CreateForMainThread(nsPIDOMWindowInner* aWindow, nsDOMNavigationTiming* aDOMTiming, - nsITimedChannel* aChannel, - Performance* aParentPerformance) + nsITimedChannel* aChannel) { MOZ_ASSERT(NS_IsMainThread()); RefPtr<Performance> performance = - new PerformanceMainThread(aWindow, aDOMTiming, aChannel, - aParentPerformance); + new PerformanceMainThread(aWindow, aDOMTiming, aChannel); return performance.forget(); } @@ -142,6 +117,24 @@ Performance::Performance(nsPIDOMWindowInner* aWindow) Performance::~Performance() {} +DOMHighResTimeStamp +Performance::Now() const +{ + TimeDuration duration = TimeStamp::Now() - CreationTimeStamp(); + return RoundTime(duration.ToMilliseconds()); +} + +DOMHighResTimeStamp +Performance::TimeOrigin() +{ + if (!mPerformanceService) { + mPerformanceService = PerformanceService::GetOrCreate(); + } + + MOZ_ASSERT(mPerformanceService); + return mPerformanceService->TimeOrigin(CreationTimeStamp()); +} + JSObject* Performance::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { @@ -266,7 +259,7 @@ Performance::ClearMarks(const Optional<nsAString>& aName) DOMHighResTimeStamp Performance::ResolveTimestampFromName(const nsAString& aName, - ErrorResult& aRv) + ErrorResult& aRv) { AutoTArray<RefPtr<PerformanceEntry>, 1> arr; DOMHighResTimeStamp ts; diff --git a/dom/performance/Performance.h b/dom/performance/Performance.h index bc70589a5..4debecc90 100644 --- a/dom/performance/Performance.h +++ b/dom/performance/Performance.h @@ -24,6 +24,7 @@ namespace dom { class PerformanceEntry; class PerformanceNavigation; class PerformanceObserver; +class PerformanceService; class PerformanceTiming; namespace workers { @@ -45,8 +46,7 @@ public: static already_AddRefed<Performance> CreateForMainThread(nsPIDOMWindowInner* aWindow, nsDOMNavigationTiming* aDOMTiming, - nsITimedChannel* aChannel, - Performance* aParentPerformance); + nsITimedChannel* aChannel); static already_AddRefed<Performance> CreateForWorker(workers::WorkerPrivate* aWorkerPrivate); @@ -54,21 +54,23 @@ public: JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override; - void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval); + virtual void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval); - void GetEntriesByType(const nsAString& aEntryType, - nsTArray<RefPtr<PerformanceEntry>>& aRetval); + virtual void GetEntriesByType(const nsAString& aEntryType, + nsTArray<RefPtr<PerformanceEntry>>& aRetval); - void GetEntriesByName(const nsAString& aName, - const Optional<nsAString>& aEntryType, - nsTArray<RefPtr<PerformanceEntry>>& aRetval); + virtual void GetEntriesByName(const nsAString& aName, + const Optional<nsAString>& aEntryType, + nsTArray<RefPtr<PerformanceEntry>>& aRetval); virtual void AddEntry(nsIHttpChannel* channel, nsITimedChannel* timedChannel) = 0; void ClearResourceTimings(); - virtual DOMHighResTimeStamp Now() const = 0; + DOMHighResTimeStamp Now() const; + + DOMHighResTimeStamp TimeOrigin(); void Mark(const nsAString& aName, ErrorResult& aRv); @@ -101,8 +103,6 @@ public: virtual nsITimedChannel* GetChannel() const = 0; - virtual Performance* GetParentPerformance() const = 0; - protected: Performance(); explicit Performance(nsPIDOMWindowInner* aWindow); @@ -126,10 +126,16 @@ protected: virtual DOMHighResTimeStamp CreationTime() const = 0; - virtual bool IsPerformanceTimingAttribute(const nsAString& aName) = 0; + virtual bool IsPerformanceTimingAttribute(const nsAString& aName) + { + return false; + } virtual DOMHighResTimeStamp - GetPerformanceTimingFromString(const nsAString& aTimingName) = 0; + GetPerformanceTimingFromString(const nsAString& aTimingName) + { + return 0; + } bool IsResourceEntryLimitReached() const { @@ -147,13 +153,15 @@ protected: nsTObserverArray<PerformanceObserver*> mObservers; -private: +protected: nsTArray<RefPtr<PerformanceEntry>> mUserEntries; nsTArray<RefPtr<PerformanceEntry>> mResourceEntries; uint64_t mResourceTimingBufferSize; static const uint64_t kDefaultResourceTimingBufferSize = 150; bool mPendingNotificationObserversTask; + + RefPtr<PerformanceService> mPerformanceService; }; } // namespace dom diff --git a/dom/performance/PerformanceEntry.h b/dom/performance/PerformanceEntry.h index bc4f84f1c..0af9f669e 100644 --- a/dom/performance/PerformanceEntry.h +++ b/dom/performance/PerformanceEntry.h @@ -90,6 +90,27 @@ protected: nsString mEntryType; }; +// Helper classes +class MOZ_STACK_CLASS PerformanceEntryComparator final +{ +public: + bool Equals(const PerformanceEntry* aElem1, + const PerformanceEntry* aElem2) const + { + MOZ_ASSERT(aElem1 && aElem2, + "Trying to compare null performance entries"); + return aElem1->StartTime() == aElem2->StartTime(); + } + + bool LessThan(const PerformanceEntry* aElem1, + const PerformanceEntry* aElem2) const + { + MOZ_ASSERT(aElem1 && aElem2, + "Trying to compare null performance entries"); + return aElem1->StartTime() < aElem2->StartTime(); + } +}; + } // namespace dom } // namespace mozilla diff --git a/dom/performance/PerformanceMainThread.cpp b/dom/performance/PerformanceMainThread.cpp index 86d42c5f8..64c06d3ea 100644 --- a/dom/performance/PerformanceMainThread.cpp +++ b/dom/performance/PerformanceMainThread.cpp @@ -6,6 +6,7 @@ #include "PerformanceMainThread.h" #include "PerformanceNavigation.h" +#include "nsICacheInfoChannel.h" namespace mozilla { namespace dom { @@ -16,7 +17,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PerformanceMainThread, Performance) NS_IMPL_CYCLE_COLLECTION_UNLINK(mTiming, mNavigation, - mParentPerformance) + mDocEntry) tmp->mMozMemory = nullptr; mozilla::DropJSObjects(this); NS_IMPL_CYCLE_COLLECTION_UNLINK_END @@ -25,7 +26,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PerformanceMainThread, Performance) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTiming, mNavigation, - mParentPerformance) + mDocEntry) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END @@ -45,12 +46,10 @@ NS_INTERFACE_MAP_END_INHERITING(Performance) PerformanceMainThread::PerformanceMainThread(nsPIDOMWindowInner* aWindow, nsDOMNavigationTiming* aDOMTiming, - nsITimedChannel* aChannel, - Performance* aParentPerformance) + nsITimedChannel* aChannel) : Performance(aWindow) , mDOMTiming(aDOMTiming) , mChannel(aChannel) - , mParentPerformance(aParentPerformance) { MOZ_ASSERT(aWindow, "Parent window object should be provided"); } @@ -78,7 +77,7 @@ PerformanceTiming* PerformanceMainThread::Timing() { if (!mTiming) { - // For navigation timing, the third argument (an nsIHtttpChannel) is null + // For navigation timing, the third argument (an nsIHttpChannel) is null // since the cross-domain redirect were already checked. The last argument // (zero time) for performance.timing is the navigation start value. mTiming = new PerformanceTiming(this, mChannel, nullptr, @@ -108,12 +107,6 @@ PerformanceMainThread::Navigation() return mNavigation; } -DOMHighResTimeStamp -PerformanceMainThread::Now() const -{ - return RoundTime(GetDOMTiming()->TimeStampToDOMHighRes(TimeStamp::Now())); -} - /** * An entry should be added only after the resource is loaded. * This method is not thread safe and can only be called on the main thread. @@ -161,27 +154,7 @@ PerformanceMainThread::AddEntry(nsIHttpChannel* channel, // The PerformanceResourceTiming object will use the PerformanceTiming // object to get all the required timings. RefPtr<PerformanceResourceTiming> performanceEntry = - new PerformanceResourceTiming(performanceTiming, this, entryName); - - nsAutoCString protocol; - channel->GetProtocolVersion(protocol); - performanceEntry->SetNextHopProtocol(NS_ConvertUTF8toUTF16(protocol)); - - uint64_t encodedBodySize = 0; - channel->GetEncodedBodySize(&encodedBodySize); - performanceEntry->SetEncodedBodySize(encodedBodySize); - - uint64_t transferSize = 0; - channel->GetTransferSize(&transferSize); - performanceEntry->SetTransferSize(transferSize); - - uint64_t decodedBodySize = 0; - channel->GetDecodedBodySize(&decodedBodySize); - if (decodedBodySize == 0) { - decodedBodySize = encodedBodySize; - } - performanceEntry->SetDecodedBodySize(decodedBodySize); - + new PerformanceResourceTiming(performanceTiming, this, entryName, channel); // If the initiator type had no valid value, then set it to the default // ("other") value. if (initiatorType.IsEmpty()) { @@ -335,5 +308,65 @@ PerformanceMainThread::CreationTime() const return GetDOMTiming()->GetNavigationStart(); } +void +PerformanceMainThread::EnsureDocEntry() +{ + if (!mDocEntry && nsContentUtils::IsPerformanceNavigationTimingEnabled()) { + nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel); + RefPtr<PerformanceTiming> timing = + new PerformanceTiming(this, mChannel, nullptr, 0); + mDocEntry = new PerformanceNavigationTiming(timing, this, + httpChannel); + } +} + + +void +PerformanceMainThread::GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval) +{ + aRetval = mResourceEntries; + aRetval.AppendElements(mUserEntries); + + EnsureDocEntry(); + if (mDocEntry) { + aRetval.AppendElement(mDocEntry); + } + + aRetval.Sort(PerformanceEntryComparator()); +} + +void +PerformanceMainThread::GetEntriesByType(const nsAString& aEntryType, + nsTArray<RefPtr<PerformanceEntry>>& aRetval) +{ + if (aEntryType.EqualsLiteral("navigation")) { + aRetval.Clear(); + EnsureDocEntry(); + if (mDocEntry) { + aRetval.AppendElement(mDocEntry); + } + return; + } + + Performance::GetEntriesByType(aEntryType, aRetval); +} + +void +PerformanceMainThread::GetEntriesByName(const nsAString& aName, + const Optional<nsAString>& aEntryType, + nsTArray<RefPtr<PerformanceEntry>>& aRetval) +{ + if (aName.EqualsLiteral("document")) { + aRetval.Clear(); + EnsureDocEntry(); + if (mDocEntry) { + aRetval.AppendElement(mDocEntry); + } + return; + } + + Performance::GetEntriesByName(aName, aEntryType, aRetval); +} + } // dom namespace } // mozilla namespace diff --git a/dom/performance/PerformanceMainThread.h b/dom/performance/PerformanceMainThread.h index 84773f29b..9f0e185fc 100644 --- a/dom/performance/PerformanceMainThread.h +++ b/dom/performance/PerformanceMainThread.h @@ -17,16 +17,12 @@ class PerformanceMainThread final : public Performance public: PerformanceMainThread(nsPIDOMWindowInner* aWindow, nsDOMNavigationTiming* aDOMTiming, - nsITimedChannel* aChannel, - Performance* aParentPerformance); + nsITimedChannel* aChannel); NS_DECL_ISUPPORTS_INHERITED NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PerformanceMainThread, Performance) - // Performance WebIDL methods - DOMHighResTimeStamp Now() const override; - virtual PerformanceTiming* Timing() override; virtual PerformanceNavigation* Navigation() override; @@ -51,10 +47,14 @@ public: return mChannel; } - virtual Performance* GetParentPerformance() const override - { - return mParentPerformance; - } + // The GetEntries* methods need to be overriden in order to add the + // the document entry of type navigation. + virtual void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; + virtual void GetEntriesByType(const nsAString& aEntryType, + nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; + virtual void GetEntriesByName(const nsAString& aName, + const Optional<nsAString>& aEntryType, + nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; protected: ~PerformanceMainThread(); @@ -72,12 +72,13 @@ protected: GetPerformanceTimingFromString(const nsAString& aTimingName) override; void DispatchBufferFullEvent() override; + void EnsureDocEntry(); + RefPtr<PerformanceEntry> mDocEntry; RefPtr<nsDOMNavigationTiming> mDOMTiming; nsCOMPtr<nsITimedChannel> mChannel; RefPtr<PerformanceTiming> mTiming; RefPtr<PerformanceNavigation> mNavigation; - RefPtr<Performance> mParentPerformance; JS::Heap<JSObject*> mMozMemory; }; diff --git a/dom/performance/PerformanceNavigationTiming.cpp b/dom/performance/PerformanceNavigationTiming.cpp new file mode 100644 index 000000000..4e00b2bb2 --- /dev/null +++ b/dom/performance/PerformanceNavigationTiming.cpp @@ -0,0 +1,96 @@ +/* -*- 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 "mozilla/dom/PerformanceNavigationTiming.h" +#include "mozilla/dom/PerformanceNavigationTimingBinding.h" + +using namespace mozilla::dom; + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PerformanceNavigationTiming) +NS_INTERFACE_MAP_END_INHERITING(PerformanceResourceTiming) + +NS_IMPL_ADDREF_INHERITED(PerformanceNavigationTiming, PerformanceResourceTiming) +NS_IMPL_RELEASE_INHERITED(PerformanceNavigationTiming, PerformanceResourceTiming) + +JSObject* +PerformanceNavigationTiming::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) +{ + return PerformanceNavigationTimingBinding::Wrap(aCx, this, aGivenProto); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::UnloadEventStart() const +{ + return mTiming->GetDOMTiming()->GetUnloadEventStartHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::UnloadEventEnd() const +{ + return mTiming->GetDOMTiming()->GetUnloadEventEndHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::DomInteractive() const +{ + return mTiming->GetDOMTiming()->GetDomInteractiveHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::DomContentLoadedEventStart() const +{ + return mTiming->GetDOMTiming()->GetDomContentLoadedEventStartHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::DomContentLoadedEventEnd() const +{ + return mTiming->GetDOMTiming()->GetDomContentLoadedEventEndHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::DomComplete() const +{ + return mTiming->GetDOMTiming()->GetDomCompleteHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::LoadEventStart() const +{ + return mTiming->GetDOMTiming()->GetLoadEventStartHighRes(); +} + +DOMHighResTimeStamp +PerformanceNavigationTiming::LoadEventEnd() const +{ + return mTiming->GetDOMTiming()->GetLoadEventEndHighRes(); +} + +NavigationType +PerformanceNavigationTiming::Type() const +{ + switch(mTiming->GetDOMTiming()->GetType()) { + case nsDOMNavigationTiming::TYPE_NAVIGATE: + return NavigationType::Navigate; + break; + case nsDOMNavigationTiming::TYPE_RELOAD: + return NavigationType::Reload; + break; + case nsDOMNavigationTiming::TYPE_BACK_FORWARD: + return NavigationType::Back_forward; + break; + default: + // The type is TYPE_RESERVED or some other value that was later added. + // We fallback to the default of Navigate. + return NavigationType::Navigate; + } +} + +uint16_t +PerformanceNavigationTiming::RedirectCount() const +{ + return mTiming->GetRedirectCount(); +} diff --git a/dom/performance/PerformanceNavigationTiming.h b/dom/performance/PerformanceNavigationTiming.h new file mode 100644 index 000000000..8555f1987 --- /dev/null +++ b/dom/performance/PerformanceNavigationTiming.h @@ -0,0 +1,71 @@ +/* -*- 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/. */ + +#ifndef mozilla_dom_PerformanceNavigationTiming_h___ +#define mozilla_dom_PerformanceNavigationTiming_h___ + +#include "nsCOMPtr.h" +#include "nsIChannel.h" +#include "nsITimedChannel.h" +#include "mozilla/dom/PerformanceResourceTiming.h" +#include "mozilla/dom/PerformanceNavigationTimingBinding.h" +#include "nsIHttpChannel.h" + +namespace mozilla { +namespace dom { + +// https://www.w3.org/TR/navigation-timing-2/#sec-PerformanceNavigationTiming +class PerformanceNavigationTiming final + : public PerformanceResourceTiming +{ +public: + NS_DECL_ISUPPORTS_INHERITED + + // Note that aPerformanceTiming must be initalized with zeroTime = 0 + // so that timestamps are relative to startTime, as opposed to the + // performance.timing object for which timestamps are absolute and has a + // zeroTime initialized to navigationStart + explicit PerformanceNavigationTiming(PerformanceTiming* aPerformanceTiming, + Performance* aPerformance, + nsIHttpChannel* aChannel) + : PerformanceResourceTiming(aPerformanceTiming, aPerformance, + NS_LITERAL_STRING("document"), aChannel) { + SetEntryType(NS_LITERAL_STRING("navigation")); + SetInitiatorType(NS_LITERAL_STRING("navigation")); + } + + DOMHighResTimeStamp Duration() const override + { + return LoadEventEnd() - StartTime(); + } + + DOMHighResTimeStamp StartTime() const override + { + return 0; + } + + JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; + + DOMHighResTimeStamp UnloadEventStart() const; + DOMHighResTimeStamp UnloadEventEnd() const; + + DOMHighResTimeStamp DomInteractive() const; + DOMHighResTimeStamp DomContentLoadedEventStart() const; + DOMHighResTimeStamp DomContentLoadedEventEnd() const; + DOMHighResTimeStamp DomComplete() const; + DOMHighResTimeStamp LoadEventStart() const; + DOMHighResTimeStamp LoadEventEnd() const; + NavigationType Type() const; + uint16_t RedirectCount() const; + +private: + ~PerformanceNavigationTiming() {} +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_PerformanceNavigationTiming_h___ diff --git a/dom/performance/PerformanceObserver.cpp b/dom/performance/PerformanceObserver.cpp index 11dd30ac2..d02acfb09 100644 --- a/dom/performance/PerformanceObserver.cpp +++ b/dom/performance/PerformanceObserver.cpp @@ -114,12 +114,13 @@ PerformanceObserver::Notify() RefPtr<PerformanceObserverEntryList> list = new PerformanceObserverEntryList(this, mQueuedEntries); + mQueuedEntries.Clear(); + ErrorResult rv; mCallback->Call(this, *list, *this, rv); if (NS_WARN_IF(rv.Failed())) { rv.SuppressException(); } - mQueuedEntries.Clear(); } void @@ -170,6 +171,17 @@ PerformanceObserver::Observe(const PerformanceObserverInit& aOptions, mEntryTypes.SwapElements(validEntryTypes); mPerformance->AddObserver(this); + + if (aOptions.mBuffered) { + for (auto entryType : mEntryTypes) { + nsTArray<RefPtr<PerformanceEntry>> existingEntries; + mPerformance->GetEntriesByType(entryType, existingEntries); + if (!existingEntries.IsEmpty()) { + mQueuedEntries.AppendElements(existingEntries); + } + } + } + mConnected = true; } diff --git a/dom/performance/PerformanceObserverEntryList.cpp b/dom/performance/PerformanceObserverEntryList.cpp index 349103f08..20e818f3d 100644 --- a/dom/performance/PerformanceObserverEntryList.cpp +++ b/dom/performance/PerformanceObserverEntryList.cpp @@ -66,6 +66,7 @@ PerformanceObserverEntryList::GetEntries( aRetval.AppendElement(entry); } + aRetval.Sort(PerformanceEntryComparator()); } void @@ -79,6 +80,7 @@ PerformanceObserverEntryList::GetEntriesByType( aRetval.AppendElement(entry); } } + aRetval.Sort(PerformanceEntryComparator()); } void @@ -88,9 +90,18 @@ PerformanceObserverEntryList::GetEntriesByName( nsTArray<RefPtr<PerformanceEntry>>& aRetval) { aRetval.Clear(); + const bool typePassed = aEntryType.WasPassed(); for (const RefPtr<PerformanceEntry>& entry : mEntries) { - if (entry->GetName().Equals(aName)) { - aRetval.AppendElement(entry); + if (!entry->GetName().Equals(aName)) { + continue; } + + if (typePassed && + !entry->GetEntryType().Equals(aEntryType.Value())) { + continue; + } + + aRetval.AppendElement(entry); } + aRetval.Sort(PerformanceEntryComparator()); } diff --git a/dom/performance/PerformanceResourceTiming.cpp b/dom/performance/PerformanceResourceTiming.cpp index 60a20ca28..2eaa4eb9a 100644 --- a/dom/performance/PerformanceResourceTiming.cpp +++ b/dom/performance/PerformanceResourceTiming.cpp @@ -25,7 +25,8 @@ NS_IMPL_RELEASE_INHERITED(PerformanceResourceTiming, PerformanceEntry) PerformanceResourceTiming::PerformanceResourceTiming(PerformanceTiming* aPerformanceTiming, Performance* aPerformance, - const nsAString& aName) + const nsAString& aName, + nsIHttpChannel* aChannel) : PerformanceEntry(aPerformance, aName, NS_LITERAL_STRING("resource")), mTiming(aPerformanceTiming), mEncodedBodySize(0), @@ -33,6 +34,34 @@ PerformanceResourceTiming::PerformanceResourceTiming(PerformanceTiming* aPerform mDecodedBodySize(0) { MOZ_ASSERT(aPerformance, "Parent performance object should be provided"); + SetPropertiesFromChannel(aChannel); +} + +void +PerformanceResourceTiming::SetPropertiesFromChannel(nsIHttpChannel* aChannel) +{ + if (!aChannel) { + return; + } + + nsAutoCString protocol; + Unused << aChannel->GetProtocolVersion(protocol); + SetNextHopProtocol(NS_ConvertUTF8toUTF16(protocol)); + + uint64_t encodedBodySize = 0; + Unused << aChannel->GetEncodedBodySize(&encodedBodySize); + SetEncodedBodySize(encodedBodySize); + + uint64_t transferSize = 0; + Unused << aChannel->GetTransferSize(&transferSize); + SetTransferSize(transferSize); + + uint64_t decodedBodySize = 0; + Unused << aChannel->GetDecodedBodySize(&decodedBodySize); + if (decodedBodySize == 0) { + decodedBodySize = encodedBodySize; + } + SetDecodedBodySize(decodedBodySize); } PerformanceResourceTiming::~PerformanceResourceTiming() @@ -42,8 +71,22 @@ PerformanceResourceTiming::~PerformanceResourceTiming() DOMHighResTimeStamp PerformanceResourceTiming::StartTime() const { - DOMHighResTimeStamp startTime = mTiming->RedirectStartHighRes(); - return startTime ? startTime : mTiming->FetchStartHighRes(); + // Force the start time to be the earliest of: + // - RedirectStart + // - WorkerStart + // - AsyncOpen + // Ignore zero values. The RedirectStart and WorkerStart values + // can come from earlier redirected channels prior to the AsyncOpen + // time being recorded. + DOMHighResTimeStamp redirect = mTiming->RedirectStartHighRes(); + redirect = redirect ? redirect : DBL_MAX; + + DOMHighResTimeStamp worker = mTiming->WorkerStartHighRes(); + worker = worker ? worker : DBL_MAX; + + DOMHighResTimeStamp asyncOpen = mTiming->AsyncOpenHighRes(); + + return std::min(asyncOpen, std::min(redirect, worker)); } JSObject* diff --git a/dom/performance/PerformanceResourceTiming.h b/dom/performance/PerformanceResourceTiming.h index 2dd6b4a06..98a03327e 100644 --- a/dom/performance/PerformanceResourceTiming.h +++ b/dom/performance/PerformanceResourceTiming.h @@ -18,7 +18,7 @@ namespace mozilla { namespace dom { // http://www.w3.org/TR/resource-timing/#performanceresourcetiming -class PerformanceResourceTiming final : public PerformanceEntry +class PerformanceResourceTiming : public PerformanceEntry { public: typedef mozilla::TimeStamp TimeStamp; @@ -30,7 +30,8 @@ public: PerformanceResourceTiming(PerformanceTiming* aPerformanceTiming, Performance* aPerformance, - const nsAString& aName); + const nsAString& aName, + nsIHttpChannel* aChannel = nullptr); virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; @@ -62,6 +63,12 @@ public: mNextHopProtocol = aNextHopProtocol; } + DOMHighResTimeStamp WorkerStart() const { + return mTiming && mTiming->TimingAllowed() + ? mTiming->WorkerStartHighRes() + : 0; + } + DOMHighResTimeStamp FetchStart() const { return mTiming ? mTiming->FetchStartHighRes() @@ -170,6 +177,7 @@ public: protected: virtual ~PerformanceResourceTiming(); + void SetPropertiesFromChannel(nsIHttpChannel* aChannel); nsString mInitiatorType; nsString mNextHopProtocol; diff --git a/dom/performance/PerformanceService.cpp b/dom/performance/PerformanceService.cpp new file mode 100644 index 000000000..cf119af89 --- /dev/null +++ b/dom/performance/PerformanceService.cpp @@ -0,0 +1,46 @@ +/* -*- 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 "PerformanceService.h" + +#include "mozilla/ClearOnShutdown.h" +#include "mozilla/StaticMutex.h" +#include "mozilla/StaticPtr.h" + +namespace mozilla { +namespace dom { + +static StaticRefPtr<PerformanceService> gPerformanceService; +static StaticMutex gPerformanceServiceMutex; + +/* static */ PerformanceService* +PerformanceService::GetOrCreate() +{ + StaticMutexAutoLock al(gPerformanceServiceMutex); + + if (!gPerformanceService) { + gPerformanceService = new PerformanceService(); + ClearOnShutdown(&gPerformanceService); + } + + return gPerformanceService; +} + +DOMHighResTimeStamp +PerformanceService::TimeOrigin(const TimeStamp& aCreationTimeStamp) const +{ + return (aCreationTimeStamp - mCreationTimeStamp).ToMilliseconds() + + (mCreationEpochTime / PR_USEC_PER_MSEC); +} + +PerformanceService::PerformanceService() +{ + mCreationTimeStamp = TimeStamp::Now(); + mCreationEpochTime = PR_Now(); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/performance/PerformanceService.h b/dom/performance/PerformanceService.h new file mode 100644 index 000000000..9abbd674d --- /dev/null +++ b/dom/performance/PerformanceService.h @@ -0,0 +1,48 @@ +/* -*- 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/. */ + +#ifndef dom_performance_PerformanceService_h +#define dom_performance_PerformanceService_h + +#include "mozilla/TimeStamp.h" +#include "nsCOMPtr.h" +#include "nsDOMNavigationTiming.h" + +namespace mozilla { +namespace dom { + +// This class is thread-safe. + +// We use this singleton for having the correct value of performance.timeOrigin. +// This value must be calculated on top of the pair: +// - mCreationTimeStamp (monotonic clock) +// - mCreationEpochTime (unix epoch time) +// These 2 values must be taken "at the same time" in order to be used +// correctly. + +class PerformanceService +{ +public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PerformanceService) + + static PerformanceService* + GetOrCreate(); + + DOMHighResTimeStamp + TimeOrigin(const TimeStamp& aCreationTimeStamp) const; + +private: + PerformanceService(); + ~PerformanceService() = default; + + TimeStamp mCreationTimeStamp; + PRTime mCreationEpochTime; +}; + +} // dom namespace +} // mozilla namespace + +#endif // dom_performance_PerformanceService_h diff --git a/dom/performance/PerformanceTiming.cpp b/dom/performance/PerformanceTiming.cpp index e2f76a21f..887a23938 100755 --- a/dom/performance/PerformanceTiming.cpp +++ b/dom/performance/PerformanceTiming.cpp @@ -73,6 +73,7 @@ PerformanceTiming::InitializeTimingInfo(nsITimedChannel* aChannel) { if (aChannel) { aChannel->GetAsyncOpen(&mAsyncOpen); + aChannel->GetDispatchFetchEventStart(&mWorkerStart); aChannel->GetAllRedirectsSameOrigin(&mAllRedirectsSameOrigin); aChannel->GetRedirectCount(&mRedirectCount); aChannel->GetRedirectStart(&mRedirectStart); @@ -88,31 +89,39 @@ PerformanceTiming::InitializeTimingInfo(nsITimedChannel* aChannel) aChannel->GetResponseEnd(&mResponseEnd); aChannel->GetCacheReadEnd(&mCacheReadEnd); - // the performance timing api essentially requires that the event timestamps - // are >= asyncOpen().. but in truth the browser engages in a number of - // speculative activities that sometimes mean connections and lookups begin - // earlier. Workaround that here by just using asyncOpen as the minimum - // timestamp for dns and connection info. + // The performance timing api essentially requires that the event timestamps + // have a strict relation with each other. The truth, however, is the browser + // engages in a number of speculative activities that sometimes mean connections + // and lookups begin at different times. Workaround that here by clamping + // these values to what we expect FetchStart to be. This means the later of + // AsyncOpen or WorkerStart times. if (!mAsyncOpen.IsNull()) { - if (!mDomainLookupStart.IsNull() && mDomainLookupStart < mAsyncOpen) { - mDomainLookupStart = mAsyncOpen; + // We want to clamp to the expected FetchStart value. This is later of + // the AsyncOpen and WorkerStart values. + const TimeStamp* clampTime = &mAsyncOpen; + if (!mWorkerStart.IsNull() && mWorkerStart > mAsyncOpen) { + clampTime = &mWorkerStart; } - if (!mDomainLookupEnd.IsNull() && mDomainLookupEnd < mAsyncOpen) { - mDomainLookupEnd = mAsyncOpen; + if (!mDomainLookupStart.IsNull() && mDomainLookupStart < *clampTime) { + mDomainLookupStart = *clampTime; } - if (!mConnectStart.IsNull() && mConnectStart < mAsyncOpen) { - mConnectStart = mAsyncOpen; + if (!mDomainLookupEnd.IsNull() && mDomainLookupEnd < *clampTime) { + mDomainLookupEnd = *clampTime; + } + + if (!mConnectStart.IsNull() && mConnectStart < *clampTime) { + mConnectStart = *clampTime; } if (mSecureConnection && !mSecureConnectionStart.IsNull() && - mSecureConnectionStart < mAsyncOpen) { - mSecureConnectionStart = mAsyncOpen; + mSecureConnectionStart < *clampTime) { + mSecureConnectionStart = *clampTime; } - if (!mConnectEnd.IsNull() && mConnectEnd < mAsyncOpen) { - mConnectEnd = mAsyncOpen; + if (!mConnectEnd.IsNull() && mConnectEnd < *clampTime) { + mConnectEnd = *clampTime; } } } @@ -131,9 +140,13 @@ PerformanceTiming::FetchStartHighRes() } MOZ_ASSERT(!mAsyncOpen.IsNull(), "The fetch start time stamp should always be " "valid if the performance timing is enabled"); - mFetchStart = (!mAsyncOpen.IsNull()) - ? TimeStampToDOMHighRes(mAsyncOpen) - : 0.0; + if (!mAsyncOpen.IsNull()) { + if (!mWorkerStart.IsNull() && mWorkerStart > mAsyncOpen) { + mFetchStart = TimeStampToDOMHighRes(mWorkerStart); + } else { + mFetchStart = TimeStampToDOMHighRes(mAsyncOpen); + } + } } return TimerClamping::ReduceMsTimeValue(mFetchStart); } @@ -180,7 +193,7 @@ PerformanceTiming::TimingAllowed() const return mTimingAllowed; } -uint16_t +uint8_t PerformanceTiming::GetRedirectCount() const { if (!nsContentUtils::IsPerformanceTimingEnabled() || !IsInitialized()) { @@ -205,6 +218,26 @@ PerformanceTiming::ShouldReportCrossOriginRedirect() const return (mRedirectCount != 0) && mReportCrossOriginRedirect; } +DOMHighResTimeStamp +PerformanceTiming::AsyncOpenHighRes() +{ + if (!nsContentUtils::IsPerformanceTimingEnabled() || !IsInitialized() || + mAsyncOpen.IsNull()) { + return mZeroTime; + } + return TimeStampToReducedDOMHighResOrFetchStart(mAsyncOpen); +} + +DOMHighResTimeStamp +PerformanceTiming::WorkerStartHighRes() +{ + if (!nsContentUtils::IsPerformanceTimingEnabled() || !IsInitialized() || + mWorkerStart.IsNull()) { + return mZeroTime; + } + return TimeStampToReducedDOMHighResOrFetchStart(mWorkerStart); +} + /** * RedirectStartHighRes() is used by both the navigation timing and the * resource timing. Since, navigation timing and resource timing check and diff --git a/dom/performance/PerformanceTiming.h b/dom/performance/PerformanceTiming.h index fc7e7d5bd..435e1bca1 100755 --- a/dom/performance/PerformanceTiming.h +++ b/dom/performance/PerformanceTiming.h @@ -139,7 +139,7 @@ public: return TimerClamping::ReduceMsTimeValue(GetDOMTiming()->GetUnloadEventEnd()); } - uint16_t GetRedirectCount() const; + uint8_t GetRedirectCount() const; // Checks if the resource is either same origin as the page that started // the load, or if the response contains the Timing-Allow-Origin header @@ -155,7 +155,12 @@ public: // the timing-allow-origin check in HttpBaseChannel::TimingAllowCheck bool ShouldReportCrossOriginRedirect() const; + // The last channel's AsyncOpen time. This may occur before the FetchStart + // in some cases. + DOMHighResTimeStamp AsyncOpenHighRes(); + // High resolution (used by resource timing) + DOMHighResTimeStamp WorkerStartHighRes(); DOMHighResTimeStamp FetchStartHighRes(); DOMHighResTimeStamp RedirectStartHighRes(); DOMHighResTimeStamp RedirectEndHighRes(); @@ -237,6 +242,14 @@ public: return TimerClamping::ReduceMsTimeValue(GetDOMTiming()->GetLoadEventEnd()); } + DOMTimeMilliSec TimeToNonBlankPaint() const + { + if (!nsContentUtils::IsPerformanceTimingEnabled()) { + return 0; + } + return TimerClamping::ReduceMsTimeValue(GetDOMTiming()->GetTimeToNonBlankPaint()); + } + private: ~PerformanceTiming(); @@ -253,6 +266,7 @@ private: DOMHighResTimeStamp mZeroTime; TimeStamp mAsyncOpen; + TimeStamp mWorkerStart; TimeStamp mRedirectStart; TimeStamp mRedirectEnd; TimeStamp mDomainLookupStart; @@ -265,7 +279,7 @@ private: TimeStamp mCacheReadStart; TimeStamp mResponseEnd; TimeStamp mCacheReadEnd; - uint16_t mRedirectCount; + uint8_t mRedirectCount; bool mTimingAllowed; bool mAllRedirectsSameOrigin; bool mInitialized; diff --git a/dom/performance/PerformanceWorker.cpp b/dom/performance/PerformanceWorker.cpp index 85ca2ccd8..f10c58446 100644 --- a/dom/performance/PerformanceWorker.cpp +++ b/dom/performance/PerformanceWorker.cpp @@ -23,37 +23,6 @@ PerformanceWorker::~PerformanceWorker() mWorkerPrivate->AssertIsOnWorkerThread(); } -DOMHighResTimeStamp -PerformanceWorker::Now() const -{ - TimeDuration duration = - TimeStamp::Now() - mWorkerPrivate->NowBaseTimeStamp(); - return RoundTime(duration.ToMilliseconds()); -} - -// To be removed once bug 1124165 lands -bool -PerformanceWorker::IsPerformanceTimingAttribute(const nsAString& aName) -{ - // In workers we just support navigationStart. - return aName.EqualsASCII("navigationStart"); -} - -DOMHighResTimeStamp -PerformanceWorker::GetPerformanceTimingFromString(const nsAString& aProperty) -{ - if (!IsPerformanceTimingAttribute(aProperty)) { - return 0; - } - - if (aProperty.EqualsLiteral("navigationStart")) { - return mWorkerPrivate->NowBaseTime(); - } - - MOZ_CRASH("IsPerformanceTimingAttribute and GetPerformanceTimingFromString are out of sync"); - return 0; -} - void PerformanceWorker::InsertUserEntry(PerformanceEntry* aEntry) { @@ -72,13 +41,13 @@ PerformanceWorker::InsertUserEntry(PerformanceEntry* aEntry) TimeStamp PerformanceWorker::CreationTimeStamp() const { - return mWorkerPrivate->NowBaseTimeStamp(); + return mWorkerPrivate->CreationTimeStamp(); } DOMHighResTimeStamp PerformanceWorker::CreationTime() const { - return mWorkerPrivate->NowBaseTime(); + return mWorkerPrivate->CreationTime(); } } // dom namespace diff --git a/dom/performance/PerformanceWorker.h b/dom/performance/PerformanceWorker.h index 7eef0d974..346bdd026 100644 --- a/dom/performance/PerformanceWorker.h +++ b/dom/performance/PerformanceWorker.h @@ -21,9 +21,6 @@ class PerformanceWorker final : public Performance public: explicit PerformanceWorker(workers::WorkerPrivate* aWorkerPrivate); - // Performance WebIDL methods - DOMHighResTimeStamp Now() const override; - virtual PerformanceTiming* Timing() override { MOZ_CRASH("This should not be called on workers."); @@ -64,12 +61,6 @@ public: return nullptr; } - virtual Performance* GetParentPerformance() const override - { - MOZ_CRASH("This should not be called on workers."); - return nullptr; - } - protected: ~PerformanceWorker(); @@ -80,11 +71,6 @@ protected: void InsertUserEntry(PerformanceEntry* aEntry) override; - bool IsPerformanceTimingAttribute(const nsAString& aName) override; - - DOMHighResTimeStamp - GetPerformanceTimingFromString(const nsAString& aTimingName) override; - void DispatchBufferFullEvent() override { MOZ_CRASH("This should not be called on workers."); diff --git a/dom/performance/moz.build b/dom/performance/moz.build index 3286a0a4c..e1f96fec8 100644 --- a/dom/performance/moz.build +++ b/dom/performance/moz.build @@ -10,9 +10,11 @@ EXPORTS.mozilla.dom += [ 'PerformanceMark.h', 'PerformanceMeasure.h', 'PerformanceNavigation.h', + 'PerformanceNavigationTiming.h', 'PerformanceObserver.h', 'PerformanceObserverEntryList.h', 'PerformanceResourceTiming.h', + 'PerformanceService.h', 'PerformanceTiming.h', ] @@ -23,9 +25,11 @@ UNIFIED_SOURCES += [ 'PerformanceMark.cpp', 'PerformanceMeasure.cpp', 'PerformanceNavigation.cpp', + 'PerformanceNavigationTiming.cpp', 'PerformanceObserver.cpp', 'PerformanceObserverEntryList.cpp', 'PerformanceResourceTiming.cpp', + 'PerformanceService.cpp', 'PerformanceTiming.cpp', 'PerformanceWorker.cpp', ] diff --git a/dom/performance/tests/mochitest.ini b/dom/performance/tests/mochitest.ini index 18f7f0e45..bee0b2e70 100644 --- a/dom/performance/tests/mochitest.ini +++ b/dom/performance/tests/mochitest.ini @@ -1,13 +1,11 @@ [DEFAULT] support-files = - performance_observer.html test_performance_observer.js test_performance_user_timing.js test_worker_performance_now.js worker_performance_user_timing.js worker_performance_observer.js sharedworker_performance_user_timing.js - worker_performance_observer.html [test_performance_observer.html] [test_performance_user_timing.html] @@ -15,3 +13,4 @@ support-files = [test_worker_observer.html] [test_sharedWorker_performance_user_timing.html] [test_worker_performance_now.html] +[test_timeOrigin.html] diff --git a/dom/performance/tests/performance_observer.html b/dom/performance/tests/performance_observer.html deleted file mode 100644 index b8ced9296..000000000 --- a/dom/performance/tests/performance_observer.html +++ /dev/null @@ -1,74 +0,0 @@ -<!-- - Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ ---> -<!DOCTYPE html> -<meta charset=utf-8> -<html> -<head> -<title>Test for performance observer</title> -<script> -'use strict'; -[ - "promise_test", "test", "setup", - "assert_true", "assert_equals", "assert_array_equals", - "assert_throws", "assert_unreached" -].forEach(func => { - window[func] = opener[func].bind(opener); -}); -function done() { - opener.add_completion_callback(() => { - self.close(); - }); - opener.done(); -} - -</script> -<script src="test_performance_observer.js"></script> -</head> -<body> -<div id="log"></div> -<script> -function makeXHR(aUrl) { - var xmlhttp = new XMLHttpRequest(); - xmlhttp.open("get", aUrl, true); - xmlhttp.send(); -} - -promise_test(t => { - var promise = new Promise(resolve => { - performance.clearResourceTimings(); - - var observer = new PerformanceObserver(list => resolve(list)); - observer.observe({entryTypes: ['resource']}); - t.add_cleanup(() => observer.disconnect()); - }); - - makeXHR("test-data.json"); - - return promise.then(list => { - assert_equals(list.getEntries().length, 1); - assert_array_equals(list.getEntries(), - performance.getEntriesByType("resource"), - "Observed 'resource' entries should equal to entries obtained by getEntriesByType."); - - // getEntries filtering tests - assert_array_equals(list.getEntries({name: "http://mochi.test:8888/tests/dom/base/test/test-data.json"}), - performance.getEntriesByName("http://mochi.test:8888/tests/dom/base/test/test-data.json"), - "getEntries with name filter should return correct results."); - assert_array_equals(list.getEntries({entryType: "resource"}), - performance.getEntriesByType("resource"), - "getEntries with entryType filter should return correct results."); - assert_array_equals(list.getEntries({initiatorType: "xmlhttprequest"}), - performance.getEntriesByType("resource"), - "getEntries with initiatorType filter should return correct results."); - assert_array_equals(list.getEntries({initiatorType: "link"}), - [], - "getEntries with non-existent initiatorType filter should return an empty array."); - }); -}, "resource-timing test"); - -done(); - -</script> -</body> diff --git a/dom/performance/tests/test_performance_observer.html b/dom/performance/tests/test_performance_observer.html index d36878315..7df881bd4 100644 --- a/dom/performance/tests/test_performance_observer.html +++ b/dom/performance/tests/test_performance_observer.html @@ -3,15 +3,55 @@ http://creativecommons.org/publicdomain/zero/1.0/ --> <!DOCTYPE html> +<html> +<head> <meta charset=utf-8> <title>Test for performance observer</title> <script src="/resources/testharness.js"></script> <script src="/resources/testharnessreport.js"></script> -<div id=log></div> +</head> +<body> +<div id="log"></div> +<script src="test_performance_observer.js"></script> <script> -'use strict'; -SpecialPowers.pushPrefEnv({"set": [["dom.enable_performance_observer", true]]}, - function() { - window.open("performance_observer.html"); - }); +function makeXHR(aUrl) { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.open("get", aUrl, true); + xmlhttp.send(); +} + +promise_test(t => { + var promise = new Promise(resolve => { + performance.clearResourceTimings(); + + var observer = new PerformanceObserver(list => resolve(list)); + observer.observe({entryTypes: ['resource']}); + t.add_cleanup(() => observer.disconnect()); + }); + + makeXHR("test-data.json"); + + return promise.then(list => { + assert_equals(list.getEntries().length, 1); + assert_array_equals(list.getEntries(), + performance.getEntriesByType("resource"), + "Observed 'resource' entries should equal to entries obtained by getEntriesByType."); + + // getEntries filtering tests + assert_array_equals(list.getEntries({name: "http://mochi.test:8888/tests/dom/base/test/test-data.json"}), + performance.getEntriesByName("http://mochi.test:8888/tests/dom/base/test/test-data.json"), + "getEntries with name filter should return correct results."); + assert_array_equals(list.getEntries({entryType: "resource"}), + performance.getEntriesByType("resource"), + "getEntries with entryType filter should return correct results."); + assert_array_equals(list.getEntries({initiatorType: "xmlhttprequest"}), + performance.getEntriesByType("resource"), + "getEntries with initiatorType filter should return correct results."); + assert_array_equals(list.getEntries({initiatorType: "link"}), + [], + "getEntries with non-existent initiatorType filter should return an empty array."); + }); +}, "resource-timing test"); + </script> +</body> diff --git a/dom/performance/tests/test_performance_user_timing.js b/dom/performance/tests/test_performance_user_timing.js index cd8261bbd..a15dbebb6 100644 --- a/dom/performance/tests/test_performance_user_timing.js +++ b/dom/performance/tests/test_performance_user_timing.js @@ -126,15 +126,18 @@ var steps = [ }, // Test measure function () { - ok(true, "Running measure addition with no start/end time test"); - performance.measure("test"); - var measures = performance.getEntriesByType("measure"); - is(measures.length, 1, "number of measures should be 1"); - var measure = measures[0]; - is(measure.name, "test", "measure name should be 'test'"); - is(measure.entryType, "measure", "measure type should be 'measure'"); - is(measure.startTime, 0, "measure start time should be zero"); - ok(measure.duration >= 0, "measure duration should not be negative"); + // We don't have navigationStart in workers. + if ("window" in self) { + ok(true, "Running measure addition with no start/end time test"); + performance.measure("test", "navigationStart"); + var measures = performance.getEntriesByType("measure"); + is(measures.length, 1, "number of measures should be 1"); + var measure = measures[0]; + is(measure.name, "test", "measure name should be 'test'"); + is(measure.entryType, "measure", "measure type should be 'measure'"); + is(measure.startTime, 0, "measure start time should be zero"); + ok(measure.duration >= 0, "measure duration should not be negative"); + } }, function () { ok(true, "Running measure addition with only start time test"); diff --git a/dom/performance/tests/test_timeOrigin.html b/dom/performance/tests/test_timeOrigin.html new file mode 100644 index 000000000..5a8a461f3 --- /dev/null +++ b/dom/performance/tests/test_timeOrigin.html @@ -0,0 +1,68 @@ +<!DOCTYPE HTML> +<html> + <head> + <title>Test for performance.timeOrigin</title> + <meta http-equiv="content-type" content="text/html; charset=UTF-8"> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="test_performance_user_timing.js"></script> + </head> + <body> + <script type="text/js-worker" id="worker-src"> + postMessage({ now: performance.now(), timeOrigin: performance.timeOrigin }); + </script> + + <script type="text/js-worker" id="shared-worker-src"> + onconnect = function(evt) { + evt.ports[0].postMessage({ now: performance.now(), timeOrigin: performance.timeOrigin }); + }; + </script> + + <script class="testbody" type="text/javascript"> + +function testBasic() { + ok("timeOrigin" in performance, "Performance.timeOrigin exists."); + ok(performance.timeOrigin > 0, "TimeOrigin must be greater than 0."); + next(); +} + +function testWorker() { + var now = performance.now(); + + var blob = new Blob([ document.getElementById("worker-src").textContent ], + { type: "text/javascript" }); + var w = new Worker(URL.createObjectURL(blob)); + w.onmessage = function(e) { + ok (e.now + e.timeOrigin > now + performance.now, "Comparing worker.now and window.now"); + next(); + } +} + +function testSharedWorker() { + var now = performance.now(); + + var blob = new Blob([ document.getElementById("shared-worker-src").textContent ], + { type: "text/javascript" }); + var w = new SharedWorker(URL.createObjectURL(blob)); + w.port.onmessage = function(e) { + ok (e.now + e.timeOrigin > now + performance.now, "Comparing worker.now and window.now"); + next(); + } +} + +var tests = [ testBasic, testWorker, testSharedWorker ]; +function next() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); +} + +SimpleTest.waitForExplicitFinish(); +addLoadEvent(next); + </script> + </pre> + </body> +</html> diff --git a/dom/performance/tests/test_worker_observer.html b/dom/performance/tests/test_worker_observer.html index b9ed0c964..9a55ef1d5 100644 --- a/dom/performance/tests/test_worker_observer.html +++ b/dom/performance/tests/test_worker_observer.html @@ -3,15 +3,16 @@ http://creativecommons.org/publicdomain/zero/1.0/ --> <!DOCTYPE html> +<html> +<head> <meta charset=utf-8> <title>Test for performance observer in worker</title> <script src="/resources/testharness.js"></script> <script src="/resources/testharnessreport.js"></script> -<div id=log></div> +</head> +<body> +<div id="log"></div> <script> -'use strict'; -SpecialPowers.pushPrefEnv({"set": [["dom.enable_performance_observer", true]]}, - function() { - window.open("worker_performance_observer.html"); - }); +fetch_tests_from_worker(new Worker("worker_performance_observer.js")); </script> +</body> diff --git a/dom/performance/tests/worker_performance_observer.html b/dom/performance/tests/worker_performance_observer.html deleted file mode 100644 index 613762f52..000000000 --- a/dom/performance/tests/worker_performance_observer.html +++ /dev/null @@ -1,32 +0,0 @@ -<!-- - Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ ---> -<!DOCTYPE html> -<meta charset=utf-8> -<html> -<head> -<title>Test for performance observer in worker</title> -</head> -<body> -<div id="log"></div> -<script> -[ - "async_test", "test", "setup", - "assert_true", "assert_equals", "assert_array_equals", - "assert_throws", "fetch_tests_from_worker" -].forEach(func => { - window[func] = opener[func].bind(opener); -}); - -function done() { - opener.add_completion_callback(() => { - self.close(); - }); - opener.done(); -} - -fetch_tests_from_worker(new Worker("worker_performance_observer.js")); -done(); -</script> -</body> |