Performance.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/DOM/EventDispatcher.h>
  10. #include <LibWeb/HTML/StructuredSerialize.h>
  11. #include <LibWeb/HTML/Window.h>
  12. #include <LibWeb/HighResolutionTime/Performance.h>
  13. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  14. #include <LibWeb/NavigationTiming/EntryNames.h>
  15. #include <LibWeb/NavigationTiming/PerformanceTiming.h>
  16. #include <LibWeb/PerformanceTimeline/EntryTypes.h>
  17. namespace Web::HighResolutionTime {
  18. JS_DEFINE_ALLOCATOR(Performance);
  19. Performance::Performance(HTML::Window& window)
  20. : DOM::EventTarget(window.realm())
  21. , m_window(window)
  22. , m_timer(Core::TimerType::Precise)
  23. {
  24. m_timer.start();
  25. }
  26. Performance::~Performance() = default;
  27. void Performance::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(Performance);
  31. }
  32. void Performance::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_window);
  36. visitor.visit(m_timing);
  37. }
  38. JS::GCPtr<NavigationTiming::PerformanceTiming> Performance::timing()
  39. {
  40. if (!m_timing)
  41. m_timing = heap().allocate<NavigationTiming::PerformanceTiming>(realm(), *m_window);
  42. return m_timing;
  43. }
  44. double Performance::time_origin() const
  45. {
  46. return static_cast<double>(m_timer.origin_time().nanoseconds()) / 1e6;
  47. }
  48. // https://w3c.github.io/user-timing/#mark-method
  49. WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMark>> Performance::mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options)
  50. {
  51. auto& realm = this->realm();
  52. // 1. Run the PerformanceMark constructor and let entry be the newly created object.
  53. auto entry = TRY(UserTiming::PerformanceMark::construct_impl(realm, mark_name, mark_options));
  54. // 2. Queue entry.
  55. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  56. VERIFY(window_or_worker);
  57. window_or_worker->queue_performance_entry(entry);
  58. // 3. Add entry to the performance entry buffer.
  59. // FIXME: This seems to be a holdover from moving to the `queue` structure for PerformanceObserver, as this would cause a double append.
  60. // 4. Return entry.
  61. return entry;
  62. }
  63. void Performance::clear_marks(Optional<String> mark_name)
  64. {
  65. auto& realm = this->realm();
  66. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  67. VERIFY(window_or_worker);
  68. // 1. If markName is omitted, remove all PerformanceMark objects from the performance entry buffer.
  69. if (!mark_name.has_value()) {
  70. window_or_worker->clear_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::mark);
  71. return;
  72. }
  73. // 2. Otherwise, remove all PerformanceMark objects listed in the performance entry buffer whose name is markName.
  74. window_or_worker->remove_entries_from_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::mark, mark_name.value());
  75. // 3. Return undefined.
  76. }
  77. WebIDL::ExceptionOr<HighResolutionTime::DOMHighResTimeStamp> Performance::convert_name_to_timestamp(JS::Realm& realm, String const& name)
  78. {
  79. auto& vm = realm.vm();
  80. // 1. If the global object is not a Window object, throw a TypeError.
  81. if (!is<HTML::Window>(realm.global_object()))
  82. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("'{}' is an attribute in the PerformanceTiming interface and thus can only be used in a Window context", name)) };
  83. // 2. If name is navigationStart, return 0.
  84. if (name == NavigationTiming::EntryNames::navigationStart)
  85. return 0.0;
  86. auto timing_interface = timing();
  87. VERIFY(timing_interface);
  88. // 3. Let startTime be the value of navigationStart in the PerformanceTiming interface.
  89. auto start_time = timing_interface->navigation_start();
  90. // 4. Let endTime be the value of name in the PerformanceTiming interface.
  91. u64 end_time { 0 };
  92. #define __ENUMERATE_NAVIGATION_TIMING_ENTRY_NAME(camel_case_name, snake_case_name) \
  93. if (name == NavigationTiming::EntryNames::camel_case_name) \
  94. end_time = timing_interface->snake_case_name();
  95. ENUMERATE_NAVIGATION_TIMING_ENTRY_NAMES
  96. #undef __ENUMERATE_NAVIGATION_TIMING_ENTRY_NAME
  97. // 5. If endTime is 0, throw an InvalidAccessError.
  98. if (end_time == 0)
  99. return WebIDL::InvalidAccessError::create(realm, MUST(String::formatted("The '{}' entry in the PerformanceTiming interface is equal to 0, meaning it hasn't happened yet", name)));
  100. // 6. Return result of subtracting startTime from endTime.
  101. return static_cast<HighResolutionTime::DOMHighResTimeStamp>(end_time - start_time);
  102. }
  103. // https://w3c.github.io/user-timing/#dfn-convert-a-mark-to-a-timestamp
  104. WebIDL::ExceptionOr<HighResolutionTime::DOMHighResTimeStamp> Performance::convert_mark_to_timestamp(JS::Realm& realm, Variant<String, HighResolutionTime::DOMHighResTimeStamp> mark)
  105. {
  106. if (mark.has<String>()) {
  107. auto const& mark_string = mark.get<String>();
  108. // 1. If mark is a DOMString and it has the same name as a read only attribute in the PerformanceTiming interface, let end
  109. // time be the value returned by running the convert a name to a timestamp algorithm with name set to the value of mark.
  110. #define __ENUMERATE_NAVIGATION_TIMING_ENTRY_NAME(name, _) \
  111. if (mark_string == NavigationTiming::EntryNames::name) \
  112. return convert_name_to_timestamp(realm, mark_string);
  113. ENUMERATE_NAVIGATION_TIMING_ENTRY_NAMES
  114. #undef __ENUMERATE_NAVIGATION_TIMING_ENTRY_NAME
  115. // 2. Otherwise, if mark is a DOMString, let end time be the value of the startTime attribute from the most recent occurrence
  116. // of a PerformanceMark object in the performance entry buffer whose name is mark. If no matching entry is found, throw a
  117. // SyntaxError.
  118. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  119. VERIFY(window_or_worker);
  120. auto& tuple = window_or_worker->relevant_performance_entry_tuple(PerformanceTimeline::EntryTypes::mark);
  121. auto& performance_entry_buffer = tuple.performance_entry_buffer;
  122. auto maybe_entry = performance_entry_buffer.last_matching([&mark_string](JS::Handle<PerformanceTimeline::PerformanceEntry> const& entry) {
  123. return entry->name() == mark_string;
  124. });
  125. if (!maybe_entry.has_value())
  126. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("No PerformanceMark object with name '{}' found in the performance timeline", mark_string)));
  127. return maybe_entry.value()->start_time();
  128. }
  129. // 3. Otherwise, if mark is a DOMHighResTimeStamp:
  130. auto mark_time_stamp = mark.get<HighResolutionTime::DOMHighResTimeStamp>();
  131. // 1. If mark is negative, throw a TypeError.
  132. if (mark_time_stamp < 0.0)
  133. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot have negative time values in PerformanceMark"sv };
  134. // 2. Otherwise, let end time be mark.
  135. return mark_time_stamp;
  136. }
  137. // https://w3c.github.io/user-timing/#dom-performance-measure
  138. WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMeasure>> Performance::measure(String const& measure_name, Variant<String, UserTiming::PerformanceMeasureOptions> const& start_or_measure_options, Optional<String> end_mark)
  139. {
  140. auto& realm = this->realm();
  141. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  142. VERIFY(window_or_worker);
  143. auto& vm = this->vm();
  144. // 1. If startOrMeasureOptions is a PerformanceMeasureOptions object and at least one of start, end, duration, and detail
  145. // are present, run the following checks:
  146. auto const* start_or_measure_options_dictionary_object = start_or_measure_options.get_pointer<UserTiming::PerformanceMeasureOptions>();
  147. if (start_or_measure_options_dictionary_object
  148. && (start_or_measure_options_dictionary_object->start.has_value()
  149. || start_or_measure_options_dictionary_object->end.has_value()
  150. || start_or_measure_options_dictionary_object->duration.has_value()
  151. || !start_or_measure_options_dictionary_object->detail.is_undefined())) {
  152. // 1. If endMark is given, throw a TypeError.
  153. if (end_mark.has_value())
  154. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot provide PerformanceMeasureOptions and endMark at the same time"sv };
  155. // 2. If startOrMeasureOptions's start and end members are both omitted, throw a TypeError.
  156. if (!start_or_measure_options_dictionary_object->start.has_value() && !start_or_measure_options_dictionary_object->end.has_value())
  157. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "PerformanceMeasureOptions must contain one or both of 'start' and 'end'"sv };
  158. // 3. If startOrMeasureOptions's start, duration, and end members are all present, throw a TypeError.
  159. if (start_or_measure_options_dictionary_object->start.has_value() && start_or_measure_options_dictionary_object->end.has_value() && start_or_measure_options_dictionary_object->duration.has_value())
  160. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "PerformanceMeasureOptions cannot contain 'start', 'duration' and 'end' properties all at once"sv };
  161. }
  162. // 2. Compute end time as follows:
  163. HighResolutionTime::DOMHighResTimeStamp end_time { 0.0 };
  164. // 1. If endMark is given, let end time be the value returned by running the convert a mark to a timestamp algorithm passing
  165. // in endMark.
  166. if (end_mark.has_value()) {
  167. end_time = TRY(convert_mark_to_timestamp(realm, end_mark.value()));
  168. }
  169. // 2. Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object, and if its end member is present, let end
  170. // time be the value returned by running the convert a mark to a timestamp algorithm passing in startOrMeasureOptions's end.
  171. else if (start_or_measure_options_dictionary_object && start_or_measure_options_dictionary_object->end.has_value()) {
  172. end_time = TRY(convert_mark_to_timestamp(realm, start_or_measure_options_dictionary_object->end.value()));
  173. }
  174. // 3. Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object, and if its start and duration members are
  175. // both present:
  176. else if (start_or_measure_options_dictionary_object && start_or_measure_options_dictionary_object->start.has_value() && start_or_measure_options_dictionary_object->duration.has_value()) {
  177. // 1. Let start be the value returned by running the convert a mark to a timestamp algorithm passing in start.
  178. auto start = TRY(convert_mark_to_timestamp(realm, start_or_measure_options_dictionary_object->start.value()));
  179. // 2. Let duration be the value returned by running the convert a mark to a timestamp algorithm passing in duration.
  180. auto duration = TRY(convert_mark_to_timestamp(realm, start_or_measure_options_dictionary_object->duration.value()));
  181. // 3. Let end time be start plus duration.
  182. end_time = start + duration;
  183. }
  184. // 4. Otherwise, let end time be the value that would be returned by the Performance object's now() method.
  185. else {
  186. // FIXME: Performance#now doesn't currently use TimeOrigin's functions, update this and Performance#now to match Performance#now's specification.
  187. end_time = HighResolutionTime::unsafe_shared_current_time();
  188. }
  189. // 3. Compute start time as follows:
  190. HighResolutionTime::DOMHighResTimeStamp start_time { 0.0 };
  191. // 1. If startOrMeasureOptions is a PerformanceMeasureOptions object, and if its start member is present, let start time be
  192. // the value returned by running the convert a mark to a timestamp algorithm passing in startOrMeasureOptions's start.
  193. if (start_or_measure_options_dictionary_object && start_or_measure_options_dictionary_object->start.has_value()) {
  194. start_time = TRY(convert_mark_to_timestamp(realm, start_or_measure_options_dictionary_object->start.value()));
  195. }
  196. // 2. Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object, and if its duration and end members are
  197. // both present:
  198. else if (start_or_measure_options_dictionary_object && start_or_measure_options_dictionary_object->duration.has_value() && start_or_measure_options_dictionary_object->end.has_value()) {
  199. // 1. Let duration be the value returned by running the convert a mark to a timestamp algorithm passing in duration.
  200. auto duration = TRY(convert_mark_to_timestamp(realm, start_or_measure_options_dictionary_object->duration.value()));
  201. // 2. Let end be the value returned by running the convert a mark to a timestamp algorithm passing in end.
  202. auto end = TRY(convert_mark_to_timestamp(realm, start_or_measure_options_dictionary_object->end.value()));
  203. // 3. Let start time be end minus duration.
  204. start_time = end - duration;
  205. }
  206. // 3. Otherwise, if startOrMeasureOptions is a DOMString, let start time be the value returned by running the convert a mark
  207. // to a timestamp algorithm passing in startOrMeasureOptions.
  208. else if (start_or_measure_options.has<String>()) {
  209. start_time = TRY(convert_mark_to_timestamp(realm, start_or_measure_options.get<String>()));
  210. }
  211. // 4. Otherwise, let start time be 0.
  212. else {
  213. start_time = 0.0;
  214. }
  215. // NOTE: Step 4 (creating the entry) is done after determining values, as we set the values once during creation and never
  216. // change them after.
  217. // 5. Set entry's name attribute to measureName.
  218. // NOTE: Will be done during construction.
  219. // 6. Set entry's entryType attribute to DOMString "measure".
  220. // NOTE: Already done via the `entry_type` virtual function.
  221. // 7. Set entry's startTime attribute to start time.
  222. // NOTE: Will be done during construction.
  223. // 8. Set entry's duration attribute to the duration from start time to end time. The resulting duration value MAY be negative.
  224. auto duration = end_time - start_time;
  225. // 9. Set entry's detail attribute as follows:
  226. JS::Value detail { JS::js_null() };
  227. // 1. If startOrMeasureOptions is a PerformanceMeasureOptions object and startOrMeasureOptions's detail member is present:
  228. if (start_or_measure_options_dictionary_object && !start_or_measure_options_dictionary_object->detail.is_undefined()) {
  229. // 1. Let record be the result of calling the StructuredSerialize algorithm on startOrMeasureOptions's detail.
  230. auto record = TRY(HTML::structured_serialize(vm, start_or_measure_options_dictionary_object->detail));
  231. // 2. Set entry's detail to the result of calling the StructuredDeserialize algorithm on record and the current realm.
  232. detail = TRY(HTML::structured_deserialize(vm, record, realm, Optional<HTML::DeserializationMemory> {}));
  233. }
  234. // 2. Otherwise, set it to null.
  235. // NOTE: Already the default value of `detail`.
  236. // 4. Create a new PerformanceMeasure object (entry) with this's relevant realm.
  237. auto entry = realm.heap().allocate<UserTiming::PerformanceMeasure>(realm, realm, measure_name, start_time, duration, detail);
  238. // 10. Queue entry.
  239. window_or_worker->queue_performance_entry(entry);
  240. // 11. Add entry to the performance entry buffer.
  241. // FIXME: This seems to be a holdover from moving to the `queue` structure for PerformanceObserver, as this would cause a double append.
  242. // 12. Return entry.
  243. return entry;
  244. }
  245. // https://w3c.github.io/user-timing/#dom-performance-clearmeasures
  246. void Performance::clear_measures(Optional<String> measure_name)
  247. {
  248. auto& realm = this->realm();
  249. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  250. VERIFY(window_or_worker);
  251. // 1. If measureName is omitted, remove all PerformanceMeasure objects in the performance entry buffer.
  252. if (!measure_name.has_value()) {
  253. window_or_worker->clear_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::measure);
  254. return;
  255. }
  256. // 2. Otherwise remove all PerformanceMeasure objects listed in the performance entry buffer whose name is measureName.
  257. window_or_worker->remove_entries_from_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::measure, measure_name.value());
  258. // 3. Return undefined.
  259. }
  260. // https://www.w3.org/TR/performance-timeline/#getentries-method
  261. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries() const
  262. {
  263. auto& realm = this->realm();
  264. auto& vm = this->vm();
  265. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  266. VERIFY(window_or_worker);
  267. // Returns a PerformanceEntryList object returned by the filter buffer map by name and type algorithm with name and
  268. // type set to null.
  269. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, /* type= */ Optional<String> {}));
  270. }
  271. // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbytype
  272. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_type(String const& type) const
  273. {
  274. auto& realm = this->realm();
  275. auto& vm = this->vm();
  276. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  277. VERIFY(window_or_worker);
  278. // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to null,
  279. // and type set to the method's input type parameter.
  280. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, type));
  281. }
  282. // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbyname
  283. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_name(String const& name, Optional<String> type) const
  284. {
  285. auto& realm = this->realm();
  286. auto& vm = this->vm();
  287. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  288. VERIFY(window_or_worker);
  289. // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to the
  290. // method input name parameter, and type set to null if optional entryType is omitted, or set to the method's input type
  291. // parameter otherwise.
  292. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(name, type));
  293. }
  294. }