mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 04:39:06 +00:00
LibWeb: Bring performance.now() closer to spec
This commit is contained in:
parent
f31c18756b
commit
f6991a2955
Notes:
github-actions[bot]
2024-10-31 13:11:10 +00:00
Author: https://github.com/stelar7
Commit: f6991a2955
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1639
Reviewed-by: https://github.com/tcl3
5 changed files with 62 additions and 5 deletions
1
Tests/LibWeb/Text/expected/performance-now.txt
Normal file
1
Tests/LibWeb/Text/expected/performance-now.txt
Normal file
|
@ -0,0 +1 @@
|
|||
PASS
|
25
Tests/LibWeb/Text/input/performance-now.html
Normal file
25
Tests/LibWeb/Text/input/performance-now.html
Normal file
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<script src="include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
let before = performance.now();
|
||||
let event = new MouseEvent('test');
|
||||
let after = performance.now();
|
||||
|
||||
if (event.timeStamp < before || event.timeStamp > after) {
|
||||
println('Event.timeStamp should be in between performance.now() calls, but was ' + event.timeStamp);
|
||||
return;
|
||||
}
|
||||
|
||||
let timestamp = performance.now();
|
||||
let date = Date.now();
|
||||
let allowedDifference = 300;
|
||||
|
||||
if (timestamp <= date - allowedDifference || timestamp >= date + allowedDifference) {
|
||||
println('performance.now() should be close to Date.now(), but was ' + (timestamp - date));
|
||||
return;
|
||||
}
|
||||
|
||||
println('PASS');
|
||||
});
|
||||
</script>
|
|
@ -65,11 +65,20 @@ JS::GCPtr<NavigationTiming::PerformanceNavigation> Performance::navigation()
|
|||
return m_navigation;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/hr-time/#timeorigin-attribute
|
||||
double Performance::time_origin() const
|
||||
{
|
||||
// FIXME: The timeOrigin attribute MUST return the number of milliseconds in the duration returned by get time origin timestamp for the relevant global object of this.
|
||||
return static_cast<double>(m_timer.origin_time().nanoseconds()) / 1e6;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/hr-time/#now-method
|
||||
double Performance::now() const
|
||||
{
|
||||
// The now() method MUST return the number of milliseconds in the current high resolution time given this's relevant global object (a duration).
|
||||
return current_high_resolution_time(HTML::relevant_global_object(*this));
|
||||
}
|
||||
|
||||
// https://w3c.github.io/user-timing/#mark-method
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMark>> Performance::mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options)
|
||||
{
|
||||
|
@ -232,8 +241,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMeasure>> Performanc
|
|||
}
|
||||
// 4. Otherwise, let end time be the value that would be returned by the Performance object's now() method.
|
||||
else {
|
||||
// FIXME: Performance#now doesn't currently use TimeOrigin's functions, update this and Performance#now to match Performance#now's specification.
|
||||
end_time = HighResolutionTime::unsafe_shared_current_time();
|
||||
end_time = now();
|
||||
}
|
||||
|
||||
// 3. Compute start time as follows:
|
||||
|
|
|
@ -22,7 +22,7 @@ class Performance final : public DOM::EventTarget {
|
|||
public:
|
||||
virtual ~Performance() override;
|
||||
|
||||
double now() const { return static_cast<double>(m_timer.elapsed_time().to_nanoseconds()) / 1e6; }
|
||||
double now() const;
|
||||
double time_origin() const;
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMark>> mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options = {});
|
||||
|
|
|
@ -14,9 +14,32 @@ namespace Web::HighResolutionTime {
|
|||
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
|
||||
DOMHighResTimeStamp get_time_origin_timestamp(JS::Object const& global)
|
||||
{
|
||||
// FIXME: Implement this.
|
||||
(void)global;
|
||||
return 0;
|
||||
|
||||
// To get time origin timestamp, given a global object global, run the following steps, which return a duration:
|
||||
// FIXME: 1. Let timeOrigin be global's relevant settings object's time origin.
|
||||
auto time_origin = 0;
|
||||
|
||||
// Each group of environment settings objects that could possibly communicate in any way
|
||||
// has an estimated monotonic time of the Unix epoch, a moment on the monotonic clock,
|
||||
// whose value is initialized by the following steps:
|
||||
|
||||
// !. Let wall time be the wall clock's unsafe current time.
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, nullptr);
|
||||
auto wall_time = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
|
||||
|
||||
// 2. Let monotonic time be the monotonic clock's unsafe current time.
|
||||
auto monotonic_time = unsafe_shared_current_time();
|
||||
|
||||
// 3. Let epoch time be monotonic time - (wall time - Unix epoch)
|
||||
auto epoch_time = monotonic_time - (wall_time - 0);
|
||||
|
||||
// 4. Initialize the estimated monotonic time of the Unix epoch to the result of calling coarsen time with epoch time
|
||||
auto estimated_monotonic_time = coarsen_time(epoch_time);
|
||||
|
||||
// 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin.
|
||||
return estimated_monotonic_time - time_origin;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/hr-time/#dfn-coarsen-time
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue