gfcp_fec_test.go 2.4 KB

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