make it atomic
This commit is contained in:
parent
594fd1d82a
commit
93769d47bc
36
src/main.rs
36
src/main.rs
@ -1,13 +1,15 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
net::{SocketAddr, TcpStream},
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Args {
|
||||
#[clap(default_value = "172.0.0.1:113")]
|
||||
#[clap(default_value = "127.0.0.1:113")]
|
||||
ip: SocketAddr,
|
||||
|
||||
#[clap(default_value = "20")]
|
||||
@ -19,28 +21,30 @@ fn main() {
|
||||
|
||||
let threads = cli.threads;
|
||||
|
||||
static mut SEND_COUNT: u64 = 0;
|
||||
println!("Target: {}", cli.ip);
|
||||
|
||||
let send_count = Arc::new(AtomicU64::new(0));
|
||||
|
||||
for _ in 0..threads {
|
||||
let send_count = Arc::clone(&send_count);
|
||||
std::thread::spawn(move || loop {
|
||||
match TcpStream::connect_timeout(&cli.ip, std::time::Duration::from_nanos(100)) {
|
||||
Ok(_) => unsafe {
|
||||
SEND_COUNT += 1;
|
||||
},
|
||||
Err(_) => unsafe {
|
||||
SEND_COUNT += 1;
|
||||
},
|
||||
}
|
||||
Ok(_) => send_count.fetch_add(1, Ordering::Relaxed),
|
||||
Err(_) => send_count.fetch_add(1, Ordering::Relaxed),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
std::thread::spawn(|| {
|
||||
std::thread::spawn(move || {
|
||||
let mut last_count = 0;
|
||||
loop {
|
||||
print!("{} Packets sent. {}/sec", unsafe { SEND_COUNT }, unsafe {
|
||||
SEND_COUNT - last_count
|
||||
});
|
||||
last_count = unsafe { SEND_COUNT };
|
||||
let send_count = send_count.load(Ordering::Relaxed);
|
||||
print!(
|
||||
"{} Packets sent. {}/sec",
|
||||
send_count,
|
||||
send_count - last_count
|
||||
);
|
||||
last_count = send_count;
|
||||
io::stdout().flush().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
print!("\r");
|
||||
@ -49,5 +53,7 @@ fn main() {
|
||||
|
||||
println!("{} Threads created.", threads);
|
||||
|
||||
loop {}
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user