entrenar/efficiency/platform/
edge.rs1use serde::{Deserialize, Serialize};
4
5use super::budget::{BudgetViolation, WasmBudget};
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct EdgeEfficiency {
10 pub binary_size_bytes: u64,
12 pub startup_latency_ms: u64,
14 pub inference_latency_p99_ms: f64,
16 pub memory_footprint_bytes: u64,
18}
19
20impl EdgeEfficiency {
21 pub fn new(
23 binary_size_bytes: u64,
24 startup_latency_ms: u64,
25 inference_latency_p99_ms: f64,
26 memory_footprint_bytes: u64,
27 ) -> Self {
28 Self {
29 binary_size_bytes,
30 startup_latency_ms,
31 inference_latency_p99_ms,
32 memory_footprint_bytes,
33 }
34 }
35
36 pub fn meets_wasm_budget(&self, budget: &WasmBudget) -> bool {
38 self.binary_size_bytes <= budget.max_binary_size
39 && self.startup_latency_ms <= budget.max_startup_ms
40 && self.memory_footprint_bytes <= budget.max_memory_bytes
41 }
42
43 pub fn binary_size_mb(&self) -> f64 {
45 self.binary_size_bytes as f64 / (1024.0 * 1024.0)
46 }
47
48 pub fn memory_footprint_mb(&self) -> f64 {
50 self.memory_footprint_bytes as f64 / (1024.0 * 1024.0)
51 }
52
53 pub fn max_throughput_per_sec(&self) -> f64 {
55 if self.inference_latency_p99_ms > 0.0 {
56 1000.0 / self.inference_latency_p99_ms
57 } else {
58 0.0
59 }
60 }
61
62 pub fn budget_violations(&self, budget: &WasmBudget) -> Vec<BudgetViolation> {
64 let mut violations = Vec::new();
65
66 if self.binary_size_bytes > budget.max_binary_size {
67 violations.push(BudgetViolation::BinarySize {
68 actual: self.binary_size_bytes,
69 limit: budget.max_binary_size,
70 });
71 }
72
73 if self.startup_latency_ms > budget.max_startup_ms {
74 violations.push(BudgetViolation::StartupLatency {
75 actual: self.startup_latency_ms,
76 limit: budget.max_startup_ms,
77 });
78 }
79
80 if self.memory_footprint_bytes > budget.max_memory_bytes {
81 violations.push(BudgetViolation::MemoryFootprint {
82 actual: self.memory_footprint_bytes,
83 limit: budget.max_memory_bytes,
84 });
85 }
86
87 violations
88 }
89}
90
91impl Default for EdgeEfficiency {
92 fn default() -> Self {
93 Self {
94 binary_size_bytes: 0,
95 startup_latency_ms: 0,
96 inference_latency_p99_ms: 0.0,
97 memory_footprint_bytes: 0,
98 }
99 }
100}