Skip to content

Commit

Permalink
ComputePressure: Turn sampleRate into sampleInterval (web-platform-te…
Browse files Browse the repository at this point in the history
…sts#45213)

Following specifications change[1], sampleRate in hertz is changed to
sampleInterval in millisecond.

w3c/compute-pressure#254

Bug: 330376756
Change-Id: Ibe909d537e462cd251d28c7d4d4702341dc33c0c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5381598
Reviewed-by: Fr <[email protected]>
Reviewed-by: Raphael Kubo Da Costa <[email protected]>
Commit-Queue: Arnaud Mandy <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1275582}

Co-authored-by: Arnaud Mandy <[email protected]>
  • Loading branch information
2 people authored and BruceDai committed Mar 25, 2024
1 parent cae3bfa commit 6f782e6
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 53 deletions.
8 changes: 4 additions & 4 deletions compute-pressure/compute_pressure_basic.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pressure_test(async (t, mockPressureService) => {
t.add_cleanup(() => observer.disconnect());
observer.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});
assert_true(changes.length === 1);
assert_equals(changes[0].state, 'critical');
Expand All @@ -37,7 +37,7 @@ pressure_test((t, mockPressureService) => {
const promise = observer.observe('cpu');
observer.unobserve('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);

return promise_rejects_dom(t, 'AbortError', promise);
}, 'Removing observer before observe() resolves works');
Expand All @@ -57,7 +57,7 @@ pressure_test(async (t, mockPressureService) => {
await Promise.all(observePromises);

mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);

return Promise.all(callbackPromises);
}, 'Calling observe() multiple times works');
Expand All @@ -72,7 +72,7 @@ pressure_test(async (t, mockPressureService) => {
t.add_cleanup(() => observer1.disconnect());
observer1.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});
assert_true(observer1_changes.length === 1);
assert_equals(observer1_changes[0][0].source, 'cpu');
Expand Down
4 changes: 2 additions & 2 deletions compute-pressure/compute_pressure_detached_iframe.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
t.add_cleanup(() => observer.disconnect());
observer.observe('cpu').catch(reject);
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});
assert_equals(changes[0].state, 'critical');
}, 'Detaching frame while PressureObserver.observe() settles');
Expand All @@ -84,7 +84,7 @@

await observer.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);

iframe.remove();

Expand Down
2 changes: 1 addition & 1 deletion compute-pressure/compute_pressure_disconnect.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pressure_test(async (t, mockPressureService) => {
t.add_cleanup(() => observer2.disconnect());
observer2.observe('cpu').catch(reject);
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});

assert_equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pressure_test(async (t, mockPressureService) => {
t.add_cleanup(() => observer2.disconnect());
observer2.observe('cpu').catch(reject);
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});

assert_equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pressure_test(async (t, mockPressureService) => {
t.add_cleanup(() => observer2.disconnect());
observer2.observe('cpu').catch(reject);
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});

assert_equals(
Expand Down Expand Up @@ -55,7 +55,7 @@ pressure_test(async (t, mockPressureService) => {
observer1.disconnect();
await promise_rejects_dom(t, 'AbortError', promise);
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});

assert_equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pressure_test(async (t, mockPressureService) => {
observer_changes.push(changes);
if (++n === 2)
resolve(observer_changes);
}, {sampleRate: 5.0});
}, {sampleInterval: 200});
observer.observe('cpu');
const updatesDelivered = mockPressureService.updatesDelivered();
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval*/ 200);
// Deliver 2 updates.
await t.step_wait(
() => mockPressureService.updatesDelivered() >= (updatesDelivered + 2),
Expand Down
2 changes: 1 addition & 1 deletion compute-pressure/compute_pressure_multiple.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pressure_test(async (t, mockPressureService) => {
});

mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);

