benchmark.rs 4.9 KB

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