gfcp_fec_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright © 2021 Jeffrey H. Johnson <trnsz@pobox.com>.
  2. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  3. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  4. // Copyright © 2021 Gridfinity, LLC. <admin@gridfinity.com>.
  5. //
  6. // All rights reserved.
  7. //
  8. // All use of this code is governed by the MIT license.
  9. // The complete license is available in the LICENSE file.
  10. package gfcp_test
  11. import (
  12. "encoding/binary"
  13. "math/rand"
  14. "testing"
  15. "github.com/johnsonjh/gfcp"
  16. )
  17. func BenchmarkFECDecode1500(
  18. b *testing.B,
  19. ) {
  20. const (
  21. dataSize = 10
  22. paritySize = 3
  23. payLoad = 1500
  24. )
  25. decoder := gfcp.NewFECDecoder(
  26. 1024,
  27. dataSize,
  28. paritySize,
  29. )
  30. b.ReportAllocs()
  31. b.SetBytes(
  32. payLoad,
  33. )
  34. for i := 0; i < b.N; i++ {
  35. if rand.Int()%(dataSize+paritySize) == 0 {
  36. continue
  37. }
  38. pkt := make(
  39. []byte,
  40. payLoad,
  41. )
  42. binary.LittleEndian.PutUint32(
  43. pkt,
  44. uint32(i),
  45. )
  46. if i%(dataSize+paritySize) >= dataSize {
  47. binary.LittleEndian.PutUint16(
  48. pkt[4:],
  49. gfcp.KTypeParity,
  50. )
  51. } else {
  52. binary.LittleEndian.PutUint16(
  53. pkt[4:],
  54. gfcp.KTypeData,
  55. )
  56. }
  57. decoder.Decode(
  58. pkt,
  59. )
  60. }
  61. }
  62. func BenchmarkFECEncode1500(
  63. b *testing.B,
  64. ) {
  65. const dataSize = 10
  66. const paritySize = 3
  67. const payLoad = 1500
  68. b.ReportAllocs()
  69. b.SetBytes(
  70. payLoad,
  71. )
  72. Encoder := gfcp.NewFECEncoder(
  73. dataSize,
  74. paritySize,
  75. 0,
  76. )
  77. for i := 0; i < b.N; i++ {
  78. data := make(
  79. []byte,
  80. payLoad,
  81. )
  82. Encoder.Encode(
  83. data,
  84. )
  85. }
  86. }
  87. func BenchmarkFECDecode9000(
  88. b *testing.B,
  89. ) {
  90. const (
  91. dataSize = 10
  92. paritySize = 3
  93. payLoad = 9000
  94. )
  95. decoder := gfcp.NewFECDecoder(
  96. 1024,
  97. dataSize,
  98. paritySize,
  99. )
  100. b.ReportAllocs()
  101. b.SetBytes(
  102. payLoad,
  103. )
  104. for i := 0; i < b.N; i++ {
  105. if rand.Int()%(dataSize+paritySize) == 0 {
  106. continue
  107. }
  108. pkt := make(
  109. []byte,
  110. payLoad,
  111. )
  112. binary.LittleEndian.PutUint32(
  113. pkt,
  114. uint32(i),
  115. )
  116. if i%(dataSize+paritySize) >= dataSize {
  117. binary.LittleEndian.PutUint16(
  118. pkt[4:],
  119. gfcp.KTypeParity,
  120. )
  121. } else {
  122. binary.LittleEndian.PutUint16(
  123. pkt[4:],
  124. gfcp.KTypeData,
  125. )
  126. }
  127. decoder.Decode(
  128. pkt,
  129. )
  130. }
  131. }
  132. func BenchmarkFECEncode9000(
  133. b *testing.B,
  134. ) {
  135. const dataSize = 10
  136. const paritySize = 3
  137. const payLoad = 9000
  138. b.ReportAllocs()
  139. b.SetBytes(
  140. payLoad,
  141. )
  142. Encoder := gfcp.NewFECEncoder(
  143. dataSize,
  144. paritySize,
  145. 0,
  146. )
  147. for i := 0; i < b.N; i++ {
  148. data := make(
  149. []byte,
  150. payLoad,
  151. )
  152. Encoder.Encode(
  153. data,
  154. )
  155. }
  156. }