const [changes1, changes2, changes3] =
await Promise.all([changes1_promise, changes2_promise, changes3_promise]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pressure_test(async (t, mockPressureService) => {
observer.observe('cpu').catch(reject);
observer.observe('cpu').catch(reject);
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});

assert_equals(update[0].state, 'critical');
Expand Down
23 changes: 12 additions & 11 deletions compute-pressure/compute_pressure_options.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
'use strict';

test(t => {
assert_throws_js(RangeError, () => {
new PressureObserver(() => {}, {sampleRate: 0});
});
}, 'PressureObserver constructor requires a non-zero sampleRate');
const observer = new PressureObserver(() => {}, {sampleInterval: 0});
assert_equals(typeof observer, 'object');
}, 'PressureObserver constructor doesnt throw error for sampleInterval value 0');


test(t => {
assert_throws_js(RangeError, () => {
new PressureObserver(() => {}, {sampleRate: -2});
assert_throws_js(TypeError, () => {
new PressureObserver(() => {}, {sampleInterval: -2});
});
}, 'PressureObserver constructor requires a positive sampleRate');
}, 'PressureObserver constructor requires a positive sampleInterval');

test(t => {
const observer = new PressureObserver(() => {}, {sampleRate: 0.5});
assert_equals(typeof observer, 'object');
}, 'PressureObserver constructor doesnt throw error on positive sampleRate');
assert_throws_js(TypeError, () => {
new PressureObserver(() => {}, {sampleInterval: 2 ** 32});
});
}, 'PressureObserver constructor requires a sampleInterval in unsigned long range');

test(t => {
const observer = new PressureObserver(() => {}, {});
assert_equals(typeof observer, 'object');
}, 'PressureObserver constructor succeeds on empty sampleRate');
}, 'PressureObserver constructor succeeds on empty sampleInterval');
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

pressure_test(async (t, mockPressureService) => {
const sampleRateInHz = 10;
const sampleIntervalInMs = 100;
const readings = ['nominal', 'fair', 'serious', 'critical'];
// Normative values for rate obfuscation parameters.
// https://w3c.github.io/compute-pressure/#rate-obfuscation-normative-parameters.
Expand All @@ -17,10 +17,10 @@ pressure_test(async (t, mockPressureService) => {
const observerChanges = [];
const observer = new PressureObserver(changes => {
observerChanges.push(changes);
}, {sampleRate: sampleRateInHz});
}, {sampleInterval: sampleIntervalInMs});

observer.observe('cpu');
mockPressureService.startPlatformCollector(sampleRateInHz);
mockPressureService.startPlatformCollector(sampleIntervalInMs);
let i = 0;
// mockPressureService.updatesDelivered() does not necessarily match
// pressureChanges.length, as system load and browser optimizations can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

pressure_test(async (t, mockPressureService) => {
const sampleRateInHz = 25;
const sampleIntervalInMs = 40;
const readings = ['nominal', 'fair', 'serious', 'critical'];
// Normative values for rate obfuscation parameters.
// https://w3c.github.io/compute-pressure/#rate-obfuscation-normative-parameters.
Expand All @@ -31,10 +31,10 @@ pressure_test(async (t, mockPressureService) => {
}
}
observerChanges.push(changes);
}, {sampleRate: sampleRateInHz});
}, {sampleInterval: sampleIntervalInMs});

observer.observe('cpu');
mockPressureService.startPlatformCollector(sampleRateInHz);
mockPressureService.startPlatformCollector(sampleIntervalInMs);
let i = 0;
// mockPressureService.updatesDelivered() does not necessarily match
// pressureChanges.length, as system load and browser optimizations can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pressure_test(async (t, mockPressureService) => {

observer.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});
assert_equals(changes[0].state, 'critical');

