//! Performance test for loading data file and searching
//!
//! licensed under CC BY-SA 4.0,
//! To view a copy of this license, visit <http://creativecommons.org/licenses/by-sa/4.0/>
//!
//! Author: Andreas, <df1asc@darc.de>

use clap::Parser;
use env_logger;
use hamcty::CountryDatabase;
use log::info;
use std::time::Instant;
use std::env;

#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
    /// the ADI file to read
    #[arg(short, long, default_value = "test_data/cty.csv")]
    file: String,
}

fn main() {
    let _ = env::var("RUST_LOG").is_err_and(|_| {
        unsafe { env::set_var("RUST_LOG", "info") }
        false
    });
    env_logger::init();

    let args = Args::parse();

    info!("Running benchmark...");
    let t_read = Instant::now();

    let cdb = CountryDatabase::load(args.file).expect("Could not load cty data");
    info!("Load  : {} ms", t_read.elapsed().as_millis());

    let calls = [
        "DF1ASC",
        "LU1ZA",
        "PP6XX",
        "4U1WB",
        "1A5FF",
        "ZU8RRE",
        "4M05HH",
        "PA5TT",
        "DO3AT",
        "OE1TTR",
        "VL43FFF",
        "PS0ZF",
        "PI4ZWN/MILL",
        "4U0IARU",
        "HP4UU",
        "G6ICR",
        "TO5M",
        "CP3TTT",
        "5C4MM",
        "WZ9K",
    ];
    let t_search = Instant::now();
    for _ in 0..100 {
        for call in calls {
            let _ =  cdb.country(call);
            // println!("{call}: {:?}", cdb.country(call));
        }
    }
    info!("Search: {} ms", t_search.elapsed().as_millis());
}
