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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use devx_cmd::read;
use khonsu_tools::{
    publish,
    universal::{
        anyhow, audit,
        clap::{self, Parser},
        code_coverage,
    },
};
use sysinfo::{RefreshKind, System, SystemExt};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

#[derive(Parser, Debug)]
pub enum Commands {
    GenerateBenchmarkOverview,
    #[clap(flatten)]
    Tools(khonsu_tools::Commands),
}

fn main() -> anyhow::Result<()> {
    let command = Commands::parse();
    match command {
        Commands::GenerateBenchmarkOverview => generate_benchmark_overview(),
        Commands::Tools(command) => command.execute::<Config>(),
    }
}

fn generate_benchmark_overview() -> anyhow::Result<()> {
    let overview = std::fs::read_to_string("benchmarks/overview.html")?;
    let now = OffsetDateTime::now_utc();
    let git_rev = read!("git", "rev-parse", "HEAD")?;
    let git_rev = git_rev.trim();

    let overview = overview.replace("TIMESTAMP", &now.format(&Rfc3339).unwrap());
    let overview = overview.replace("GITREV", git_rev);
    let environment = match std::env::var("ENVIRONMENT") {
        Ok(environment) => environment,
        Err(_) => {
            let whoami = read!("whoami")?;
            let whoami = whoami.trim();
            let system = System::new_with_specifics(RefreshKind::new().with_cpu().with_memory());
            format!(
                "on {}'s machine with {} cores and {} GB of RAM",
                whoami,
                system
                    .physical_core_count()
                    .expect("unable to count processor cores"),
                system.total_memory() / 1024 / 1024,
            )
        }
    };
    let overview = overview.replace("ENVIRONMENT", &environment);

    std::fs::write("target/criterion/index.html", &overview)?;

    Ok(())
}

enum Config {}

impl khonsu_tools::Config for Config {
    type Publish = Self;

    type Universal = Self;
}

impl khonsu_tools::universal::Config for Config {
    type Audit = Self;

    type CodeCoverage = Self;
}

impl audit::Config for Config {
    fn args() -> Vec<String> {
        vec![
            String::from("--all-features"),
            String::from("--exclude=xtask"),
            String::from("--exclude=benchmarks"),
        ]
    }
}

impl publish::Config for Config {
    fn paths() -> Vec<String> {
        vec![String::from("nebari")]
    }
}

impl code_coverage::Config for Config {
    fn cargo_args() -> Vec<String> {
        vec![String::from("test"), String::from("--all-features")]
    }
}