cbtop/export_reporting/
builder.rs1use std::collections::HashMap;
4
5use super::exporter::ReportExporter;
6use super::types::{
7 BenchmarkMetric, BenchmarkReport, ComparisonEntry, ComparisonReport, ExportFormat, ReportType,
8};
9
10#[derive(Debug)]
12pub struct ReportBuilder {
13 report_type: ReportType,
15 format: ExportFormat,
17 title: String,
19 metrics: Vec<BenchmarkMetric>,
21 comparisons: Vec<ComparisonEntry>,
23 metadata: HashMap<String, String>,
25}
26
27impl ReportBuilder {
28 pub fn new(report_type: ReportType) -> Self {
30 Self {
31 report_type,
32 format: ExportFormat::Json,
33 title: format!("{} Report", report_type.name()),
34 metrics: Vec::new(),
35 comparisons: Vec::new(),
36 metadata: HashMap::new(),
37 }
38 }
39
40 pub fn format(mut self, format: ExportFormat) -> Self {
42 self.format = format;
43 self
44 }
45
46 pub fn title(mut self, title: &str) -> Self {
48 self.title = title.to_string();
49 self
50 }
51
52 pub fn metric(mut self, name: &str, value: f64, unit: &str) -> Self {
54 self.metrics.push(BenchmarkMetric::new(name, value, unit));
55 self
56 }
57
58 pub fn comparison(mut self, metric: &str, baseline: f64, current: f64, unit: &str) -> Self {
60 self.comparisons
61 .push(ComparisonEntry::new(metric, baseline, current, unit));
62 self
63 }
64
65 pub fn metadata(mut self, key: &str, value: &str) -> Self {
67 self.metadata.insert(key.to_string(), value.to_string());
68 self
69 }
70
71 pub fn build(self) -> String {
73 match self.report_type {
74 ReportType::Benchmark | ReportType::Summary => {
75 let mut report = BenchmarkReport::new(&self.title);
76 report.metrics = self.metrics;
77 report.metadata = self.metadata;
78 ReportExporter::export_benchmark(&report, self.format)
79 }
80 ReportType::Comparison | ReportType::Regression => {
81 let mut report = ComparisonReport::new(&self.title);
82 report.entries = self.comparisons;
83 ReportExporter::export_comparison(&report, self.format)
84 }
85 }
86 }
87}