Skip to main content

day_part_http/
statistics.rs

1// Copyright © The Daybrite Project
2// SPDX-License-Identifier: MPL-2.0
3
4//! Payload accounting at the transport boundary. No headers, TLS or TCP overhead is counted.
5use crate::{
6    Capabilities, Message,
7    session::lock,
8    transport::{
9        Answer, Completion, Event, Events, Prepared, PreparedBody, QuestionId, Socket, Transfer,
10        Transport, WsEvent, WsEvents,
11    },
12};
13use std::{
14    collections::VecDeque,
15    sync::{Arc, Mutex, OnceLock},
16    time::Duration,
17};
18
19pub(crate) fn now() -> Duration {
20    #[cfg(not(target_arch = "wasm32"))]
21    {
22        static START: OnceLock<std::time::Instant> = OnceLock::new();
23        START.get_or_init(std::time::Instant::now).elapsed()
24    }
25    #[cfg(target_arch = "wasm32")]
26    {
27        Duration::from_secs_f64(crate::bridge::simulation_now() / 1000.0)
28    }
29}
30/// Application payload totals. Rates divide bytes by elapsed scope time, including idle time.
31#[derive(Clone, Debug, Default)]
32pub struct Statistics {
33    pub started: u64,
34    pub active: u64,
35    pub completed: u64,
36    pub failed: u64,
37    pub uploaded_bytes: u64,
38    pub downloaded_bytes: u64,
39    pub simulated_uploaded_bytes: u64,
40    pub simulated_downloaded_bytes: u64,
41    pub elapsed: Duration,
42}
43impl Statistics {
44    #[must_use]
45    pub fn upload_bytes_per_second(&self) -> f64 {
46        self.uploaded_bytes as f64 / self.elapsed.as_secs_f64().max(0.001)
47    }
48    #[must_use]
49    pub fn download_bytes_per_second(&self) -> f64 {
50        self.downloaded_bytes as f64 / self.elapsed.as_secs_f64().max(0.001)
51    }
52    #[must_use]
53    pub fn total_bytes(&self) -> u64 {
54        self.uploaded_bytes + self.downloaded_bytes
55    }
56    /// All sessions, including explicit custom transports. Only day-part-http traffic.
57    #[must_use]
58    pub fn global() -> Self {
59        global().snapshot()
60    }
61}
62#[derive(Clone, Debug)]
63pub struct TransferStatistics {
64    pub id: u64,
65    pub method: String,
66    pub url: String,
67    pub simulated: bool,
68    pub statistics: Statistics,
69    pub error: Option<String>,
70}
71struct State {
72    start: Duration,
73    totals: Statistics,
74    history: VecDeque<Arc<Mutex<Record>>>,
75}
76struct Record {
77    start: Duration,
78    value: TransferStatistics,
79    ended: bool,
80}
81pub(crate) struct Store(Mutex<State>);
82impl Default for Store {
83    fn default() -> Self {
84        Self(Mutex::new(State {
85            start: now(),
86            totals: Statistics::default(),
87            history: VecDeque::new(),
88        }))
89    }
90}
91fn global() -> &'static Arc<Store> {
92    static STORE: OnceLock<Arc<Store>> = OnceLock::new();
93    STORE.get_or_init(|| Arc::new(Store::default()))
94}
95impl Store {
96    pub fn snapshot(&self) -> Statistics {
97        let s = lock(&self.0);
98        let mut t = s.totals.clone();
99        t.elapsed = now().saturating_sub(s.start);
100        t
101    }
102    pub fn transfers(&self) -> Vec<TransferStatistics> {
103        let records = lock(&self.0).history.iter().cloned().collect::<Vec<_>>();
104        records
105            .into_iter()
106            .map(|r| {
107                let r = lock(&r);
108                let mut v = r.value.clone();
109                if !r.ended {
110                    v.statistics.elapsed = now().saturating_sub(r.start);
111                }
112                v
113            })
114            .collect()
115    }
116}
117struct Meter {
118    record: Arc<Mutex<Record>>,
119    stores: [Arc<Store>; 2],
120}
121impl Meter {
122    fn new(store: Arc<Store>, req: &Prepared, simulated: bool) -> Arc<Self> {
123        static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
124        let record = Arc::new(Mutex::new(Record {
125            start: now(),
126            ended: false,
127            value: TransferStatistics {
128                id: NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
129                method: req.method.clone(),
130                url: req.url.clone(),
131                simulated,
132                statistics: Statistics {
133                    started: 1,
134                    active: 1,
135                    ..Default::default()
136                },
137                error: None,
138            },
139        }));
140        let stores = [store, global().clone()];
141        for s in &stores {
142            let mut st = lock(&s.0);
143            st.totals.started += 1;
144            st.totals.active += 1;
145            st.history.push_back(record.clone());
146            if st.history.len() > 256 {
147                st.history.pop_front();
148            }
149        }
150        Arc::new(Self { record, stores })
151    }
152    fn bytes(&self, upload: u64, download: u64) {
153        self.add_bytes(&mut lock(&self.record), upload, download);
154    }
155    fn add_bytes(&self, r: &mut Record, upload: u64, download: u64) {
156        fn add(s: &mut Statistics, u: u64, d: u64, sim: bool) {
157            s.uploaded_bytes += u;
158            s.downloaded_bytes += d;
159            if sim {
160                s.simulated_uploaded_bytes += u;
161                s.simulated_downloaded_bytes += d;
162            }
163        }
164        if r.ended {
165            return;
166        }
167        let sim = r.value.simulated;
168        add(&mut r.value.statistics, upload, download, sim);
169        for store in &self.stores {
170            add(&mut lock(&store.0).totals, upload, download, sim);
171        }
172    }
173    fn uploaded(&self, total: u64) {
174        let mut r = lock(&self.record);
175        let delta = total.saturating_sub(r.value.statistics.uploaded_bytes);
176        self.add_bytes(&mut r, delta, 0);
177    }
178    fn end(&self, error: Option<String>) {
179        fn finish(s: &mut Statistics, failed: bool) {
180            s.active = s.active.saturating_sub(1);
181            if failed {
182                s.failed += 1;
183            } else {
184                s.completed += 1;
185            }
186        }
187        let mut r = lock(&self.record);
188        if r.ended {
189            return;
190        }
191        r.ended = true;
192        r.value.statistics.elapsed = now().saturating_sub(r.start);
193        let failed = error.is_some();
194        r.value.error = error;
195        finish(&mut r.value.statistics, failed);
196        for store in &self.stores {
197            finish(&mut lock(&store.0).totals, failed);
198        }
199    }
200}
201struct Observed {
202    inner: Arc<dyn Transport>,
203    store: Arc<Store>,
204    simulated: bool,
205}
206pub(crate) fn observe(
207    inner: Arc<dyn Transport>,
208    store: Arc<Store>,
209    simulated: bool,
210) -> Arc<dyn Transport> {
211    Arc::new(Observed {
212        inner,
213        store,
214        simulated,
215    })
216}
217impl Transport for Observed {
218    fn capabilities(&self) -> Capabilities {
219        self.inner.capabilities()
220    }
221    fn start(&self, req: Prepared, events: Events) -> Arc<dyn Transfer> {
222        let meter = Meter::new(self.store.clone(), &req, self.simulated);
223        let m = meter.clone();
224        // Browser fetch does not report upload progress. A received response establishes that
225        // its known request payload was submitted; partial failed uploads remain unknown.
226        let fallback_upload = if self.inner.capabilities().upload_progress {
227            0
228        } else {
229            match &req.body {
230                PreparedBody::Empty => 0,
231                PreparedBody::Bytes(bytes) => bytes.len() as u64,
232                PreparedBody::File { len, .. } => *len,
233                PreparedBody::Stream { len, .. } => len.unwrap_or(0),
234            }
235        };
236        let inner = self.inner.start(
237            req,
238            Arc::new(move |e| {
239                match &e {
240                    Event::Head(_) => m.uploaded(fallback_upload),
241                    Event::Chunk(b) => m.bytes(0, b.len() as u64),
242                    Event::Sent { sent, .. } => m.uploaded(*sent),
243                    Event::End => m.end(None),
244                    Event::Failed(e) => m.end(Some(e.to_string())),
245                    _ => {}
246                }
247                events(e);
248            }),
249        );
250        Arc::new(ObservedTransfer { inner, meter })
251    }
252    fn websocket(&self, req: Prepared, events: WsEvents) -> Arc<dyn Socket> {
253        let meter = Meter::new(self.store.clone(), &req, self.simulated);
254        let m = meter.clone();
255        let inner = self.inner.websocket(
256            req,
257            Arc::new(move |e| {
258                match &e {
259                    WsEvent::Message(msg) => m.bytes(0, message_len(msg)),
260                    WsEvent::Closed { .. } => m.end(None),
261                    WsEvent::Failed(e) => m.end(Some(e.to_string())),
262                    WsEvent::Open { .. } => {}
263                }
264                events(e);
265            }),
266        );
267        Arc::new(ObservedSocket { inner, meter })
268    }
269    fn cookies(&self) -> Vec<crate::Cookie> {
270        self.inner.cookies()
271    }
272    fn clear_cookies(&self) {
273        self.inner.clear_cookies();
274    }
275    fn clear_cache(&self) {
276        self.inner.clear_cache();
277    }
278}
279struct ObservedTransfer {
280    inner: Arc<dyn Transfer>,
281    meter: Arc<Meter>,
282}
283impl Transfer for ObservedTransfer {
284    fn demand(&self, n: u32) {
285        self.inner.demand(n);
286    }
287    fn answer(&self, id: QuestionId, a: Answer) {
288        self.inner.answer(id, a);
289    }
290    fn cancel(&self) {
291        self.meter.end(Some("cancelled".into()));
292        self.inner.cancel();
293    }
294}
295struct ObservedSocket {
296    inner: Arc<dyn Socket>,
297    meter: Arc<Meter>,
298}
299pub(crate) fn message_len(m: &Message) -> u64 {
300    match m {
301        Message::Text(t) => t.len() as u64,
302        Message::Binary(b) => b.len() as u64,
303        Message::Close { .. } => 0,
304    }
305}
306impl Socket for ObservedSocket {
307    fn send(&self, m: Message, done: Completion) {
308        let n = message_len(&m);
309        let meter = self.meter.clone();
310        self.inner.send(
311            m,
312            Box::new(move |r| {
313                if r.is_ok() {
314                    meter.bytes(n, 0);
315                }
316                done(r);
317            }),
318        );
319    }
320    fn ping(&self, done: Completion) {
321        self.inner.ping(done);
322    }
323    fn close(&self, code: u16, reason: &str) {
324        self.meter.end(None);
325        self.inner.close(code, reason);
326    }
327    fn demand(&self, n: u32) {
328        self.inner.demand(n);
329    }
330}