lkcp9_fec_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  2. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  3. // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
  4. // Copyright © 2020 Jeffrey H. Johnson <jeff@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 lkcp9_test
  11. import (
  12. "encoding/binary"
  13. "math/rand"
  14. "testing"
  15. "go.gridfinity.dev/lkcp9"
  16. )
  17. func BenchmarkFECDecode(
  18. b *testing.B,
  19. ) {
  20. const (
  21. dataSize = 10
  22. paritySize = 3
  23. payLoad = 1500
  24. )
  25. decoder := lkcp9.KcpNewDECDecoder(
  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. lkcp9.KTypeParity,
  50. )
  51. } else {
  52. binary.LittleEndian.PutUint16(
  53. pkt[4:],
  54. lkcp9.KTypeData,
  55. )
  56. }
  57. decoder.Decode(
  58. pkt,
  59. )
  60. }
  61. }
  62. func BenchmarkFECEncode(
  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 := lkcp9.KcpNewDECEncoder(
  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. }