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.

127 lines
2.9 KiB

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