From c10a0c3e4f0fc600f30c0bb5bd340b0b328d1c6c Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Tue, 19 Nov 2024 19:02:53 -0500 Subject: [PATCH] add benchmarking --- Cargo.lock | 290 +++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 19 +++ benches/nvrs_bench.rs | 52 ++++++++ src/api/aur.rs | 7 +- src/config.rs | 17 +-- src/lib.rs | 47 +++++++ src/main.rs | 2 +- 7 files changed, 417 insertions(+), 17 deletions(-) create mode 100644 benches/nvrs_bench.rs create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 403db5b..e4393b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,21 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstream" version = "0.6.18" @@ -117,6 +132,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.2.1" @@ -132,6 +153,33 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "clap" version = "4.5.21" @@ -203,6 +251,73 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "criterion" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +dependencies = [ + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "is-terminal", + "itertools", + "num-traits", + "once_cell", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "displaydoc" version = "0.2.5" @@ -214,6 +329,12 @@ dependencies = [ "syn", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -400,6 +521,16 @@ dependencies = [ "tracing", ] +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.15.1" @@ -418,6 +549,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "http" version = "1.1.0" @@ -685,12 +822,32 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi 0.4.0", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -773,7 +930,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "wasi", "windows-sys 0.52.0", @@ -796,12 +953,22 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "nvrs" version = "0.1.3" dependencies = [ "clap", "colored", + "criterion", "futures", "lazy_static", "reqwest", @@ -826,6 +993,12 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "oorandom" +version = "11.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" + [[package]] name = "openssl" version = "0.10.68" @@ -917,6 +1090,34 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + [[package]] name = "proc-macro2" version = "1.0.89" @@ -935,6 +1136,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.5.7" @@ -944,6 +1165,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "reqwest" version = "0.12.9" @@ -1065,6 +1315,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.26" @@ -1268,6 +1527,16 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tokio" version = "1.41.1" @@ -1443,6 +1712,16 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -1535,6 +1814,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "windows-registry" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 6f61531..1a1f346 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,3 +41,22 @@ codegen-units = 1 panic = "abort" strip = true opt-level = "z" + +[dev-dependencies] +criterion = { version = "0.5", features = ["html_reports"] } + +[[bench]] +name = "nvrs_bench" +harness = false + +[lib] +name = "nvrs" +path = "src/lib.rs" + +[[bin]] +name = "nvrs" +path = "src/main.rs" + +[profile.bench] +opt-level = 3 +debug = false diff --git a/benches/nvrs_bench.rs b/benches/nvrs_bench.rs new file mode 100644 index 0000000..1674954 --- /dev/null +++ b/benches/nvrs_bench.rs @@ -0,0 +1,52 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +fn configure_criterion() -> Criterion { + Criterion::default() + .sample_size(50) + .measurement_time(std::time::Duration::from_secs(10)) + .with_output_color(true) +} + +fn bench_config_load(c: &mut Criterion) { + c.bench_function("config_load", |b| { + b.iter(|| nvrs::config::load(black_box(None))) + }); +} + +fn bench_verfile_operations(c: &mut Criterion) { + let (config_content, _, _) = nvrs::config::load(None); + + c.bench_function("verfile_load", |b| { + b.iter(|| nvrs::verfiles::load(black_box(config_content.__config__.clone()))) + }); +} + +fn bench_api_requests(c: &mut Criterion) { + use tokio::runtime::Runtime; + let rt = Runtime::new().unwrap(); + + let mock_package = nvrs::config::Package { + source: "aur".to_string(), + aur: "hyprland-git".to_string(), + github: String::new(), + gitlab: String::new(), + host: String::new(), + prefix: String::new(), + }; + + c.bench_function("api_request", |b| { + b.iter(|| { + rt.block_on(nvrs::run_source( + ("hyprland-git".to_string(), mock_package.clone()), + None, + )) + }) + }); +} + +criterion_group! { + name = benches; + config = configure_criterion(); + targets = bench_config_load, bench_verfile_operations, bench_api_requests +} +criterion_main!(benches); diff --git a/src/api/aur.rs b/src/api/aur.rs index 09d2d56..d5023db 100644 --- a/src/api/aur.rs +++ b/src/api/aur.rs @@ -26,12 +26,9 @@ pub fn get_latest(package: String, _: Vec, _: String) -> crate::api::Rel Some(crate::api::Release { tag_name: first_result .get("Version") - .unwrap() - .to_string() - .split('-') - .next() + .and_then(|v| v.as_str()) + .and_then(|s| s.split('-').next()) .unwrap_or("") - .replace("\"", "") .to_string(), html_url: first_result .get("URL") diff --git a/src/config.rs b/src/config.rs index 53f3a7f..94d3aac 100644 --- a/src/config.rs +++ b/src/config.rs @@ -101,7 +101,7 @@ pub fn load(custom_path: Option) -> (Config, PathBuf, Option) { return ( toml_content.clone(), PathBuf::from(config_path), - load_keyfile(toml_content), + load_keyfile(&toml_content), ); } else { crate::custom_error("specified config file not found", String::new(), ""); @@ -143,17 +143,14 @@ pub fn load(custom_path: Option) -> (Config, PathBuf, Option) { let toml_content: Config = toml::from_str(&content).expect("error reading the config file"); - ( - toml_content.clone(), - path_actual, - load_keyfile(toml_content), - ) + let keyfile = load_keyfile(&toml_content); + (toml_content, path_actual, keyfile) } -fn load_keyfile(toml_content: Config) -> Option { - if let Some(config_content) = toml_content.__config__ { - if let Some(keyfile) = config_content.keyfile { - let keyfile_path = Path::new(&keyfile); +fn load_keyfile(toml_content: &Config) -> Option { + if let Some(ref config_content) = toml_content.__config__ { + if let Some(ref keyfile) = config_content.keyfile { + let keyfile_path = Path::new(keyfile); let keyfile_content = if keyfile_path.exists() && keyfile_path.is_file() { fs::read_to_string(keyfile_path).unwrap_or_default() } else { diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2ed77ed --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,47 @@ +pub mod api; +pub mod config; +pub mod verfiles; + +pub use crate::config::{Config, ConfigTable, Keyfile, Package}; +pub use crate::verfiles::{Data, Package as VerPackage, Verfile}; + +use colored::Colorize; +use std::sync::Mutex; + +lazy_static::lazy_static! { + static ref MSG_NOEXIT: Mutex = Mutex::new(false); +} + +pub async fn run_source( + package: (String, Package), + keyfile: Option, +) -> Option { + let source = package.1.source.clone(); + if let Some(api_used) = api::API_LIST.iter().find(|a| a.name == source) { + let api_key = if let Some(k) = keyfile { + k.get_api_key(source) + } else { + String::new() + }; + + Some( + (api_used.func)( + package.0, + package.1.get_api_arg(api_used.name).unwrap(), + api_key, + ) + .await?, + ) + } else { + custom_error("api not found: ", source, ""); + None + } +} + +pub fn custom_error(message: &'static str, message_ext: String, override_exit: &str) { + println!("! {}{}", message.red(), message_ext.replace('\n', "\n ")); + + if override_exit != "noexit" && !*MSG_NOEXIT.lock().unwrap() { + std::process::exit(1); + } +} diff --git a/src/main.rs b/src/main.rs index fbab430..2c0d00d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -210,7 +210,7 @@ copies or substantial portions of the Software.", } } -async fn run_source( +pub async fn run_source( package: (String, config::Package), keyfile: Option, ) -> Option {