benchmark.rs 5.0 KB

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