From 6fa1e31a99cd9cd758c36e30db07e00411d3ea7a Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Sun, 16 Jun 2024 06:39:02 +0000 Subject: [PATCH] Bug 1894242 [wpt PR 45989] - compute pressure: Fix timestamp to be relative to the time origin, a=testonly Automatic update from web-platform-tests compute pressure: Fix timestamp to be relative to the time origin The Compute Pressure API specification stipulates that the timestamp must be relative to time origin of the global associated object with the PressureObserver instance that generates the notification.[1] The implementation was reporting the current time, relative to epoch in Unix-like system. This patch fixes the issue, by using base::TimeTicks instead of base::Time, which is always incrementing, and also by subtracting the time origin from the current time to provide a timespace relative to the time origin of the associated object. [1]: https://w3c.github.io/compute-pressure/#the-pressurerecord-interface Bug: 336140984 Change-Id: I5e3cfb5ad966f62ce13ade3d81e55b5b4273025a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5472087 Reviewed-by: Reilly Grant Reviewed-by: Raphael Kubo Da Costa Reviewed-by: Michael Cui Reviewed-by: Daniel Cheng Commit-Queue: Arnaud Mandy Cr-Commit-Position: refs/heads/main{#1296013} -- wpt-commits: c5ced76d2189c00405d2c153f406fdcd8f91781d wpt-pr: 45989 UltraBlame original commit: 900af819e11a361a5fd3f1b1c6f51db6a318538d --- .../compute_pressure_timestamp.https.any.js | 420 +++++++++++++++++- .../chromium/mock-pressure-service.js | 324 ++++---------- 2 files changed, 501 insertions(+), 243 deletions(-) diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js index a0d9f4616b7d3..0c7ae8e079bdd 100644 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js @@ -52,6 +52,129 @@ mockPressureService > { const +[ +change +timeOrigin +] += +await +new +Promise +( +resolve += +> +{ +const +observer += +new +PressureObserver +( +change += +> +{ +resolve +( +[ +change +performance +. +timeOrigin +] +) +; +} +) +; +t +. +add_cleanup +( +( +) += +> +observer +. +disconnect +( +) +) +; +observer +. +observe +( +' +cpu +' +) +; +mockPressureService +. +setPressureUpdate +( +' +cpu +' +' +critical +' +) +; +mockPressureService +. +startPlatformCollector +( +/ +* +sampleInterval += +* +/ +200 +) +; +} +) +; +assert_greater_than +( +change +[ +0 +] +. +time +timeOrigin +) +; +} +' +Timestamp +from +update +should +be +greater +than +timeOrigin +' +) +; +pressure_test +( +async +( +t +mockPressureService +) += +> +{ +const readings = [ @@ -77,18 +200,6 @@ sampleInterval const pressureChanges = -await -new -Promise -( -async -resolve -= -> -{ -const -observerChanges -= [ ] ; @@ -102,7 +213,7 @@ changes = > { -observerChanges +pressureChanges . push ( @@ -187,7 +298,7 @@ requested . while ( -observerChanges +pressureChanges . length < @@ -248,12 +359,287 @@ disconnect ( ) ; -resolve +assert_equals +( +pressureChanges +. +length +4 +) +; +assert_greater_than +( +pressureChanges +[ +1 +] +[ +0 +] +. +time +pressureChanges +[ +0 +] +[ +0 +] +. +time +) +; +assert_greater_than ( -observerChanges +pressureChanges +[ +2 +] +[ +0 +] +. +time +pressureChanges +[ +1 +] +[ +0 +] +. +time +) +; +assert_greater_than +( +pressureChanges +[ +3 +] +[ +0 +] +. +time +pressureChanges +[ +2 +] +[ +0 +] +. +time ) ; } +' +Timestamp +difference +between +two +changes +should +be +continuously +increasing +' +) +; +pressure_test +( +async +( +t +mockPressureService +) += +> +{ +const +readings += +[ +' +nominal +' +' +fair +' +' +serious +' +' +critical +' +] +; +const +sampleInterval += +250 +; +const +pressureChanges += +[ +] +; +const +observer += +new +PressureObserver +( +change += +> +{ +pressureChanges +. +push +( +change +) +; +} +) +; +observer +. +observe +( +' +cpu +' +{ +sampleInterval +} +) +; +mockPressureService +. +startPlatformCollector +( +sampleInterval +/ +2 +) +; +let +i += +0 +; +/ +/ +mockPressureService +. +updatesDelivered +( +) +does +not +necessarily +match +/ +/ +pressureChanges +. +length +as +system +load +and +browser +optimizations +can +/ +/ +cause +the +actual +timer +used +by +mockPressureService +to +deliver +readings +/ +/ +to +be +a +bit +slower +or +faster +than +requested +. +while +( +pressureChanges +. +length +< +4 +) +{ +mockPressureService +. +setPressureUpdate +( +' +cpu +' +readings +[ +i ++ ++ +% +readings +. +length +] +) +; +await +t +. +step_wait +( +( +) += +> +mockPressureService +. +updatesDelivered +( +) +> += +i +At +least +{ +i +} +readings +have +been +delivered +) +; +} +observer +. +disconnect +( ) ; assert_equals @@ -592,6 +978,7 @@ requirement . assert_less_than ( +( pressureChanges [ 1 @@ -611,6 +998,7 @@ pressureChanges ] . time +) sampleInterval ) ; diff --git a/testing/web-platform/tests/resources/chromium/mock-pressure-service.js b/testing/web-platform/tests/resources/chromium/mock-pressure-service.js index 8f2eebd3852b9..1454d2975dc73 100644 --- a/testing/web-platform/tests/resources/chromium/mock-pressure-service.js +++ b/testing/web-platform/tests/resources/chromium/mock-pressure-service.js @@ -472,225 +472,6 @@ stopPlatformCollector ( ) ; -/ -/ -The -following -code -for -calculating -the -timestamp -was -taken -from -/ -/ -https -: -/ -/ -source -. -chromium -. -org -/ -chromium -/ -chromium -/ -src -/ -+ -/ -main -: -third_party -/ -/ -/ -blink -/ -web_tests -/ -http -/ -tests -/ -resources -/ -/ -/ -geolocation -- -mock -. -js -; -l -= -131 -; -drc -= -37a9b6c03b9bda9fcd62fc0e5e8016c278abd31f -/ -/ -The -new -Date -( -) -. -getTime -( -) -returns -the -number -of -milliseconds -since -the -/ -/ -UNIX -epoch -( -1970 -- -01 -- -01 -00 -: -: -00 -: -00 -UTC -) -while -| -internalValue -| -of -the -/ -/ -device -. -mojom -. -PressureUpdate -represents -the -value -of -microseconds -since -/ -/ -the -Windows -FILETIME -epoch -( -1601 -- -01 -- -01 -00 -: -00 -: -00 -UTC -) -. -So -add -the -delta -/ -/ -when -sets -the -| -internalValue -| -. -See -more -info -in -/ -/ -base -/ -time -/ -time -. -h -. -const -windowsEpoch -= -Date -. -UTC -( -1601 -0 -1 -0 -0 -0 -0 -) -; -const -unixEpoch -= -Date -. -UTC -( -1970 -0 -1 -0 -0 -0 -0 -) -; -/ -/ -| -epochDeltaInMs -| -equals -to -base -: -: -Time -: -: -kTimeTToMicrosecondsOffset -. -const -epochDeltaInMs -= -unixEpoch -- -windowsEpoch -; this . pressureServiceReadingTimerId_ @@ -727,6 +508,92 @@ length ) return ; +/ +/ +Because +we +cannot +retrieve +directly +the +timeOrigin +internal +in +/ +/ +TimeTicks +from +Chromium +we +need +to +create +a +timestamp +that +/ +/ +would +help +to +test +basic +functionality +. +/ +/ +by +multiplying +performance +. +timeOrigin +by +10 +we +make +sure +that +the +/ +/ +origin +is +higher +than +the +internal +time +origin +in +TimeTicks +. +/ +/ +performance +. +now +( +) +allows +us +to +have +an +increase +matching +the +TimeTicks +/ +/ +that +would +be +read +from +the +platform +collector +. this . pressureUpdate_ @@ -736,23 +603,26 @@ timestamp { internalValue : -BigInt -( +Math +. +round ( -new -Date ( +performance +. +timeOrigin +* +10 ) ++ +performance . -getTime +now ( ) -+ -epochDeltaInMs ) * 1000 -) } ; for