extern crate alloc;
use alloc::{string::String, vec::Vec};
use serde::{Deserialize, Serialize};
use crate::profiling::{RecordProfilingMetrics, StageEntry};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StageProfilingInfo {
pub stage_type: String,
pub index: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub call_count: u64,
pub total_time_ns: u64,
pub avg_time_ns: u64,
pub min_time_ns: u64,
pub max_time_ns: u64,
}
impl StageProfilingInfo {
fn from_entry(stage_type: &str, index: usize, entry: &StageEntry) -> Self {
let m = &entry.metrics;
Self {
stage_type: String::from(stage_type),
index,
name: entry.name.clone(),
call_count: m.call_count(),
total_time_ns: m.total_time_ns(),
avg_time_ns: m.avg_time_ns(),
min_time_ns: m.min_time_ns(),
max_time_ns: m.max_time_ns(),
}
}
}
impl RecordProfilingMetrics {
pub fn snapshot(&self) -> Vec<StageProfilingInfo> {
let mut out = Vec::new();
for (kind, stages) in [
("source", self.sources()),
("tap", self.taps()),
("link", self.links()),
] {
for (i, entry) in stages.iter().enumerate() {
out.push(StageProfilingInfo::from_entry(kind, i, entry));
}
}
out
}
}