2 * Copyright (C) 2012 Intel Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/timing/PerformanceUserTiming.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "core/timing/Performance.h"
32 #include "core/timing/PerformanceMark.h"
33 #include "core/timing/PerformanceMeasure.h"
34 #include "public/platform/Platform.h"
40 using RestrictedKeyMap = HashMap<String, NavigationTimingFunction>;
41 static RestrictedKeyMap restrictedKeyMap()
43 DEFINE_STATIC_LOCAL(RestrictedKeyMap, map, ());
45 map.add("navigationStart", &PerformanceTiming::navigationStart);
46 map.add("unloadEventStart", &PerformanceTiming::unloadEventStart);
47 map.add("unloadEventEnd", &PerformanceTiming::unloadEventEnd);
48 map.add("redirectStart", &PerformanceTiming::redirectStart);
49 map.add("redirectEnd", &PerformanceTiming::redirectEnd);
50 map.add("fetchStart", &PerformanceTiming::fetchStart);
51 map.add("domainLookupStart", &PerformanceTiming::domainLookupStart);
52 map.add("domainLookupEnd", &PerformanceTiming::domainLookupEnd);
53 map.add("connectStart", &PerformanceTiming::connectStart);
54 map.add("connectEnd", &PerformanceTiming::connectEnd);
55 map.add("secureConnectionStart", &PerformanceTiming::secureConnectionStart);
56 map.add("requestStart", &PerformanceTiming::requestStart);
57 map.add("responseStart", &PerformanceTiming::responseStart);
58 map.add("responseEnd", &PerformanceTiming::responseEnd);
59 map.add("domLoading", &PerformanceTiming::domLoading);
60 map.add("domInteractive", &PerformanceTiming::domInteractive);
61 map.add("domContentLoadedEventStart", &PerformanceTiming::domContentLoadedEventStart);
62 map.add("domContentLoadedEventEnd", &PerformanceTiming::domContentLoadedEventEnd);
63 map.add("domComplete", &PerformanceTiming::domComplete);
64 map.add("loadEventStart", &PerformanceTiming::loadEventStart);
65 map.add("loadEventEnd", &PerformanceTiming::loadEventEnd);
70 } // namespace anonymous
72 UserTiming::UserTiming(Performance* performance)
73 : m_performance(performance)
77 static void insertPerformanceEntry(PerformanceEntryMap& performanceEntryMap, PassRefPtrWillBeRawPtr<PerformanceEntry> performanceEntry)
79 RefPtrWillBeRawPtr<PerformanceEntry> entry = performanceEntry;
80 PerformanceEntryMap::iterator it = performanceEntryMap.find(entry->name());
81 if (it != performanceEntryMap.end())
82 it->value.append(entry);
84 PerformanceEntryVector vector(1);
86 performanceEntryMap.set(entry->name(), vector);
90 static void clearPeformanceEntries(PerformanceEntryMap& performanceEntryMap, const String& name)
93 performanceEntryMap.clear();
97 if (performanceEntryMap.contains(name))
98 performanceEntryMap.remove(name);
101 void UserTiming::mark(const String& markName, ExceptionState& exceptionState)
103 if (restrictedKeyMap().contains(markName)) {
104 exceptionState.throwDOMException(SyntaxError, "'" + markName + "' is part of the PerformanceTiming interface, and cannot be used as a mark name.");
108 double startTime = m_performance->now();
109 insertPerformanceEntry(m_marksMap, PerformanceMark::create(markName, startTime));
110 blink::Platform::current()->histogramCustomCounts("PLT.UserTiming_Mark", static_cast<int>(startTime), 0, 600000, 100);
113 void UserTiming::clearMarks(const String& markName)
115 clearPeformanceEntries(m_marksMap, markName);
118 double UserTiming::findExistingMarkStartTime(const String& markName, ExceptionState& exceptionState)
120 if (m_marksMap.contains(markName))
121 return m_marksMap.get(markName).last()->startTime();
123 if (restrictedKeyMap().contains(markName)) {
124 double value = static_cast<double>((m_performance->timing()->*(restrictedKeyMap().get(markName)))());
126 exceptionState.throwDOMException(InvalidAccessError, "'" + markName + "' is empty: either the event hasn't happened yet, or it would provide cross-origin timing information.");
129 return value - m_performance->timing()->navigationStart();
132 exceptionState.throwDOMException(SyntaxError, "The mark '" + markName + "' does not exist.");
136 void UserTiming::measure(const String& measureName, const String& startMark, const String& endMark, ExceptionState& exceptionState)
138 double startTime = 0.0;
139 double endTime = 0.0;
141 if (startMark.isNull())
142 endTime = m_performance->now();
143 else if (endMark.isNull()) {
144 endTime = m_performance->now();
145 startTime = findExistingMarkStartTime(startMark, exceptionState);
146 if (exceptionState.hadException())
149 endTime = findExistingMarkStartTime(endMark, exceptionState);
150 if (exceptionState.hadException())
152 startTime = findExistingMarkStartTime(startMark, exceptionState);
153 if (exceptionState.hadException())
157 insertPerformanceEntry(m_measuresMap, PerformanceMeasure::create(measureName, startTime, endTime));
158 if (endTime >= startTime)
159 blink::Platform::current()->histogramCustomCounts("PLT.UserTiming_MeasureDuration", static_cast<int>(endTime - startTime), 0, 600000, 100);
162 void UserTiming::clearMeasures(const String& measureName)
164 clearPeformanceEntries(m_measuresMap, measureName);
167 static PerformanceEntryVector convertToEntrySequence(const PerformanceEntryMap& performanceEntryMap)
169 PerformanceEntryVector entries;
171 for (const auto& entry : performanceEntryMap)
172 entries.appendVector(entry.value);
177 static PerformanceEntryVector getEntrySequenceByName(const PerformanceEntryMap& performanceEntryMap, const String& name)
179 PerformanceEntryVector entries;
181 PerformanceEntryMap::const_iterator it = performanceEntryMap.find(name);
182 if (it != performanceEntryMap.end())
183 entries.appendVector(it->value);
188 PerformanceEntryVector UserTiming::getMarks() const
190 return convertToEntrySequence(m_marksMap);
193 PerformanceEntryVector UserTiming::getMarks(const String& name) const
195 return getEntrySequenceByName(m_marksMap, name);
198 PerformanceEntryVector UserTiming::getMeasures() const
200 return convertToEntrySequence(m_measuresMap);
203 PerformanceEntryVector UserTiming::getMeasures(const String& name) const
205 return getEntrySequenceByName(m_measuresMap, name);
208 void UserTiming::trace(Visitor* visitor)
211 visitor->trace(m_performance);
212 visitor->trace(m_marksMap);
213 visitor->trace(m_measuresMap);