benchmark.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, SocketSet};
  12. use smoltcp::phy::{wait as phy_wait, Device, Medium};
  13. use smoltcp::socket::tcp;
  14. use smoltcp::time::{Duration, Instant};
  15. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
  16. const AMOUNT: usize = 1_000_000_000;
  17. enum Client {
  18. Reader,
  19. Writer,
  20. }
  21. fn client(kind: Client) {
  22. let port = match kind {
  23. Client::Reader => 1234,
  24. Client::Writer => 1235,
  25. };
  26. let mut stream = TcpStream::connect(("192.168.69.1", port)).unwrap();
  27. let mut buffer = vec![0; 1_000_000];
  28. let start = Instant::now();
  29. let mut processed = 0;
  30. while processed < AMOUNT {
  31. let length = cmp::min(buffer.len(), AMOUNT - processed);
  32. let result = match kind {
  33. Client::Reader => stream.read(&mut buffer[..length]),
  34. Client::Writer => stream.write(&buffer[..length]),
  35. };
  36. match result {
  37. Ok(0) => break,
  38. Ok(result) => {
  39. // print!("(P:{})", result);
  40. processed += result
  41. }
  42. Err(err) => panic!("cannot process: {}", err),
  43. }
  44. }
  45. let end = Instant::now();
  46. let elapsed = (end - start).total_millis() as f64 / 1000.0;
  47. println!("throughput: {:.3} Gbps", AMOUNT as f64 / elapsed / 0.125e9);
  48. CLIENT_DONE.store(true, Ordering::SeqCst);
  49. }
  50. static CLIENT_DONE: AtomicBool = AtomicBool::new(false);
  51. fn main() {
  52. #[cfg(feature = "log")]
  53. utils::setup_logging("info");
  54. let (mut opts, mut free) = utils::create_options();
  55. utils::add_tuntap_options(&mut opts, &mut free);
  56. utils::add_middleware_options(&mut opts, &mut free);
  57. free.push("MODE");
  58. let mut matches = utils::parse_options(&opts, free);
  59. let device = utils::parse_tuntap_options(&mut matches);
  60. let fd = device.as_raw_fd();
  61. let mut device =
  62. 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 = tcp::SocketBuffer::new(vec![0; 65535]);
  71. let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  72. let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  73. let tcp2_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  74. let tcp2_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
  75. let tcp2_socket = tcp::Socket::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().ip_addrs(ip_addrs);
  80. if medium == Medium::Ethernet {
  81. builder = builder
  82. .hardware_addr(ethernet_addr.into())
  83. .neighbor_cache(neighbor_cache);
  84. }
  85. let mut iface = builder.finalize(&mut device);
  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(timestamp, &mut device, &mut sockets) {
  94. Ok(_) => {}
  95. Err(e) => {
  96. debug!("poll error: {}", e);
  97. }
  98. }
  99. // tcp:1234: emit data
  100. let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
  101. if !socket.is_open() {
  102. socket.listen(1234).unwrap();
  103. }
  104. if socket.can_send() {
  105. if processed < AMOUNT {
  106. let length = socket
  107. .send(|buffer| {
  108. let length = cmp::min(buffer.len(), AMOUNT - processed);
  109. (length, length)
  110. })
  111. .unwrap();
  112. processed += length;
  113. }
  114. }
  115. // tcp:1235: sink data
  116. let socket = sockets.get_mut::<tcp::Socket>(tcp2_handle);
  117. if !socket.is_open() {
  118. socket.listen(1235).unwrap();
  119. }
  120. if socket.can_recv() {
  121. if processed < AMOUNT {
  122. let length = socket
  123. .recv(|buffer| {
  124. let length = cmp::min(buffer.len(), AMOUNT - processed);
  125. (length, length)
  126. })
  127. .unwrap();
  128. processed += length;
  129. }
  130. }
  131. match iface.poll_at(timestamp, &sockets) {
  132. Some(poll_at) if timestamp < poll_at => {
  133. phy_wait(fd, Some(poll_at - timestamp)).expect("wait error");
  134. }
  135. Some(_) => (),
  136. None => {
  137. phy_wait(fd, default_timeout).expect("wait error");
  138. }
  139. }
  140. }
  141. }