From 72f1409e1bdaabcf4415904def4b0a9400911d1d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 29 Feb 2024 01:05:41 +0800 Subject: [PATCH] with clap --- Cargo.lock | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +- src/main.rs | 59 +++++++++-------- src/name.rs | 4 +- 4 files changed, 216 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5eb14e3..e8ffbe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,54 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "base16384" version = "0.1.0" @@ -14,6 +62,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "deranged" version = "0.3.11" @@ -23,6 +117,12 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "itoa" version = "1.0.10" @@ -46,6 +146,7 @@ name = "namerena-rs" version = "0.1.0" dependencies = [ "base16384", + "clap", "tracing", "tracing-subscriber", ] @@ -143,6 +244,12 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +[[package]] +name = "strsim" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" + [[package]] name = "syn" version = "2.0.51" @@ -259,6 +366,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "valuable" version = "0.1.0" @@ -286,3 +399,69 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d380ba1dc7187569a8a9e91ed34b8ccfc33123bbacb8c0aed2d1ad7f3ef2dc5f" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68e5dcfb9413f53afd9c8f86e56a7b4d86d9a2fa26090ea2dc9e40fba56c6ec6" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dab469ebbc45798319e69eebf92308e541ce46760b49b18c6b3fe5e8965b30f" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a4e9b6a7cac734a8b4138a4e1044eac3404d8326b6c0f939276560687a033fb" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b0ec9c422ca95ff34a78755cfa6ad4a51371da2a5ace67500cf7ca5f232c58" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704131571ba93e89d7cd43482277d6632589b18ecf4468f591fbae0a8b101614" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42079295511643151e98d61c38c0acc444e52dd42ab456f7ccfd5152e8ecf21c" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6" diff --git a/Cargo.toml b/Cargo.toml index 6bd34eb..a760c7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,12 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# tokio = { version = "1.36.0", features = ["full"] } - -# serde = { version = "1.0", features = ["derive"] } -# md-5 = "0.10.6" -# toml = "0.8.10" -# base64 = "0.21.7" +clap = { version = "4.5.1", features = ["derive"] } base16384 = "0.1.0" tracing = "0.1.40" diff --git a/src/main.rs b/src/main.rs index adb9297..27bbb66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod data_struct; mod name; use base16384::Base16384Utf8; +use clap::Parser; use tracing::info; /// 根据 u64 生成对应的 name @@ -39,14 +40,29 @@ const allow_d: u32 = 10; #[allow(non_upper_case_globals)] const report_interval: u32 = 1_000_0000; -fn cacl(max: u64, step: usize, top: u32, id: u64) { +#[derive(Parser, Debug)] +pub struct Command { + #[arg(long, default_value_t = 0)] + pub start: u64, + #[arg(long, default_value_t = u64::MAX)] + pub end: u64, + #[arg(long, short = 't', default_value_t = 10)] + pub thread_count: u32, + #[arg(long, default_value_t = 750)] + pub top: u32, + #[arg(long)] + pub team: String, +} + +fn cacl(max: u64, step: usize, top: u32, id: u64, team: &String) { let mut start_time = std::time::Instant::now(); let mut k: u64 = 0; let mut top = top; for i in (0+id..max).step_by(step) { let name = gen_name(i as u64); - let full_name = format!("{}@shenjack", name); - let namer = name::Namer::new(&full_name); + // let full_name = format!("{}@shenjack", name); + // let namer = name::Namer::new(&full_name); + let namer = name::Namer::new_raw(name, team); if let Some(namer) = namer { let prop = namer.get_property(); if (prop + allow_d as f32) > top as f32 { @@ -54,13 +70,15 @@ fn cacl(max: u64, step: usize, top: u32, id: u64) { info!("新的最高属性 {}", prop); top = prop as u32; } - info!("{:>10}|{}|{}", i, full_name, show_name(&namer)); + let name = gen_name(i as u64); + let full_name = format!("{}@{}", name, team); + info!("{:>20}|{}|{}", i, full_name, show_name(&namer)); } } k += 1; if k > report_interval as u64 { let now = std::time::Instant::now(); - info!("{} {} {}/s", k, id, k / now.duration_since(start_time).as_secs()); + info!("{:>20} {} {}/s", i, id, k / now.duration_since(start_time).as_secs()); start_time = now; k = 0; } @@ -69,34 +87,25 @@ fn cacl(max: u64, step: usize, top: u32, id: u64) { fn main() { tracing_subscriber::fmt::init(); - // let team = "shenjack"; - // cli 获取 max - let max = match std::env::args().nth(1) { - Some(arg) => arg.parse().unwrap_or(i64::MAX), - None => i64::MAX, - }; - // cli 获取线程数量 - let thread_count = match std::env::args().nth(2) { - Some(arg) => arg.parse().unwrap_or(10), - None => 10, - }; + let mut cli_arg = Command::parse(); - // 新建线程计算 - let top: u32 = 750; // 将数据量处理成可被 thread_count 整除 - let left = max % thread_count; - // max += left; + let left = cli_arg.start % cli_arg.thread_count as u64; + cli_arg.end += left; + let mut n = 0; - let mut threads = Vec::with_capacity(thread_count as usize); - for i in 0..thread_count { - let top = top; - let max = max / thread_count + if i < left { 1 } else { 0 }; + let mut threads = Vec::with_capacity(cli_arg.thread_count as usize); + for i in 0..cli_arg.thread_count { + let top = cli_arg.top; + let max = cli_arg.end / cli_arg.thread_count as u64; n += 1; let thread_name = format!("thread_{}", i); + let thread_count = cli_arg.thread_count; + let team = cli_arg.team.clone(); threads.push(std::thread::spawn(move || { info!("线程 {} 开始计算", thread_name); - cacl(max as u64, thread_count as usize, top as u32, n as u64); + cacl(max as u64, thread_count as usize, top as u32, n as u64, &team); info!("线程 {} 结束计算", thread_name); })); } diff --git a/src/name.rs b/src/name.rs index 787ff6e..91b1459 100644 --- a/src/name.rs +++ b/src/name.rs @@ -35,10 +35,10 @@ impl Namer { warn!("Team too long({}): {}", team.len(), team); return None; } - Self::new_raw(name.to_string(), team.to_string()) + Self::new_raw(name.to_string(), &team.to_string()) } - pub fn new_raw(name: String, team: String) -> Option { + pub fn new_raw(name: String, team: &String) -> Option { let mut val = [0_u8; 256]; for i in 0..256 {