stress.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = 10_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; 64];
  28. let mut processed = 0;
  29. while processed < AMOUNT {
  30. let length = cmp::min(buffer.len(), AMOUNT - processed);
  31. let result = match kind {
  32. Client::Reader => stream.read(&mut buffer[..length]),
  33. Client::Writer => stream.write(&buffer[..length]),
  34. };
  35. match result {
  36. Ok(0) => break,
  37. Ok(result) => {
  38. print!("(P:{})", result);
  39. processed += result
  40. }
  41. Err(err) => panic!("cannot process: {}", err)
  42. }
  43. }
  44. println!("client done");
  45. CLIENT_DONE.store(true, Ordering::SeqCst);
  46. }
  47. static CLIENT_DONE: AtomicBool = ATOMIC_BOOL_INIT;
  48. fn main() {
  49. let (mut opts, mut free) = utils::create_options();
  50. utils::add_tap_options(&mut opts, &mut free);
  51. utils::add_middleware_options(&mut opts, &mut free);
  52. free.push("MODE");
  53. let mut matches = utils::parse_options(&opts, free);
  54. let device = utils::parse_tap_options(&mut matches);
  55. let fd = device.as_raw_fd();
  56. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  57. let mode = match matches.free[0].as_ref() {
  58. "reader" => Client::Reader,
  59. "writer" => Client::Writer,
  60. _ => panic!("invalid mode")
  61. };
  62. thread::spawn(move || client(mode));
  63. let startup_time = Instant::now();
  64. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  65. let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  66. let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  67. let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
  68. let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  69. let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
  70. let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
  71. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
  72. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
  73. let mut iface = EthernetInterfaceBuilder::new(device)
  74. .ethernet_addr(ethernet_addr)
  75. .neighbor_cache(neighbor_cache)
  76. .ip_addrs(ip_addrs)
  77. .finalize();
  78. let mut sockets = SocketSet::new(vec![]);
  79. let tcp1_handle = sockets.add(tcp1_socket);
  80. let tcp2_handle = sockets.add(tcp2_socket);
  81. let mut processed = 0;
  82. while !CLIENT_DONE.load(Ordering::SeqCst) {
  83. // tcp:1234: emit data
  84. {
  85. let mut socket = sockets.get::<TcpSocket>(tcp1_handle);
  86. if !socket.is_open() {
  87. socket.listen(1234).unwrap();
  88. }
  89. if socket.can_send() {
  90. if processed < AMOUNT {
  91. let length = socket.send(|buffer| {
  92. let length = cmp::min(buffer.len(), AMOUNT - processed);
  93. (length, length)
  94. }).unwrap();
  95. processed += length;
  96. }
  97. }
  98. }
  99. // tcp:1235: sink data
  100. {
  101. let mut socket = sockets.get::<TcpSocket>(tcp2_handle);
  102. if !socket.is_open() {
  103. socket.listen(1235).unwrap();
  104. }
  105. if socket.can_recv() {
  106. if processed < AMOUNT {
  107. let length = socket.recv(|buffer| {
  108. let length = cmp::min(buffer.len(), AMOUNT - processed);
  109. (length, length)
  110. }).unwrap();
  111. processed += length;
  112. }
  113. }
  114. }
  115. let timestamp = utils::millis_since(startup_time);
  116. let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
  117. phy_wait(fd, poll_at.map(|at| at.saturating_sub(timestamp))).expect("wait error");
  118. }
  119. }