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