/* 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 std::cell::Cell; use std::cmp::Ordering; use std::time::Duration; use base::cross_process_instant::CrossProcessInstant; use malloc_size_of_derive::MallocSizeOf; use profile_traits::time::{ ProfilerCategory, ProfilerChan, TimerMetadata, TimerMetadataFrameType, TimerMetadataReflowType, send_profile_data, }; use script_traits::ProgressiveWebMetricType; use servo_config::opts; use servo_url::ServoUrl; /// TODO make this configurable /// maximum task time is 50ms (in ns) pub const MAX_TASK_NS: u128 = 50000000; /// 10 second window const INTERACTIVE_WINDOW_SECONDS: Duration = Duration::from_secs(10); pub trait ToMs { fn to_ms(&self) -> T; } impl ToMs for u64 { fn to_ms(&self) -> f64 { *self as f64 / 1000000. } } fn set_metric( pwm: &ProgressiveWebMetrics, metadata: Option, metric_type: ProgressiveWebMetricType, category: ProfilerCategory, attr: &Cell>, metric_time: CrossProcessInstant, url: &ServoUrl, ) { attr.set(Some(metric_time)); // Send the metric to the time profiler. send_profile_data( category, metadata, pwm.time_profiler_chan(), metric_time, metric_time, ); // Print the metric to console if the print-pwm option was given. if opts::get().print_pwm { let navigation_start = pwm .navigation_start() .unwrap_or_else(CrossProcessInstant::epoch); println!( "{:?} {:?} {:?}", url, metric_type, (metric_time - navigation_start).as_seconds_f64() ); } } /// A data structure to track web metrics dfined in various specifications: /// /// - /// - / /// /// We can look at three different metrics here: /// - navigation start -> visually ready (dom content loaded) /// - navigation start -> thread ready (main thread available) /// - visually ready -> thread ready #[derive(MallocSizeOf)] pub struct ProgressiveWebMetrics { /// Whether or not this metric is for an `