loopback_benchmark.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. mod utils;
  2. use log::debug;
  3. use smoltcp::iface::{Config, Interface, SocketSet};
  4. use smoltcp::phy::{Device, Loopback, Medium};
  5. use smoltcp::socket::tcp;
  6. use smoltcp::time::Instant;
  7. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
  8. fn main() {
  9. let device = Loopback::new(Medium::Ethernet);
  10. let mut device = {
  11. utils::setup_logging("info");
  12. let (mut opts, mut free) = utils::create_options();
  13. utils::add_middleware_options(&mut opts, &mut free);
  14. let mut matches = utils::parse_options(&opts, free);
  15. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ true)
  16. };
  17. // Create interface
  18. let config = match device.capabilities().medium {
  19. Medium::Ethernet => {
  20. Config::new(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into())
  21. }
  22. Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
  23. Medium::Ieee802154 => todo!(),
  24. };
  25. let mut iface = Interface::new(config, &mut device, Instant::now());
  26. iface.update_ip_addrs(|ip_addrs| {
  27. ip_addrs
  28. .push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
  29. .unwrap();
  30. });
  31. // Create sockets
  32. let server_socket = {
  33. let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 65536]);
  34. let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 65536]);
  35. tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer)
  36. };
  37. let client_socket = {
  38. let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 65536]);
  39. let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 65536]);
  40. tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer)
  41. };
  42. let mut sockets: [_; 2] = Default::default();
  43. let mut sockets = SocketSet::new(&mut sockets[..]);
  44. let server_handle = sockets.add(server_socket);
  45. let client_handle = sockets.add(client_socket);
  46. let start_time = Instant::now();
  47. let mut did_listen = false;
  48. let mut did_connect = false;
  49. let mut processed = 0;
  50. while processed < 1024 * 1024 * 1024 {
  51. iface.poll(Instant::now(), &mut device, &mut sockets);
  52. let socket = sockets.get_mut::<tcp::Socket>(server_handle);
  53. if !socket.is_active() && !socket.is_listening() && !did_listen {
  54. debug!("listening");
  55. socket.listen(1234).unwrap();
  56. did_listen = true;
  57. }
  58. while socket.can_recv() {
  59. let received = socket.recv(|buffer| (buffer.len(), buffer.len())).unwrap();
  60. debug!("got {:?}", received,);
  61. processed += received;
  62. }
  63. let socket = sockets.get_mut::<tcp::Socket>(client_handle);
  64. let cx = iface.context();
  65. if !socket.is_open() && !did_connect {
  66. debug!("connecting");
  67. socket
  68. .connect(cx, (IpAddress::v4(127, 0, 0, 1), 1234), 65000)
  69. .unwrap();
  70. did_connect = true;
  71. }
  72. while socket.can_send() {
  73. debug!("sending");
  74. socket.send(|buffer| (buffer.len(), ())).unwrap();
  75. }
  76. }
  77. let duration = Instant::now() - start_time;
  78. println!(
  79. "done in {} s, bandwidth is {} Gbps",
  80. duration.total_millis() as f64 / 1000.0,
  81. (processed as u64 * 8 / duration.total_millis()) as f64 / 1000000.0
  82. );
  83. }