bench.rs 3.8 KB

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