Skip to content

Instantly share code, notes, and snippets.

@retep998
Last active August 21, 2016 11:38
Show Gist options
  • Save retep998/450ca335d494d89d77f54fd7b7d77fbc to your computer and use it in GitHub Desktop.
Save retep998/450ca335d494d89d77f54fd7b7d77fbc to your computer and use it in GitHub Desktop.
use std::io::Read;
use std::net::TcpStream;
use std::sync::atomic::{ ATOMIC_USIZE_INIT, AtomicUsize, Ordering };
use std::thread::{spawn, sleep};
use std::time::Duration;
static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
const NUM_SOCKETS: u32 = 10_000;
fn main() {
let mut streams = Vec::new();
for _ in 0..NUM_SOCKETS {
let mut stream = TcpStream::connect("127.0.0.1:273").unwrap();
stream.set_nonblocking(true).unwrap();
streams.push(stream);
}
spawn(move || {
loop {
let mut buf = [0u8; 1];
for stream in streams.iter_mut() {
let result = stream.read(&mut buf);
}
COUNTER.fetch_add(1, Ordering::Relaxed);
}
});
loop {
let num = COUNTER.swap(0, Ordering::Relaxed);
println!("{}", num);
sleep(Duration::from_secs(1));
}
}
use std::io::Read;
use std::net::TcpStream;
use std::sync::atomic::{ ATOMIC_USIZE_INIT, AtomicUsize, Ordering };
use std::thread::{Builder, sleep};
use std::time::Duration;
static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
const NUM_SOCKETS: u32 = 10_000;
fn main() {
for _ in 0..NUM_SOCKETS {
Builder::new().stack_size(0x10000).spawn(|| {
let mut tcp = TcpStream::connect("127.0.0.1:273").unwrap();
let mut buf = [0u8; 1];
loop {
tcp.read(&mut buf).unwrap();
COUNTER.fetch_add(1, Ordering::Relaxed);
}
}).unwrap();
sleep(Duration::from_millis(1));
}
loop {
let num = COUNTER.swap(0, Ordering::Relaxed);
println!("{}", num);
sleep(Duration::from_secs(1));
}
}
use std::io::Write;
use std::net::{TcpListener};
use std::thread::{Builder, sleep};
use std::time::Duration;
fn main() {
let listener = TcpListener::bind("127.0.0.1:273").unwrap();
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
Builder::new().stack_size(0x10000).spawn(move|| {
loop {
stream.write(&[0]).unwrap();
sleep(Duration::from_secs(1));
}
}).unwrap();
},
Err(e) => {
println!("{:?}", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment