mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Examples, HUGE fixes, test, make it alot smother
- switch `listen` to async in oder to verify if the bind was successful - Introduce the following examples - network speed - chat - fileshare - add additional tests - fix dropping stream before last messages can be handled bug, when dropping a stream, BParticipant will wait for prio to be empty before dropping the stream and sending the signal - correct closing of stream and participant - move tcp to protocols and create udp front and backend - tracing and fixing a bug that is caused by not waiting for configuration after receiving a frame - fix a bug in network-speed, but there is still a bug if trace=warn after 2.000.000 messages the server doesnt get that client has shut down and seems to lock somewhere. hard to reproduce open tasks [ ] verify UDP works correctly, especcially the connect! [ ] implements UDP shutdown correctly, the one created in connect! [ ] unify logging [ ] fill metrics [ ] fix dropping stream before last messages can be handled bug [ ] add documentation [ ] add benchmarks [ ] remove async_serde??? [ ] add mpsc
This commit is contained in:
13
network/examples/tcp-loadtest/Cargo.toml
Normal file
13
network/examples/tcp-loadtest/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[workspace]
|
||||
|
||||
[package]
|
||||
name = "tcp-loadtest"
|
||||
version = "0.1.0"
|
||||
authors = ["Marcel Märtens <marcel.cochem@googlemail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
rand = "0.7"
|
108
network/examples/tcp-loadtest/src/main.rs
Normal file
108
network/examples/tcp-loadtest/src/main.rs
Normal file
@ -0,0 +1,108 @@
|
||||
use std::{
|
||||
env,
|
||||
io::Write,
|
||||
net::{SocketAddr, TcpStream},
|
||||
sync::{
|
||||
atomic::{AtomicU64, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
extern crate rand;
|
||||
|
||||
fn setup() -> Result<SocketAddr, u32> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() < 3 {
|
||||
println!("usage: tcp-loadtest <ip> <port>");
|
||||
println!("example: tcp-loadtest 127.0.0.1 52000");
|
||||
return Err(1);
|
||||
}
|
||||
let a: SocketAddr = format!("{}:{}", args[1], args[2]).parse().unwrap();
|
||||
println!("You provided address: {}", &a);
|
||||
return Ok(a);
|
||||
}
|
||||
/// This example file is not running veloren-network at all,
|
||||
/// instead it's just trying to create 4 threads and pump as much bytes as
|
||||
/// possible through a specific listener, the listener needs to be created
|
||||
/// before this program is started.
|
||||
fn main() -> Result<(), u32> {
|
||||
let addr = Arc::new(setup()?);
|
||||
let data: Arc<String> = Arc::new(
|
||||
(0..1000000)
|
||||
.map(|_| (0x20u8 + (rand::random::<f32>() * 96.0) as u8) as char)
|
||||
.collect(),
|
||||
);
|
||||
|
||||
let total_bytes_send = Arc::new(AtomicU64::new(0));
|
||||
let total_send_count = Arc::new(AtomicU64::new(0));
|
||||
let total_finished_threads = Arc::new(AtomicU64::new(0));
|
||||
let start_time = Instant::now();
|
||||
|
||||
let mut threads = Vec::new();
|
||||
let thread_count = 4;
|
||||
for i in 0..thread_count {
|
||||
let addr = addr.clone();
|
||||
let total_bytes_send = total_bytes_send.clone();
|
||||
let total_send_count = total_send_count.clone();
|
||||
let total_finished_threads = total_finished_threads.clone();
|
||||
let data = data.clone();
|
||||
threads.push(thread::spawn(move || {
|
||||
let mut stream = match TcpStream::connect(addr.as_ref()) {
|
||||
Err(err) => {
|
||||
total_finished_threads.fetch_add(1, Ordering::Relaxed);
|
||||
panic!("could not open connection: {}", err);
|
||||
},
|
||||
Ok(s) => s,
|
||||
};
|
||||
let mut thread_bytes_send: u64 = 0;
|
||||
let mut thread_last_sync = Instant::now();
|
||||
|
||||
loop {
|
||||
let tosend: u64 = rand::random::<u16>() as u64 * 10 + 1000;
|
||||
thread_bytes_send += tosend;
|
||||
|
||||
let cur = Instant::now();
|
||||
if cur.duration_since(thread_last_sync) >= Duration::from_secs(1) {
|
||||
thread_last_sync = cur;
|
||||
println!("[{}]send: {}MiB/s", i, thread_bytes_send / (1024 * 1024));
|
||||
total_bytes_send.fetch_add(thread_bytes_send, Ordering::Relaxed);
|
||||
thread_bytes_send = 0;
|
||||
}
|
||||
|
||||
total_send_count.fetch_add(1, Ordering::Relaxed);
|
||||
let ret = stream.write_all(data[0..(tosend as usize)].as_bytes());
|
||||
if ret.is_err() {
|
||||
println!("[{}] error: {}", i, ret.err().unwrap());
|
||||
total_finished_threads.fetch_add(1, Ordering::Relaxed);
|
||||
return;
|
||||
}
|
||||
//stream.flush();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
while total_finished_threads.load(Ordering::Relaxed) < thread_count {
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
|
||||
let cur = Instant::now();
|
||||
let dur = cur.duration_since(start_time);
|
||||
println!("================");
|
||||
println!("test endet");
|
||||
println!(
|
||||
"total send: {}MiB",
|
||||
total_bytes_send.load(Ordering::Relaxed) / (1024 * 1024)
|
||||
);
|
||||
println!("total time: {}s", dur.as_secs());
|
||||
println!(
|
||||
"average: {}KiB/s",
|
||||
total_bytes_send.load(Ordering::Relaxed) * 1000 / dur.as_millis() as u64 / 1024
|
||||
);
|
||||
println!(
|
||||
"send count: {}/s",
|
||||
total_send_count.load(Ordering::Relaxed) * 1000 / dur.as_millis() as u64
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user