benchmark.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #[cfg(feature = "log")]
  2. #[macro_use]
  3. extern crate log;
  4. #[cfg(feature = "log")]
  5. extern crate env_logger;
  6. extern crate getopts;
  7. extern crate smoltcp;
  8. mod utils;
  9. use std::cmp;
  10. use std::collections::BTreeMap;
  11. use std::sync::atomic::{Ordering, AtomicBool, ATOMIC_BOOL_INIT};
  12. use std::thread;
  13. use std::io::{Read, Write};
  14. use std::net::TcpStream;
  15. use std::os::unix::io::AsRawFd;
  16. use smoltcp::phy::wait as phy_wait;
  17. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
  18. use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
  19. use smoltcp::socket::SocketSet;
  20. use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
  21. use smoltcp::time::{Duration, Instant};
  22. const AMOUNT: usize = 1_000_000_000;
  23. enum Client { Reader, Writer }
  24. fn client(kind: Client) {
  25. let port = match kind { Client::Reader => 1234, Client::Writer => 1235 };
  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 = ATOMIC_BOOL_INIT;
  51. fn main() {
  52. #[cfg(feature = "log")]
  53. utils::setup_logging("info");
  54. let (mut opts, mut free) = utils::create_options();
  55. utils::add_tap_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_tap_options(&mut matches);
  60. let fd = device.as_raw_fd();
  61. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  62. let mode = match matches.free[0].as_ref() {
  63. "reader" => Client::Reader,
  64. "writer" => Client::Writer,
  65. _ => panic!("invalid mode")
  66. };
  67. thread::spawn(move || client(mode));
  68. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  69. let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  70. let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  71. let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  72. let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  73. let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  74. let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
  75. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
  76. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
  77. let mut iface = EthernetInterfaceBuilder::new(device)
  78. .ethernet_addr(ethernet_addr)
  79. .neighbor_cache(neighbor_cache)
  80. .ip_addrs(ip_addrs)
  81. .finalize();
  82. let mut sockets = SocketSet::new(vec![]);
  83. let tcp1_handle = sockets.add(tcp1_socket);
  84. let tcp2_handle = sockets.add(tcp2_socket);
  85. let default_timeout = Some(Duration::from_millis(1000));
  86. let mut processed = 0;
  87. while !CLIENT_DONE.load(Ordering::SeqCst) {
  88. let timestamp = Instant::now();
  89. iface.poll(&mut sockets, timestamp).expect("poll error");
  90. // tcp:1234: emit data
  91. {
  92. let mut socket = sockets.get::<TcpSocket>(tcp1_handle);
  93. if !socket.is_open() {
  94. socket.listen(1234).unwrap();
  95. }
  96. if socket.can_send() {
  97. if processed < AMOUNT {
  98. let length = socket.send(|buffer| {
  99. let length = cmp::min(buffer.len(), AMOUNT - processed);
  100. (length, length)
  101. }).unwrap();
  102. processed += length;
  103. }
  104. }
  105. }
  106. // tcp:1235: sink data
  107. {
  108. let mut socket = sockets.get::<TcpSocket>(tcp2_handle);
  109. if !socket.is_open() {
  110. socket.listen(1235).unwrap();
  111. }
  112. if socket.can_recv() {
  113. if processed < AMOUNT {
  114. let length = socket.recv(|buffer| {
  115. let length = cmp::min(buffer.len(), AMOUNT - processed);
  116. (length, length)
  117. }).unwrap();
  118. processed += length;
  119. }
  120. }
  121. }
  122. match iface.poll_at(&sockets, timestamp) {
  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. }