mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Auto merge of #24508 - pajamapants3000:21254, r=jdm
Add start_time to resource timing. <!-- Please describe your changes on the following line: --> `start_time` property added to `ResourceFetchTiming`, which enables the setting of `start_time` in the `PerformanceEntry` member of `PerformanceResourceTiming`. Following the specification at https://w3c.github.io/resource-timing/#dfn-starttime, `start_time` is set to the value of `redirect_start` if redirection occurs and the timing allow check passes. Otherwise it has the same value as `fetch_start`. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #21254 <!-- Either: --> - [x] There are tests for these changes OR - [ ] These changes do not require tests because ___ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
commit
bf587f22d7
8 changed files with 277 additions and 15 deletions
|
@ -474,6 +474,7 @@ pub struct ResourceFetchTiming {
|
|||
pub redirect_end: u64,
|
||||
pub connect_start: u64,
|
||||
pub connect_end: u64,
|
||||
pub start_time: u64,
|
||||
}
|
||||
|
||||
pub enum RedirectStartValue {
|
||||
|
@ -487,6 +488,15 @@ pub enum RedirectEndValue {
|
|||
ResponseEnd,
|
||||
}
|
||||
|
||||
// TODO: refactor existing code to use this enum for setting time attributes
|
||||
// suggest using this with all time attributes in the future
|
||||
pub enum ResourceTimeValue {
|
||||
Zero,
|
||||
Now,
|
||||
FetchStart,
|
||||
RedirectStart,
|
||||
}
|
||||
|
||||
pub enum ResourceAttribute {
|
||||
RedirectCount(u16),
|
||||
DomainLookupStart,
|
||||
|
@ -499,6 +509,7 @@ pub enum ResourceAttribute {
|
|||
ConnectEnd(u64),
|
||||
SecureConnectionStart,
|
||||
ResponseEnd,
|
||||
StartTime(ResourceTimeValue),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
|
||||
|
@ -525,6 +536,7 @@ impl ResourceFetchTiming {
|
|||
connect_start: 0,
|
||||
connect_end: 0,
|
||||
response_end: 0,
|
||||
start_time: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -532,7 +544,9 @@ impl ResourceFetchTiming {
|
|||
// time origin (as described in Performance::now)
|
||||
pub fn set_attribute(&mut self, attribute: ResourceAttribute) {
|
||||
let should_attribute_always_be_updated = match attribute {
|
||||
ResourceAttribute::FetchStart | ResourceAttribute::ResponseEnd => true,
|
||||
ResourceAttribute::FetchStart |
|
||||
ResourceAttribute::ResponseEnd |
|
||||
ResourceAttribute::StartTime(_) => true,
|
||||
_ => false,
|
||||
};
|
||||
if !self.timing_check_passed && !should_attribute_always_be_updated {
|
||||
|
@ -562,6 +576,20 @@ impl ResourceFetchTiming {
|
|||
self.secure_connection_start = precise_time_ns()
|
||||
},
|
||||
ResourceAttribute::ResponseEnd => self.response_end = precise_time_ns(),
|
||||
ResourceAttribute::StartTime(val) => match val {
|
||||
ResourceTimeValue::RedirectStart
|
||||
if self.redirect_start == 0 || !self.timing_check_passed => {},
|
||||
_ => self.start_time = self.get_time_value(val),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_time_value(&self, time: ResourceTimeValue) -> u64 {
|
||||
match time {
|
||||
ResourceTimeValue::Zero => 0,
|
||||
ResourceTimeValue::Now => precise_time_ns(),
|
||||
ResourceTimeValue::FetchStart => self.fetch_start,
|
||||
ResourceTimeValue::RedirectStart => self.redirect_start,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
212
components/net_traits/tests/lib.rs
Normal file
212
components/net_traits/tests/lib.rs
Normal file
|
@ -0,0 +1,212 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use net_traits::{ResourceAttribute, ResourceFetchTiming, ResourceTimeValue, ResourceTimingType};
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time_to_fetch_start_if_nonzero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.fetch_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
assert!(
|
||||
resource_timing.fetch_start > 0,
|
||||
"`fetch_start` should have a positive value"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `fetch_start` succeeds
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, resource_timing.fetch_start,
|
||||
"`start_time` should equal `fetch_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time_to_fetch_start_if_zero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.start_time = 1;
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.fetch_start, 0,
|
||||
"`fetch_start` should be zero"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `fetch_start` succeeds even when `fetch_start` == zero
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, resource_timing.fetch_start,
|
||||
"`start_time` should equal `fetch_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time_to_fetch_start_if_nonzero_no_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
resource_timing.fetch_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
assert!(
|
||||
resource_timing.fetch_start > 0,
|
||||
"`fetch_start` should have a positive value"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `fetch_start` succeeds even when TAO check failed
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, resource_timing.fetch_start,
|
||||
"`start_time` should equal `fetch_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time_to_fetch_start_if_zero_no_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
resource_timing.start_time = 1;
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.fetch_start, 0,
|
||||
"`fetch_start` should be zero"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `fetch_start` succeeds even when `fetch_start`==0 and no TAO
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::FetchStart));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, resource_timing.fetch_start,
|
||||
"`start_time` should equal `fetch_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time_to_redirect_start_if_nonzero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.redirect_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
assert!(
|
||||
resource_timing.redirect_start > 0,
|
||||
"`redirect_start` should have a positive value"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `redirect_start` succeeds for nonzero `redirect_start`, TAO pass
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(
|
||||
ResourceTimeValue::RedirectStart,
|
||||
));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, resource_timing.redirect_start,
|
||||
"`start_time` should equal `redirect_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not_set_start_time_to_redirect_start_if_zero_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.start_time = 1;
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.redirect_start, 0,
|
||||
"`redirect_start` should be zero"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `redirect_start` fails if `redirect_start` == 0
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(
|
||||
ResourceTimeValue::RedirectStart,
|
||||
));
|
||||
assert_ne!(
|
||||
resource_timing.start_time, resource_timing.redirect_start,
|
||||
"`start_time` should *not* equal `redirect_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not_set_start_time_to_redirect_start_if_nonzero_no_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
// Note: properly-behaved redirect_start should never be nonzero once TAO check has failed
|
||||
resource_timing.redirect_start = 1;
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
assert!(
|
||||
resource_timing.redirect_start > 0,
|
||||
"`redirect_start` should have a positive value"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `redirect_start` fails if TAO check fails
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(
|
||||
ResourceTimeValue::RedirectStart,
|
||||
));
|
||||
assert_ne!(
|
||||
resource_timing.start_time, resource_timing.redirect_start,
|
||||
"`start_time` should *not* equal `redirect_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not_set_start_time_to_redirect_start_if_zero_no_tao() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
resource_timing.mark_timing_check_failed();
|
||||
resource_timing.start_time = 1;
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
assert_eq!(
|
||||
resource_timing.redirect_start, 0,
|
||||
"`redirect_start` should be zero"
|
||||
);
|
||||
|
||||
// verify that setting `start_time` to `redirect_start` fails if `redirect_start`==0 and no TAO
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(
|
||||
ResourceTimeValue::RedirectStart,
|
||||
));
|
||||
assert_ne!(
|
||||
resource_timing.start_time, resource_timing.redirect_start,
|
||||
"`start_time` should *not* equal `redirect_start`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_start_time() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
|
||||
// verify setting `start_time` to current time succeeds
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::Now));
|
||||
assert!(resource_timing.start_time > 0, "failed to set `start_time`");
|
||||
}
|
||||
#[test]
|
||||
fn test_reset_start_time() {
|
||||
let mut resource_timing: ResourceFetchTiming =
|
||||
ResourceFetchTiming::new(ResourceTimingType::Resource);
|
||||
assert_eq!(resource_timing.start_time, 0, "`start_time` should be zero");
|
||||
|
||||
resource_timing.start_time = 1;
|
||||
assert!(
|
||||
resource_timing.start_time > 0,
|
||||
"`start_time` should have a positive value"
|
||||
);
|
||||
|
||||
// verify resetting `start_time` (to zero) succeeds
|
||||
resource_timing.set_attribute(ResourceAttribute::StartTime(ResourceTimeValue::Zero));
|
||||
assert_eq!(
|
||||
resource_timing.start_time, 0,
|
||||
"failed to reset `start_time`"
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue