1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use structopt::StructOpt;

pub use super::RamBench;
use crate::quick::QuickCli;
use crate::report::BenchReport;
use crate::report::RamForm;

#[derive(Debug, StructOpt)]
#[structopt(name = "ram")]
/// 内存性能测试
///
/// 测试结果使用相对值进行比较,绝对值并没有什么实际意义。
///
/// `mem` 指定内存大小 {n}
/// 注意: 实际使用的内存大小为: 2^mem * 128MB {n}
/// 0 表示使用  128MB {n}
/// 1 表示使用  256MB {n}
/// 2 表示使用  512MB {n}
/// 3 表示使用 1024MB=1GB {n}
/// ... {n}
///
/// 在相同的参数下,使用的时间越少越好
pub struct RAMCli {
    /// 指定内存的大小
    #[structopt(long, default_value = "0")]
    pub mem: u8,

    /// 多少轮测试
    #[structopt(long, default_value = "16")]
    pub round: usize,

    #[structopt(flatten)]
    pub shared: crate::shared::SharedCli,
}

impl RAMCli {
    /// 运行 RAM 测试
    pub fn run(&self, job_id: Option<String>, reporter: Option<BenchReport>) {
        let mut ram = RamBench::new(2usize.pow(self.mem as u32));
        let results: Vec<_> = (0..self.round)
            .map(|idx| {
                println!("第 {} 轮的 内存 测试开始...", idx + 1);
                let result = ram.run_bench();
                println!(
                    "第 {} 轮的 内存 测试结束\n{}\n",
                    idx + 1,
                    result.to_string()
                );
                result
            })
            .collect();

        if let Some(reporter) = self.shared.get_reporter(reporter) {
            let form = RamForm::new(job_id, self.mem, results);
            println!("开始上报 内存 基准测试结果 ...");
            reporter.ram_report(&form);
            println!("上报 内存 基准测试结果 已完成")
        }
    }
}

impl From<&QuickCli> for RAMCli {
    fn from(q: &QuickCli) -> Self {
        Self {
            mem: q.mem_size,
            round: q.mem_round,
            shared: q.shared.clone(),
        }
    }
}