|
1 |
| -/// Communicate one or more metrics and descriptions for the given request-response cycle. |
2 |
| -/// |
3 |
| -/// This is an implementation of the W3C [Server |
4 |
| -/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field) |
5 |
| -/// header spec. Read more on |
6 |
| -/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing) |
7 |
| -pub struct ServerTiming; |
| 1 | +use std::time::Duration; |
8 | 2 |
|
| 3 | +/// An individual [`ServerTiming`] entry. |
| 4 | +// |
| 5 | +// # Implementation notes |
| 6 | +// |
9 | 7 | // Four different cases are valid:
|
10 | 8 | //
|
11 | 9 | // 1. metric name only cache
|
12 |
| -// 2. metric + metric cache;dur=2.4 |
13 |
| -// 3. metric + description cache;desc="Cache Read" |
| 10 | +// 2. metric + value cache;dur=2.4 |
| 11 | +// 3. metric + desc cache;desc="Cache Read" |
14 | 12 | // 4. metric + value + desc cache;desc="Cache Read";dur=23.2
|
15 | 13 | //
|
16 | 14 | // Multiple different entries per line are supported; separated with a `,`.
|
| 15 | +#[derive(Debug)] |
| 16 | +pub struct TimingEntry { |
| 17 | + name: String, |
| 18 | + dur: Option<Duration>, |
| 19 | + desc: Option<String>, |
| 20 | +} |
| 21 | + |
| 22 | +impl TimingEntry { |
| 23 | + /// Create a new instance of `TimingEntry`. |
| 24 | + pub fn new(name: String, dur: Option<Duration>, desc: Option<String>) -> Self { |
| 25 | + Self { name, dur, desc } |
| 26 | + } |
| 27 | + |
| 28 | + /// The timing name. |
| 29 | + pub fn name(&self) -> &String { |
| 30 | + &self.name |
| 31 | + } |
| 32 | + |
| 33 | + /// Set the timing name. |
| 34 | + pub fn set_name(&mut self, name: String) { |
| 35 | + self.name = name; |
| 36 | + } |
| 37 | + |
| 38 | + /// The timing duration. |
| 39 | + pub fn duration(&self) -> Option<Duration> { |
| 40 | + self.dur |
| 41 | + } |
| 42 | + |
| 43 | + /// Set the timing name. |
| 44 | + pub fn set_duration(&mut self, dur: Option<Duration>) { |
| 45 | + self.dur = dur; |
| 46 | + } |
| 47 | + |
| 48 | + /// The timing description. |
| 49 | + pub fn description(&self) -> Option<&String> { |
| 50 | + self.desc.as_ref() |
| 51 | + } |
| 52 | + |
| 53 | + /// Set the timing description. |
| 54 | + pub fn set_description(&mut self, desc: Option<String>) { |
| 55 | + self.desc = desc; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Communicate one or more metrics and descriptions for the given request-response cycle. |
| 60 | +/// |
| 61 | +/// This is an implementation of the W3C [Server |
| 62 | +/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field) |
| 63 | +/// header spec. Read more on |
| 64 | +/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing). |
| 65 | +#[derive(Debug)] |
| 66 | +pub struct ServerTiming { |
| 67 | + timings: Vec<TimingEntry>, |
| 68 | +} |
17 | 69 |
|
18 | 70 | mod test {
|
19 | 71 | const CASE1: &str =
|
|
0 commit comments