Expand Down
26 changes: 11 additions & 15 deletions compute-pressure/compute_pressure_timestamp.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
pressure_test(async (t, mockPressureService) => {
const readings = ['nominal', 'fair', 'serious', 'critical'];

const sampleRate = 4.0;
const sampleInterval = 250;
const pressureChanges = await new Promise(async resolve => {
const observerChanges = [];
const observer = new PressureObserver(changes => {
observerChanges.push(changes);
}, {sampleRate});
}, {sampleInterval});
observer.observe('cpu');

mockPressureService.startPlatformCollector(sampleRate * 2);
mockPressureService.startPlatformCollector(sampleInterval / 2);
let i = 0;
// mockPressureService.updatesDelivered() does not necessarily match
// pressureChanges.length, as system load and browser optimizations can
Expand All @@ -34,27 +34,24 @@ pressure_test(async (t, mockPressureService) => {

assert_equals(pressureChanges.length, 4);
assert_greater_than_equal(
pressureChanges[1][0].time - pressureChanges[0][0].time,
(1 / sampleRate * 1000));
pressureChanges[1][0].time - pressureChanges[0][0].time, sampleInterval);
assert_greater_than_equal(
pressureChanges[2][0].time - pressureChanges[1][0].time,
(1 / sampleRate * 1000));
pressureChanges[2][0].time - pressureChanges[1][0].time, sampleInterval);
assert_greater_than_equal(
pressureChanges[3][0].time - pressureChanges[2][0].time,
(1 / sampleRate * 1000));
pressureChanges[3][0].time - pressureChanges[2][0].time, sampleInterval);
}, 'Faster collector: Timestamp difference between two changes should be higher or equal to the observer sample rate');

pressure_test(async (t, mockPressureService) => {
const pressureChanges = [];
const sampleRate = 1.0;
const sampleInterval = 1000;
const observer = new PressureObserver(changes => {
pressureChanges.push(changes);
}, {sampleRate});
}, {sampleInterval});

await new Promise(async resolve => {
observer.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(sampleRate);
mockPressureService.startPlatformCollector(sampleInterval);
await t.step_wait(() => pressureChanges.length == 1);
observer.disconnect();
resolve();
Expand All @@ -63,7 +60,7 @@ pressure_test(async (t, mockPressureService) => {
await new Promise(async resolve => {
observer.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'serious');
mockPressureService.startPlatformCollector(sampleRate * 4);
mockPressureService.startPlatformCollector(sampleInterval / 4);
await t.step_wait(() => pressureChanges.length == 2);
observer.disconnect();
resolve();
Expand All @@ -74,6 +71,5 @@ pressure_test(async (t, mockPressureService) => {
// should be deleted. So the second PressureRecord is not discarded even
// though the time interval does not meet the requirement.
assert_less_than(
pressureChanges[1][0].time - pressureChanges[0][0].time,
(1 / sampleRate * 1000));
pressureChanges[1][0].time - pressureChanges[0][0].time, sampleInterval);
}, 'disconnect() should update [[LastRecordMap]]');
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pressure_test(async (t, mockPressureService) => {
const observer = new PressureObserver(resolve);
observer.observe('cpu');
mockPressureService.setPressureUpdate('cpu', 'critical');
mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0);
mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200);
});
assert_true(changes.length === 1);
const json = changes[0].toJSON();
Expand Down
2 changes: 1 addition & 1 deletion compute-pressure/idlharness.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ idl_test(['compute-pressure'], ['dom', 'html'], async idl_array => {
PressureObserver: ['observer'],
});

self.observer = new PressureObserver(() => {}, {sampleRate: 1.0});
self.observer = new PressureObserver(() => {}, {sampleInterval: 1000});
});
7 changes: 3 additions & 4 deletions resources/chromium/mock-pressure-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class MockPressureService {
return {status: this.pressureStatus_};
}

startPlatformCollector(sampleRate) {
if (sampleRate === 0)
startPlatformCollector(sampleInterval) {
if (sampleInterval === 0)
return;

if (this.pressureServiceReadingTimerId_ != null)
Expand All @@ -80,7 +80,6 @@ class MockPressureService {
// |epochDeltaInMs| equals to base::Time::kTimeTToMicrosecondsOffset.
const epochDeltaInMs = unixEpoch - windowsEpoch;

const timeout = (1 / sampleRate) * 1000;
this.pressureServiceReadingTimerId_ = self.setInterval(() => {
if (this.pressureUpdate_ === null || this.observers_.length === 0)
return;
Expand All @@ -90,7 +89,7 @@ class MockPressureService {
for (let observer of this.observers_)
observer.onPressureUpdated(this.pressureUpdate_);
this.updatesDelivered_++;
}, timeout);
}, sampleInterval);
}

stopPlatformCollector() {
Expand Down

0 comments on commit 6f782e6

Please sign in to comment.