You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package consensus
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. // "sync"
  7. "testing"
  8. "time"
  9. "github.com/tendermint/tendermint/consensus/types"
  10. tmtypes "github.com/tendermint/tendermint/types"
  11. tmtime "github.com/tendermint/tendermint/types/time"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func TestWALEncoderDecoder(t *testing.T) {
  16. now := tmtime.Now()
  17. msgs := []TimedWALMessage{
  18. TimedWALMessage{Time: now, Msg: EndHeightMessage{0}},
  19. TimedWALMessage{Time: now, Msg: timeoutInfo{Duration: time.Second, Height: 1, Round: 1, Step: types.RoundStepPropose}},
  20. }
  21. b := new(bytes.Buffer)
  22. for _, msg := range msgs {
  23. b.Reset()
  24. enc := NewWALEncoder(b)
  25. err := enc.Encode(&msg)
  26. require.NoError(t, err)
  27. dec := NewWALDecoder(b)
  28. decoded, err := dec.Decode()
  29. require.NoError(t, err)
  30. assert.Equal(t, msg.Time.UTC(), decoded.Time)
  31. assert.Equal(t, msg.Msg, decoded.Msg)
  32. }
  33. }
  34. func TestWALSearchForEndHeight(t *testing.T) {
  35. walBody, err := WALWithNBlocks(6)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. walFile := tempWALWithData(walBody)
  40. wal, err := NewWAL(walFile)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. h := int64(3)
  45. gr, found, err := wal.SearchForEndHeight(h, &WALSearchOptions{})
  46. assert.NoError(t, err, fmt.Sprintf("expected not to err on height %d", h))
  47. assert.True(t, found, fmt.Sprintf("expected to find end height for %d", h))
  48. assert.NotNil(t, gr, "expected group not to be nil")
  49. defer gr.Close()
  50. dec := NewWALDecoder(gr)
  51. msg, err := dec.Decode()
  52. assert.NoError(t, err, "expected to decode a message")
  53. rs, ok := msg.Msg.(tmtypes.EventDataRoundState)
  54. assert.True(t, ok, "expected message of type EventDataRoundState")
  55. assert.Equal(t, rs.Height, h+1, fmt.Sprintf("wrong height"))
  56. }
  57. /*
  58. var initOnce sync.Once
  59. func registerInterfacesOnce() {
  60. initOnce.Do(func() {
  61. var _ = wire.RegisterInterface(
  62. struct{ WALMessage }{},
  63. wire.ConcreteType{[]byte{}, 0x10},
  64. )
  65. })
  66. }
  67. */
  68. func nBytes(n int) []byte {
  69. buf := make([]byte, n)
  70. n, _ = rand.Read(buf)
  71. return buf[:n]
  72. }
  73. func benchmarkWalDecode(b *testing.B, n int) {
  74. // registerInterfacesOnce()
  75. buf := new(bytes.Buffer)
  76. enc := NewWALEncoder(buf)
  77. data := nBytes(n)
  78. enc.Encode(&TimedWALMessage{Msg: data, Time: time.Now().Round(time.Second).UTC()})
  79. encoded := buf.Bytes()
  80. b.ResetTimer()
  81. for i := 0; i < b.N; i++ {
  82. buf.Reset()
  83. buf.Write(encoded)
  84. dec := NewWALDecoder(buf)
  85. if _, err := dec.Decode(); err != nil {
  86. b.Fatal(err)
  87. }
  88. }
  89. b.ReportAllocs()
  90. }
  91. func BenchmarkWalDecode512B(b *testing.B) {
  92. benchmarkWalDecode(b, 512)
  93. }
  94. func BenchmarkWalDecode10KB(b *testing.B) {
  95. benchmarkWalDecode(b, 10*1024)
  96. }
  97. func BenchmarkWalDecode100KB(b *testing.B) {
  98. benchmarkWalDecode(b, 100*1024)
  99. }
  100. func BenchmarkWalDecode1MB(b *testing.B) {
  101. benchmarkWalDecode(b, 1024*1024)
  102. }
  103. func BenchmarkWalDecode10MB(b *testing.B) {
  104. benchmarkWalDecode(b, 10*1024*1024)
  105. }
  106. func BenchmarkWalDecode100MB(b *testing.B) {
  107. benchmarkWalDecode(b, 100*1024*1024)
  108. }
  109. func BenchmarkWalDecode1GB(b *testing.B) {
  110. benchmarkWalDecode(b, 1024*1024*1024)
  111. }