benchmark.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #![allow(clippy::collapsible_if)]
  2. mod utils;
  3. use log::debug;
  4. use std::cmp;
  5. use std::collections::BTreeMap;
  6. use std::io::{Read, Write};
  7. use std::net::TcpStream;
  8. use std::os::unix::io::AsRawFd;
  9. use std::sync::atomic::{AtomicBool, Ordering};
  10. use std::thread;
  11. use smoltcp::iface::{InterfaceBuilder, NeighborCache};
  12. use smoltcp::phy::{wait as phy_wait, Device, Medium};
  13. use smoltcp::socket::SocketSet;
  14. use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
  15. use smoltcp::time::{Duration, Instant};
  16. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
  17. const AMOUNT: usize = 1_000_000_000;
  18. enum Client {
  19. Reader,
  20. Writer,
  21. }
  22. fn client(kind: Client) {
  23. let port = match kind {
  24. Client::Reader => 1234,
  25. Client::Writer => 1235,
  26. };
  27. let mut stream = TcpStream::connect(("192.168.69.1", port)).unwrap();
  28. let mut buffer = vec![0; 1_000_000];
  29. let start = Instant::now();
  30. let mut processed = 0;
  31. while processed < AMOUNT {
  32. let length = cmp::min(buffer.len(), AMOUNT - processed);
  33. let result = match kind {
  34. Client::Reader => stream.read(&mut buffer[..length]),
  35. Client::Writer => stream.write(&buffer[..length]),
  36. };
  37. match result {
  38. Ok(0) => break,
  39. Ok(result) => {
  40. // print!("(P:{})", result);
  41. processed += result
  42. }
  43. Err(err) => panic!("cannot process: {}", err),
  44. }
  45. }
  46. let end = Instant::now();
  47. let elapsed = (end - start).total_millis() as f64 / 1000.0;
  48. println!("throughput: {:.3} Gbps", AMOUNT as f64 / elapsed / 0.125e9);
  49. CLIENT_DONE.store(true, Ordering::SeqCst);
  50. }
  51. static CLIENT_DONE: AtomicBool = AtomicBool::new(false);
  52. fn main() {
  53. #[cfg(feature = "log")]
  54. utils::setup_logging("info");
  55. let (mut opts, mut free) = utils::create_options();
  56. utils::add_tuntap_options(&mut opts, &mut free);
  57. utils::add_middleware_options(&mut opts, &mut free);
  58. free.push("MODE");
  59. let mut matches = utils::parse_options(&opts, free);
  60. let device = utils::parse_tuntap_options(&mut matches);
  61. let fd = device.as_raw_fd();
  62. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  63. let mode = match matches.free[0].as_ref() {
  64. "reader" => Client::Reader,
  65. "writer" => Client::Writer,
  66. _ => panic!("invalid mode"),
  67. };
  68. thread::spawn(move || client(mode));
  69. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  70. let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  71. let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  72. let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  73. let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  74. let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  75. let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
  76. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
  77. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
  78. let medium = device.capabilities().medium;
  79. let mut builder = InterfaceBuilder::new(device).ip_addrs(ip_addrs);
  80. if medium == Medium::Ethernet {
  81. builder = builder
  82. .ethernet_addr(ethernet_addr)
  83. .neighbor_cache(neighbor_cache);
  84. }
  85. let mut iface = builder.finalize();
  86. let mut sockets = SocketSet::new(vec![]);
  87. let tcp1_handle = sockets.add(tcp1_socket);
  88. let tcp2_handle = sockets.add(tcp2_socket);
  89. let default_timeout = Some(Duration::from_millis(1000));
  90. let mut processed = 0;
  91. while !CLIENT_DONE.load(Ordering::SeqCst) {
  92. let timestamp = Instant::now();
  93. match iface.poll(&mut sockets, timestamp) {
  94. Ok(_) => {}
  95. Err(e) => {
  96. debug!("poll error: {}", e);
  97. }
  98. }
  99. // tcp:1234: emit data
  100. {
  101. let mut socket = sockets.get::<TcpSocket>(tcp1_handle);
  102. if !socket.is_open() {
  103. socket.listen(1234).unwrap();
  104. }
  105. if socket.can_send() {
  106. if processed < AMOUNT {
  107. let length = socket
  108. .send(|buffer| {
  109. let length = cmp::min(buffer.len(), AMOUNT - processed);
  110. (length, length)
  111. })
  112. .unwrap();
  113. processed += length;
  114. }
  115. }
  116. }
  117. // tcp:1235: sink data
  118. {
  119. let mut socket = sockets.get::<TcpSocket>(tcp2_handle);
  120. if !socket.is_open() {
  121. socket.listen(1235).unwrap();
  122. }
  123. if socket.can_recv() {
  124. if processed < AMOUNT {
  125. let length = socket
  126. .recv(|buffer| {
  127. let length = cmp::min(buffer.len(), AMOUNT - processed);
  128. (length, length)
  129. })
  130. .unwrap();
  131. processed += length;
  132. }
  133. }
  134. }
  135. match iface.poll_at(&sockets, timestamp) {
  136. Some(poll_at) if timestamp < poll_at => {
  137. phy_wait(fd, Some(poll_at - timestamp)).expect("wait error");
  138. }
  139. Some(_) => (),
  140. None => {
  141. phy_wait(fd, default_timeout).expect("wait error");
  142. }
  143. }
  144. }
  145. }