bench.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #![feature(test)]
  2. mod wire {
  3. use smoltcp::phy::ChecksumCapabilities;
  4. use smoltcp::wire::{IpAddress, IpProtocol};
  5. #[cfg(feature = "proto-ipv4")]
  6. use smoltcp::wire::{Ipv4Address, Ipv4Packet, Ipv4Repr};
  7. #[cfg(feature = "proto-ipv6")]
  8. use smoltcp::wire::{Ipv6Address, Ipv6Packet, Ipv6Repr};
  9. use smoltcp::wire::{TcpControl, TcpPacket, TcpRepr, TcpSeqNumber};
  10. use smoltcp::wire::{UdpPacket, UdpRepr};
  11. extern crate test;
  12. #[cfg(feature = "proto-ipv6")]
  13. const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
  14. 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  15. ]));
  16. #[cfg(feature = "proto-ipv6")]
  17. const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
  18. 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
  19. ]));
  20. #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
  21. const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 1]));
  22. #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
  23. const DST_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 2]));
  24. #[bench]
  25. #[cfg(any(feature = "proto-ipv6", feature = "proto-ipv4"))]
  26. fn bench_emit_tcp(b: &mut test::Bencher) {
  27. static PAYLOAD_BYTES: [u8; 400] = [0x2a; 400];
  28. let repr = TcpRepr {
  29. src_port: 48896,
  30. dst_port: 80,
  31. control: TcpControl::Syn,
  32. seq_number: TcpSeqNumber(0x01234567),
  33. ack_number: None,
  34. window_len: 0x0123,
  35. window_scale: None,
  36. max_seg_size: None,
  37. sack_permitted: false,
  38. sack_ranges: [None, None, None],
  39. payload: &PAYLOAD_BYTES,
  40. timestamp: None,
  41. };
  42. let mut bytes = vec![0xa5; repr.buffer_len()];
  43. b.iter(|| {
  44. let mut packet = TcpPacket::new_unchecked(&mut bytes);
  45. repr.emit(
  46. &mut packet,
  47. &SRC_ADDR,
  48. &DST_ADDR,
  49. &ChecksumCapabilities::default(),
  50. );
  51. });
  52. }
  53. #[bench]
  54. #[cfg(any(feature = "proto-ipv6", feature = "proto-ipv4"))]
  55. fn bench_emit_udp(b: &mut test::Bencher) {
  56. static PAYLOAD_BYTES: [u8; 400] = [0x2a; 400];
  57. let repr = UdpRepr {
  58. src_port: 48896,
  59. dst_port: 80,
  60. };
  61. let mut bytes = vec![0xa5; repr.header_len() + PAYLOAD_BYTES.len()];
  62. b.iter(|| {
  63. let mut packet = UdpPacket::new_unchecked(&mut bytes);
  64. repr.emit(
  65. &mut packet,
  66. &SRC_ADDR,
  67. &DST_ADDR,
  68. PAYLOAD_BYTES.len(),
  69. |buf| buf.copy_from_slice(&PAYLOAD_BYTES),
  70. &ChecksumCapabilities::default(),
  71. );
  72. });
  73. }
  74. #[bench]
  75. #[cfg(feature = "proto-ipv4")]
  76. fn bench_emit_ipv4(b: &mut test::Bencher) {
  77. let repr = Ipv4Repr {
  78. src_addr: Ipv4Address([192, 168, 1, 1]),
  79. dst_addr: Ipv4Address([192, 168, 1, 2]),
  80. next_header: IpProtocol::Tcp,
  81. payload_len: 100,
  82. hop_limit: 64,
  83. };
  84. let mut bytes = vec![0xa5; repr.buffer_len()];
  85. b.iter(|| {
  86. let mut packet = Ipv4Packet::new_unchecked(&mut bytes);
  87. repr.emit(&mut packet, &ChecksumCapabilities::default());
  88. });
  89. }
  90. #[bench]
  91. #[cfg(feature = "proto-ipv6")]
  92. fn bench_emit_ipv6(b: &mut test::Bencher) {
  93. let repr = Ipv6Repr {
  94. src_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
  95. dst_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]),
  96. next_header: IpProtocol::Tcp,
  97. payload_len: 100,
  98. hop_limit: 64,
  99. };
  100. let mut bytes = vec![0xa5; repr.buffer_len()];
  101. b.iter(|| {
  102. let mut packet = Ipv6Packet::new_unchecked(&mut bytes);
  103. repr.emit(&mut packet);
  104. });
  105. }
  106. }