bench.rs 3.7 KB

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