bench.rs 3.8 KB

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