bench.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. };
  41. let mut bytes = vec![0xa5; repr.buffer_len()];
  42. b.iter(|| {
  43. let mut packet = TcpPacket::new_unchecked(&mut bytes);
  44. repr.emit(
  45. &mut packet,
  46. &SRC_ADDR,
  47. &DST_ADDR,
  48. &ChecksumCapabilities::default(),
  49. );
  50. });
  51. }
  52. #[bench]
  53. #[cfg(any(feature = "proto-ipv6", feature = "proto-ipv4"))]
  54. fn bench_emit_udp(b: &mut test::Bencher) {
  55. static PAYLOAD_BYTES: [u8; 400] = [0x2a; 400];
  56. let repr = UdpRepr {
  57. src_port: 48896,
  58. dst_port: 80,
  59. };
  60. let mut bytes = vec![0xa5; repr.header_len() + PAYLOAD_BYTES.len()];
  61. b.iter(|| {
  62. let mut packet = UdpPacket::new_unchecked(&mut bytes);
  63. repr.emit(
  64. &mut packet,
  65. &SRC_ADDR,
  66. &DST_ADDR,
  67. PAYLOAD_BYTES.len(),
  68. |buf| buf.copy_from_slice(&PAYLOAD_BYTES),
  69. &ChecksumCapabilities::default(),
  70. );
  71. });
  72. }
  73. #[bench]
  74. #[cfg(feature = "proto-ipv4")]
  75. fn bench_emit_ipv4(b: &mut test::Bencher) {
  76. let repr = Ipv4Repr {
  77. src_addr: Ipv4Address([192, 168, 1, 1]),
  78. dst_addr: Ipv4Address([192, 168, 1, 2]),
  79. protocol: IpProtocol::Tcp,
  80. payload_len: 100,
  81. hop_limit: 64,
  82. };
  83. let mut bytes = vec![0xa5; repr.buffer_len()];
  84. b.iter(|| {
  85. let mut packet = Ipv4Packet::new_unchecked(&mut bytes);
  86. repr.emit(&mut packet, &ChecksumCapabilities::default());
  87. });
  88. }
  89. #[bench]
  90. #[cfg(feature = "proto-ipv6")]
  91. fn bench_emit_ipv6(b: &mut test::Bencher) {
  92. let repr = Ipv6Repr {
  93. src_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
  94. dst_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]),
  95. next_header: IpProtocol::Tcp,
  96. payload_len: 100,
  97. hop_limit: 64,
  98. };
  99. let mut bytes = vec![0xa5; repr.buffer_len()];
  100. b.iter(|| {
  101. let mut packet = Ipv6Packet::new_unchecked(&mut bytes);
  102. repr.emit(&mut packet);
  103. });
  104. }
  105. }