Created
June 21, 2021 09:13
-
-
Save amiremohamadi/ce1e0b578ea22d448673812c31071b20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde::{Deserialize, Serialize}; | |
use std::io::prelude::Write; | |
use std::os::unix::net::UnixStream; | |
use std::time::SystemTime; | |
#[derive(Serialize, Deserialize, PartialEq, Debug)] | |
struct Message { | |
pub source: String, | |
pub target: String, | |
pub weight: usize, | |
pub time: std::time::SystemTime, | |
} | |
fn send_req() { | |
if let Ok(mut stream) = UnixStream::connect("/path/to/sock") { | |
println!("accepted"); | |
// TODO: read and init messages from dataset. | |
let msg = Message { | |
source: "127.0.0.1".to_string(), | |
target: "192.168.1.1".to_string(), | |
weight: 4, | |
time: std::time::SystemTime::now(), | |
}; | |
let data = bincode::serialize(&msg).unwrap(); | |
let data_len = bincode::serialize(&data.len()).unwrap(); | |
let msg = data_len.into_iter().chain(data).collect::<Vec<u8>>(); | |
for i in 0..10000 { | |
stream.write_all(msg.as_slice()); | |
} | |
} else { | |
println!("something bad happened"); | |
} | |
} | |
fn main() { | |
let mut threads = vec![]; | |
for i in 0..100 { | |
threads.push(std::thread::spawn(send_req)); | |
} | |
let then = SystemTime::now(); | |
for thread in threads { | |
thread.join(); | |
} | |
let now = SystemTime::now(); | |
println!("elapsed: {:?}", now.duration_since(then)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment