forked from smoltcp-rs/smoltcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rs
165 lines (136 loc) · 5.21 KB
/
benchmark.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#![allow(clippy::collapsible_if)]
mod utils;
use std::cmp;
use std::collections::BTreeMap;
use std::sync::atomic::{Ordering, AtomicBool};
use std::thread;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::os::unix::io::AsRawFd;
use log::debug;
use smoltcp::phy::{Device, Medium, wait as phy_wait};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
use smoltcp::iface::{NeighborCache, InterfaceBuilder};
use smoltcp::socket::SocketSet;
use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
use smoltcp::time::{Duration, Instant};
const AMOUNT: usize = 1_000_000_000;
enum Client { Reader, Writer }
fn client(kind: Client) {
let port = match kind { Client::Reader => 1234, Client::Writer => 1235 };
let mut stream = TcpStream::connect(("192.168.69.1", port)).unwrap();
let mut buffer = vec![0; 1_000_000];
let start = Instant::now();
let mut processed = 0;
while processed < AMOUNT {
let length = cmp::min(buffer.len(), AMOUNT - processed);
let result = match kind {
Client::Reader => stream.read(&mut buffer[..length]),
Client::Writer => stream.write(&buffer[..length]),
};
match result {
Ok(0) => break,
Ok(result) => {
// print!("(P:{})", result);
processed += result
}
Err(err) => panic!("cannot process: {}", err)
}
}
let end = Instant::now();
let elapsed = (end - start).total_millis() as f64 / 1000.0;
println!("throughput: {:.3} Gbps", AMOUNT as f64 / elapsed / 0.125e9);
CLIENT_DONE.store(true, Ordering::SeqCst);
}
static CLIENT_DONE: AtomicBool = AtomicBool::new(false);
fn main() {
#[cfg(feature = "log")]
utils::setup_logging("info");
let (mut opts, mut free) = utils::create_options();
utils::add_tuntap_options(&mut opts, &mut free);
utils::add_middleware_options(&mut opts, &mut free);
free.push("MODE");
let mut matches = utils::parse_options(&opts, free);
let device = utils::parse_tuntap_options(&mut matches);
let fd = device.as_raw_fd();
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
let mode = match matches.free[0].as_ref() {
"reader" => Client::Reader,
"writer" => Client::Writer,
_ => panic!("invalid mode")
};
thread::spawn(move || client(mode));
let neighbor_cache = NeighborCache::new(BTreeMap::new());
let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
let medium = device.capabilities().medium;
let mut builder = InterfaceBuilder::new(device)
.ip_addrs(ip_addrs);
if medium == Medium::Ethernet {
builder = builder
.ethernet_addr(ethernet_addr)
.neighbor_cache(neighbor_cache);
}
let mut iface = builder.finalize();
let mut sockets = SocketSet::new(vec![]);
let tcp1_handle = sockets.add(tcp1_socket);
let tcp2_handle = sockets.add(tcp2_socket);
let default_timeout = Some(Duration::from_millis(1000));
let mut processed = 0;
while !CLIENT_DONE.load(Ordering::SeqCst) {
let timestamp = Instant::now();
match iface.poll(&mut sockets, timestamp) {
Ok(_) => {},
Err(e) => {
debug!("poll error: {}",e);
}
}
// tcp:1234: emit data
{
let mut socket = sockets.get::<TcpSocket>(tcp1_handle);
if !socket.is_open() {
socket.listen(1234).unwrap();
}
if socket.can_send() {
if processed < AMOUNT {
let length = socket.send(|buffer| {
let length = cmp::min(buffer.len(), AMOUNT - processed);
(length, length)
}).unwrap();
processed += length;
}
}
}
// tcp:1235: sink data
{
let mut socket = sockets.get::<TcpSocket>(tcp2_handle);
if !socket.is_open() {
socket.listen(1235).unwrap();
}
if socket.can_recv() {
if processed < AMOUNT {
let length = socket.recv(|buffer| {
let length = cmp::min(buffer.len(), AMOUNT - processed);
(length, length)
}).unwrap();
processed += length;
}
}
}
match iface.poll_at(&sockets, timestamp) {
Some(poll_at) if timestamp < poll_at => {
phy_wait(fd, Some(poll_at - timestamp)).expect("wait error");
},
Some(_) => (),
None => {
phy_wait(fd, default_timeout).expect("wait error");
}
}
}
}