aprender_zram_cli/commands/
benchmark.rs1use clap::Args;
6use trueno_zram_core::benchmark::{
7 generate_test_pages, parse_algorithm, run_benchmark, DataPattern,
8};
9
10#[derive(Debug, Args)]
12pub struct BenchmarkArgs {
13 #[arg(short = 'n', long, default_value = "10000")]
21 pub pages: usize,
22
23 #[arg(short, long, default_value = "all")]
25 pub algorithm: String,
26
27 #[arg(long, default_value = "mixed")]
31 pub pattern: String,
32}
33
34pub fn benchmark(args: &BenchmarkArgs) -> Result<(), Box<dyn std::error::Error>> {
40 let pattern = DataPattern::parse(&args.pattern)
41 .ok_or_else(|| format!("Unknown pattern: {}", args.pattern))?;
42
43 let algorithms = parse_algorithm(&args.algorithm)
44 .ok_or_else(|| format!("Unknown algorithm: {}", args.algorithm))?;
45
46 println!("trueno-zram Compression Benchmark");
47 println!("==================================");
48 println!("Pages: {}", args.pages);
49 println!("Pattern: {pattern:?}");
50 println!();
51
52 let pages = generate_test_pages(args.pages, pattern);
54
55 println!(
56 "{:<15} {:>10} {:>12} {:>12} {:>10}",
57 "Algorithm", "Backend", "Compress", "Decompress", "Ratio"
58 );
59 println!("{}", "-".repeat(60));
60
61 for algo in algorithms {
62 let result = run_benchmark(algo, &pages)?;
63
64 let compress_throughput = result.compress_throughput() / 1e9;
65 let decompress_throughput = result.decompress_throughput() / 1e9;
66
67 println!(
68 "{:<15} {:>10} {:>10.2} GB/s {:>10.2} GB/s {:>9.2}x",
69 format!("{:?}", result.algorithm),
70 format!("{:?}", result.backend),
71 compress_throughput,
72 decompress_throughput,
73 result.compression_ratio()
74 );
75 }
76
77 Ok(())
78}