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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
extern crate clap;
extern crate stdbench;
extern crate stderrlog;
extern crate tempdir;

use clap::{App, Arg};
use log::{error, info, warn};
use std::collections::HashSet;
use std::env;
use std::path::PathBuf;
use std::process;
use stdbench::build;
use stdbench::config::Config;
use stdbench::error::Error;
use stdbench::run::process_run;
use strum::IntoEnumIterator;

pub fn app<'a, 'b>() -> App<'a, 'b> {
    App::new("PISA standard benchmark for regression tests.")
        .version("1.0")
        .author("Michal Siedlaczek <michal.siedlaczek@gmail.com>")
        .arg(
            Arg::with_name("config-file")
                .help("Configuration file path")
                .long("config-file")
                .takes_value(true)
                .required(true),
        )
        .arg(
            Arg::with_name("print-stages")
                .help("Prints all available stages")
                .long("print-stages"),
        )
        .arg(
            Arg::with_name("suppress")
                .help("A list of stages to suppress")
                .long("suppress")
                .multiple(true)
                .takes_value(true),
        )
        .arg(
            Arg::with_name("collections")
                .help("Filter out collections you want to run")
                .long("collections")
                .multiple(true)
                .takes_value(true),
        )
}

fn filter_collections<'a, I>(mut config: &mut Config, collections: I)
where
    I: IntoIterator<Item = &'a str>,
{
    let colset = collections.into_iter().collect::<HashSet<&str>>();
    config.collections = config
        .collections
        .iter()
        .filter(|c| {
            let name = &c.name;
            colset.contains(&name.as_ref())
        })
        .cloned()
        .collect();
    config.runs = config
        .runs
        .iter()
        .filter(|r| colset.contains(&r.collection.name.as_ref()))
        .cloned()
        .collect();
}

fn parse_config(args: Vec<String>) -> Result<Config, Error> {
    let matches = app().get_matches_from(args);
    if matches.is_present("print-stages") {
        for stage in stdbench::Stage::iter() {
            println!("{}", stage);
        }
        process::exit(0);
    }
    info!("Parsing config");
    let config_file = matches
        .value_of("config-file")
        .ok_or("failed to read required argument")?;
    let mut config = Config::from_file(PathBuf::from(config_file))?;
    if let Some(stages) = matches.values_of("suppress") {
        for name in stages {
            if let Ok(stage) = name.parse() {
                config.suppress_stage(stage);
            } else {
                warn!("Requested suppression of stage `{}` that is invalid", name);
            }
        }
    }
    if let Some(collections) = matches.values_of("collections") {
        filter_collections(&mut config, collections);
    }
    Ok(config)
}

#[cfg_attr(tarpaulin, skip)]
fn run() -> Result<(), Error> {
    stderrlog::new().verbosity(100).init().unwrap();
    let config = parse_config(env::args().collect())?;
    info!("Code source: {:?}", &config.source);
    let executor = config.executor()?;
    info!("Executor ready");

    for collection in &config.collections {
        build::collection(executor.as_ref(), collection, &config)?;
    }
    for run in &config.runs {
        info!("{:?}", run);
        process_run(executor.as_ref(), run)?;
    }
    Ok(())
}

#[cfg_attr(tarpaulin, skip)]
fn main() {
    if let Err(err) = run() {
        error!("{}", err);
        process::exit(1);
    }
}

#[cfg(test)]
mod test {
    extern crate tempdir;

    use super::stdbench::Stage;
    use super::*;
    use std::fs;
    use tempdir::TempDir;

    #[test]
    fn test_parse_config_missing_file() {
        assert!(parse_config(
            ["exe", "--config-file", "file"]
                .into_iter()
                .map(|&s| String::from(s))
                .collect(),
        )
        .is_err());
    }

    #[test]
    fn test_parse_config() {
        let tmp = TempDir::new("tmp").unwrap();
        let config_file = tmp.path().join("conf.yml");
        let yml = "
workdir: /tmp
source:
    type: git
    branch: dev
    url: https://github.com/pisa-engine/pisa.git
collections:
    - name: wapo
      kind: wapo
      description: WashingtonPost.v2
      collection_dir: coll
      forward_index: fwd/wapo
      inverted_index: inv/wapo
      encodings:
        - block_simdbp";
        fs::write(config_file.to_str().unwrap(), &yml).unwrap();
        let conf = parse_config(
            [
                "exe",
                "--config-file",
                config_file.to_str().unwrap(),
                "--suppress",
                "compile",
                "invalid",
            ]
            .into_iter()
            .map(|&s| String::from(s))
            .collect(),
        )
        .unwrap();
        assert!(conf.is_suppressed(Stage::Compile));
    }
}