benchmark.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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::time::Instant;
  13. use std::thread;
  14. use std::io::{Read, Write};
  15. use std::net::TcpStream;
  16. use std::os::unix::io::AsRawFd;
  17. use smoltcp::phy::wait as phy_wait;
  18. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
  19. use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
  20. use smoltcp::socket::SocketSet;
  21. use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
  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;
  47. let elapsed = elapsed.as_secs() as f64
  48. + elapsed.subsec_nanos() as f64 * 1e-9;
  49. println!("throughput: {:.3} Gbps", AMOUNT as f64 / elapsed / 0.125e9);
  50. CLIENT_DONE.store(true, Ordering::SeqCst);
  51. }
  52. static CLIENT_DONE: AtomicBool = ATOMIC_BOOL_INIT;
  53. fn main() {
  54. #[cfg(feature = "log")]
  55. utils::setup_logging("info");
  56. let (mut opts, mut free) = utils::create_options();
  57. utils::add_tap_options(&mut opts, &mut free);
  58. utils::add_middleware_options(&mut opts, &mut free);
  59. free.push("MODE");
  60. let mut matches = utils::parse_options(&opts, free);
  61. let device = utils::parse_tap_options(&mut matches);
  62. let fd = device.as_raw_fd();
  63. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  64. let mode = match matches.free[0].as_ref() {
  65. "reader" => Client::Reader,
  66. "writer" => Client::Writer,
  67. _ => panic!("invalid mode")
  68. };
  69. thread::spawn(move || client(mode));
  70. let startup_time = Instant::now();
  71. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  72. let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  73. let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  74. let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  75. let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  76. let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  77. let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
  78. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
  79. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
  80. let mut iface = EthernetInterfaceBuilder::new(device)
  81. .ethernet_addr(ethernet_addr)
  82. .neighbor_cache(neighbor_cache)
  83. .ip_addrs(ip_addrs)
  84. .finalize();
  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 mut processed = 0;
  89. while !CLIENT_DONE.load(Ordering::SeqCst) {
  90. let timestamp = utils::millis_since(startup_time);
  91. iface.poll(&mut sockets, timestamp).expect("poll error");
  92. // tcp:1234: emit data
  93. {
  94. let mut socket = sockets.get::<TcpSocket>(tcp1_handle);
  95. if !socket.is_open() {
  96. socket.listen(1234).unwrap();
  97. }
  98. if socket.can_send() {
  99. if processed < AMOUNT {
  100. let length = socket.send(|buffer| {
  101. let length = cmp::min(buffer.len(), AMOUNT - processed);
  102. (length, length)
  103. }).unwrap();
  104. processed += length;
  105. }
  106. }
  107. }
  108. // tcp:1235: sink data
  109. {
  110. let mut socket = sockets.get::<TcpSocket>(tcp2_handle);
  111. if !socket.is_open() {
  112. socket.listen(1235).unwrap();
  113. }
  114. if socket.can_recv() {
  115. if processed < AMOUNT {
  116. let length = socket.recv(|buffer| {
  117. let length = cmp::min(buffer.len(), AMOUNT - processed);
  118. (length, length)
  119. }).unwrap();
  120. processed += length;
  121. }
  122. }
  123. }
  124. phy_wait(fd, iface.poll_delay(&sockets, timestamp).or(Some(1000))).expect("wait error");
  125. }
  126. }