Skip to main content

browser_protocol/performance/
mod.rs

1use serde::{Serialize, Deserialize};
2use serde_json::Value as JsonValue;
3use std::borrow::Cow;
4
5/// Run-time execution metric.
6
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8#[serde(rename_all = "camelCase")]
9pub struct Metric<'a> {
10    /// Metric name.
11    name: Cow<'a, str>,
12    /// Metric value.
13    value: f64,
14}
15
16impl<'a> Metric<'a> {
17    /// Creates a builder for this type with the required parameters:
18    /// * `name`: Metric name.
19    /// * `value`: Metric value.
20    pub fn builder(name: impl Into<Cow<'a, str>>, value: f64) -> MetricBuilder<'a> {
21        MetricBuilder {
22            name: name.into(),
23            value: value,
24        }
25    }
26    /// Metric name.
27    pub fn name(&self) -> &str { self.name.as_ref() }
28    /// Metric value.
29    pub fn value(&self) -> f64 { self.value }
30}
31
32
33pub struct MetricBuilder<'a> {
34    name: Cow<'a, str>,
35    value: f64,
36}
37
38impl<'a> MetricBuilder<'a> {
39    pub fn build(self) -> Metric<'a> {
40        Metric {
41            name: self.name,
42            value: self.value,
43        }
44    }
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize, Default)]
48pub struct DisableParams {}
49
50impl DisableParams { pub const METHOD: &'static str = "Performance.disable"; }
51
52impl<'a> crate::CdpCommand<'a> for DisableParams {
53    const METHOD: &'static str = "Performance.disable";
54    type Response = crate::EmptyReturns;
55}
56
57/// Enable collecting and reporting metrics.
58
59#[derive(Debug, Clone, Serialize, Deserialize, Default)]
60#[serde(rename_all = "camelCase")]
61pub struct EnableParams<'a> {
62    /// Time domain to use for collecting and reporting duration metrics.
63    #[serde(skip_serializing_if = "Option::is_none", rename = "timeDomain")]
64    time_domain: Option<Cow<'a, str>>,
65}
66
67impl<'a> EnableParams<'a> {
68    /// Creates a builder for this type.
69    pub fn builder() -> EnableParamsBuilder<'a> {
70        EnableParamsBuilder {
71            time_domain: None,
72        }
73    }
74    /// Time domain to use for collecting and reporting duration metrics.
75    pub fn time_domain(&self) -> Option<&str> { self.time_domain.as_deref() }
76}
77
78#[derive(Default)]
79pub struct EnableParamsBuilder<'a> {
80    time_domain: Option<Cow<'a, str>>,
81}
82
83impl<'a> EnableParamsBuilder<'a> {
84    /// Time domain to use for collecting and reporting duration metrics.
85    pub fn time_domain(mut self, time_domain: impl Into<Cow<'a, str>>) -> Self { self.time_domain = Some(time_domain.into()); self }
86    pub fn build(self) -> EnableParams<'a> {
87        EnableParams {
88            time_domain: self.time_domain,
89        }
90    }
91}
92
93impl<'a> EnableParams<'a> { pub const METHOD: &'static str = "Performance.enable"; }
94
95impl<'a> crate::CdpCommand<'a> for EnableParams<'a> {
96    const METHOD: &'static str = "Performance.enable";
97    type Response = crate::EmptyReturns;
98}
99
100/// Sets time domain to use for collecting and reporting duration metrics.
101/// Note that this must be called before enabling metrics collection. Calling
102/// this method while metrics collection is enabled returns an error.
103
104#[derive(Debug, Clone, Serialize, Deserialize, Default)]
105#[serde(rename_all = "camelCase")]
106pub struct SetTimeDomainParams<'a> {
107    /// Time domain
108    #[serde(rename = "timeDomain")]
109    time_domain: Cow<'a, str>,
110}
111
112impl<'a> SetTimeDomainParams<'a> {
113    /// Creates a builder for this type with the required parameters:
114    /// * `time_domain`: Time domain
115    pub fn builder(time_domain: impl Into<Cow<'a, str>>) -> SetTimeDomainParamsBuilder<'a> {
116        SetTimeDomainParamsBuilder {
117            time_domain: time_domain.into(),
118        }
119    }
120    /// Time domain
121    pub fn time_domain(&self) -> &str { self.time_domain.as_ref() }
122}
123
124
125pub struct SetTimeDomainParamsBuilder<'a> {
126    time_domain: Cow<'a, str>,
127}
128
129impl<'a> SetTimeDomainParamsBuilder<'a> {
130    pub fn build(self) -> SetTimeDomainParams<'a> {
131        SetTimeDomainParams {
132            time_domain: self.time_domain,
133        }
134    }
135}
136
137impl<'a> SetTimeDomainParams<'a> { pub const METHOD: &'static str = "Performance.setTimeDomain"; }
138
139impl<'a> crate::CdpCommand<'a> for SetTimeDomainParams<'a> {
140    const METHOD: &'static str = "Performance.setTimeDomain";
141    type Response = crate::EmptyReturns;
142}
143
144/// Retrieve current values of run-time metrics.
145
146#[derive(Debug, Clone, Serialize, Deserialize, Default)]
147#[serde(rename_all = "camelCase")]
148pub struct GetMetricsReturns<'a> {
149    /// Current values for run-time metrics.
150    metrics: Vec<Metric<'a>>,
151}
152
153impl<'a> GetMetricsReturns<'a> {
154    /// Creates a builder for this type with the required parameters:
155    /// * `metrics`: Current values for run-time metrics.
156    pub fn builder(metrics: Vec<Metric<'a>>) -> GetMetricsReturnsBuilder<'a> {
157        GetMetricsReturnsBuilder {
158            metrics: metrics,
159        }
160    }
161    /// Current values for run-time metrics.
162    pub fn metrics(&self) -> &[Metric<'a>] { &self.metrics }
163}
164
165
166pub struct GetMetricsReturnsBuilder<'a> {
167    metrics: Vec<Metric<'a>>,
168}
169
170impl<'a> GetMetricsReturnsBuilder<'a> {
171    pub fn build(self) -> GetMetricsReturns<'a> {
172        GetMetricsReturns {
173            metrics: self.metrics,
174        }
175    }
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize, Default)]
179pub struct GetMetricsParams {}
180
181impl GetMetricsParams { pub const METHOD: &'static str = "Performance.getMetrics"; }
182
183impl<'a> crate::CdpCommand<'a> for GetMetricsParams {
184    const METHOD: &'static str = "Performance.getMetrics";
185    type Response = GetMetricsReturns<'a>;
186}