-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
95 lines (73 loc) · 2.34 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extern crate mux;
extern crate time;
use mux::session::*;
extern crate byteorder;
extern crate rand;
use mux::Rmsg;
use std::cmp::max;
use std::net::TcpStream;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
fn test_session(socket: TcpStream) {
println!("Testing mux client session.");
let session = Arc::new(MuxSession::new(socket).unwrap());
let iters = 10_000;
let threadc = 50;
let startt = time::get_time();
let threads: Vec<thread::JoinHandle<Duration>> = (0..threadc).map(|id| {
let session = session.clone();
thread::spawn(move || {
let mut ping_time = Duration::new(0, 0);
let mut pingc = 0;
for _ in 0..iters {
if rand::random::<u8>() > 64 {
let b = format!("Hello, world: {}", id).into_bytes();
let frame = mux::Tdispatch::basic_("/foo".to_string(), b);
let msg = session.dispatch(&frame).unwrap();
if let Rmsg::Ok(body) = msg.msg {
let _ = String::from_utf8(body).unwrap();
} else {
panic!("Error during mux request!");
}
} else {
ping_time = ping_time + session.ping().unwrap();
pingc += 1;
}
}
ping_time/max(1, pingc)
})
}).collect();
let mut total_ping = Duration::new(0, 0);
let threadc = threads.len() as u32;
for t in threads {
total_ping = total_ping + t.join().unwrap();
}
let rps = {
let elapsed = time::get_time() - startt;
((iters*threadc) as f32/(elapsed.num_milliseconds() as f32)) * 1e3
};
println!("Finished. Rps: {}. Mean Ping: {:?}", rps, total_ping/threadc);
}
fn main() {
let socket = TcpStream::connect(("localhost", 9000)).unwrap();
test_session(socket);
}
#[cfg(test)]
mod tests {
use std::process::Command;
#[test]
fn run() {
test_crate("mux");
}
fn test_crate(subcrate: &str) {
let status = Command::new("cargo")
.args(&["test", "-p", subcrate])
.status()
.unwrap();
assert!(status.success(),
"test for sub-crate: {} returned: {:?}",
subcrate,
status.code().unwrap());
}
}