Mountain/IPC/Common/ServiceInfo/
ServicePerformance.rs1#![allow(non_snake_case)]
2
3use std::time::Instant;
9
10use serde::Serialize;
11
12#[derive(Debug, Clone, Serialize)]
13pub struct Struct {
14 pub RequestCount:u64,
15 pub ErrorCount:u64,
16 pub AverageResponseTimeMs:f64,
17 #[serde(skip)]
18 pub LastUpdated:Instant,
19}
20
21impl Struct {
22 pub fn new() -> Self {
23 Self {
24 RequestCount:0,
25 ErrorCount:0,
26 AverageResponseTimeMs:0.0,
27 LastUpdated:Instant::now(),
28 }
29 }
30
31 pub fn RecordRequest(&mut self, ResponseTimeMs:f64) {
32 self.RequestCount += 1;
33 if self.AverageResponseTimeMs == 0.0 {
34 self.AverageResponseTimeMs = ResponseTimeMs;
35 } else {
36 self.AverageResponseTimeMs = (self.AverageResponseTimeMs * (self.RequestCount - 1) as f64 + ResponseTimeMs)
37 / self.RequestCount as f64;
38 }
39 self.LastUpdated = Instant::now();
40 }
41
42 pub fn RecordError(&mut self) {
43 self.ErrorCount += 1;
44 self.LastUpdated = Instant::now();
45 }
46
47 pub fn ErrorRate(&self) -> f64 {
48 if self.RequestCount == 0 {
49 return 0.0;
50 }
51 self.ErrorCount as f64 / self.RequestCount as f64
52 }
53}
54
55impl Default for Struct {
56 fn default() -> Self { Self::new() }
57}