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.

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