benchmark.rs 4.9 KB

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