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.

132 lines
3.0 KiB

  1. package consensus
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "sync"
  6. "testing"
  7. "time"
  8. wire "github.com/tendermint/go-wire"
  9. "github.com/tendermint/tendermint/consensus/types"
  10. tmtypes "github.com/tendermint/tendermint/types"
  11. cmn "github.com/tendermint/tmlibs/common"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func TestWALEncoderDecoder(t *testing.T) {
  16. now := time.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.Truncate(time.Millisecond), decoded.Time)
  31. assert.Equal(t, msg.Msg, decoded.Msg)
  32. }
  33. }
  34. func TestSearchForEndHeight(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, false)
  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, cmn.Fmt("expected not to err on height %d", h))
  47. assert.True(t, found, cmn.Fmt("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, cmn.Fmt("wrong height"))
  56. }
  57. var initOnce sync.Once
  58. func registerInterfacesOnce() {
  59. initOnce.Do(func() {
  60. var _ = wire.RegisterInterface(
  61. struct{ WALMessage }{},
  62. wire.ConcreteType{[]byte{}, 0x10},
  63. )
  64. })
  65. }
  66. func nBytes(n int) []byte {
  67. buf := make([]byte, n)
  68. n, _ = rand.Read(buf)
  69. return buf[:n]
  70. }
  71. func benchmarkWalDecode(b *testing.B, n int) {
  72. registerInterfacesOnce()
  73. buf := new(bytes.Buffer)
  74. enc := NewWALEncoder(buf)
  75. data := nBytes(n)
  76. enc.Encode(&TimedWALMessage{Msg: data, Time: time.Now().Round(time.Second)})
  77. encoded := buf.Bytes()
  78. b.ResetTimer()
  79. for i := 0; i < b.N; i++ {
  80. buf.Reset()
  81. buf.Write(encoded)
  82. dec := NewWALDecoder(buf)
  83. if _, err := dec.Decode(); err != nil {
  84. b.Fatal(err)
  85. }
  86. }
  87. b.ReportAllocs()
  88. }
  89. func BenchmarkWalDecode512B(b *testing.B) {
  90. benchmarkWalDecode(b, 512)
  91. }
  92. func BenchmarkWalDecode10KB(b *testing.B) {
  93. benchmarkWalDecode(b, 10*1024)
  94. }
  95. func BenchmarkWalDecode100KB(b *testing.B) {
  96. benchmarkWalDecode(b, 100*1024)
  97. }
  98. func BenchmarkWalDecode1MB(b *testing.B) {
  99. benchmarkWalDecode(b, 1024*1024)
  100. }
  101. func BenchmarkWalDecode10MB(b *testing.B) {
  102. benchmarkWalDecode(b, 10*1024*1024)
  103. }
  104. func BenchmarkWalDecode100MB(b *testing.B) {
  105. benchmarkWalDecode(b, 100*1024*1024)
  106. }
  107. func BenchmarkWalDecode1GB(b *testing.B) {
  108. benchmarkWalDecode(b, 1024*1024*1024)
  109. }