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.

166 lines
3.1 KiB

11 years ago
11 years ago
11 years ago
  1. package blocks
  2. import (
  3. "bytes"
  4. "encoding/gob"
  5. "encoding/json"
  6. "testing"
  7. . "github.com/tendermint/tendermint/binary"
  8. "github.com/ugorji/go/codec"
  9. "github.com/vmihailenco/msgpack"
  10. )
  11. func BenchmarkTestCustom(b *testing.B) {
  12. b.StopTimer()
  13. h := &Header{
  14. Name: "Header",
  15. Height: 123,
  16. Fees: 123,
  17. Time: 123,
  18. PrevHash: ByteSlice("prevhash"),
  19. ValidationHash: ByteSlice("validationhash"),
  20. TxsHash: ByteSlice("txshash"),
  21. }
  22. buf := bytes.NewBuffer(nil)
  23. b.StartTimer()
  24. for i := 0; i < b.N; i++ {
  25. buf.Reset()
  26. h.WriteTo(buf)
  27. h2 := ReadHeader(buf)
  28. if h2.Name != "Header" {
  29. b.Fatalf("wrong name")
  30. }
  31. }
  32. }
  33. type HHeader struct {
  34. Name string `json:"N"`
  35. Height uint64 `json:"H"`
  36. Fees uint64 `json:"F"`
  37. Time uint64 `json:"T"`
  38. PrevHash []byte `json:"PH"`
  39. ValidationHash []byte `json:"VH"`
  40. TxsHash []byte `json:"DH"`
  41. }
  42. func BenchmarkTestJSON(b *testing.B) {
  43. b.StopTimer()
  44. h := &HHeader{
  45. Name: "Header",
  46. Height: 123,
  47. Fees: 123,
  48. Time: 123,
  49. PrevHash: []byte("prevhash"),
  50. ValidationHash: []byte("validationhash"),
  51. TxsHash: []byte("txshash"),
  52. }
  53. h2 := &HHeader{}
  54. buf := bytes.NewBuffer(nil)
  55. enc := json.NewEncoder(buf)
  56. dec := json.NewDecoder(buf)
  57. b.StartTimer()
  58. for i := 0; i < b.N; i++ {
  59. buf.Reset()
  60. enc.Encode(h)
  61. dec.Decode(h2)
  62. if h2.Name != "Header" {
  63. b.Fatalf("wrong name")
  64. }
  65. }
  66. }
  67. func BenchmarkTestGob(b *testing.B) {
  68. b.StopTimer()
  69. h := &Header{
  70. Name: "Header",
  71. Height: 123,
  72. Fees: 123,
  73. Time: 123,
  74. PrevHash: []byte("prevhash"),
  75. ValidationHash: []byte("validationhash"),
  76. TxsHash: []byte("txshash"),
  77. }
  78. h2 := &Header{}
  79. buf := bytes.NewBuffer(nil)
  80. enc := gob.NewEncoder(buf)
  81. dec := gob.NewDecoder(buf)
  82. b.StartTimer()
  83. for i := 0; i < b.N; i++ {
  84. buf.Reset()
  85. enc.Encode(h)
  86. dec.Decode(h2)
  87. if h2.Name != "Header" {
  88. b.Fatalf("wrong name")
  89. }
  90. }
  91. }
  92. func BenchmarkTestMsgPack(b *testing.B) {
  93. b.StopTimer()
  94. h := &Header{
  95. Name: "Header",
  96. Height: 123,
  97. Fees: 123,
  98. Time: 123,
  99. PrevHash: []byte("prevhash"),
  100. ValidationHash: []byte("validationhash"),
  101. TxsHash: []byte("txshash"),
  102. }
  103. h2 := &Header{}
  104. buf := bytes.NewBuffer(nil)
  105. enc := msgpack.NewEncoder(buf)
  106. dec := msgpack.NewDecoder(buf)
  107. b.StartTimer()
  108. for i := 0; i < b.N; i++ {
  109. buf.Reset()
  110. enc.Encode(h)
  111. dec.Decode(h2)
  112. if h2.Name != "Header" {
  113. b.Fatalf("wrong name")
  114. }
  115. }
  116. }
  117. func BenchmarkTestMsgPack2(b *testing.B) {
  118. b.StopTimer()
  119. h := &Header{
  120. Name: "Header",
  121. Height: 123,
  122. Fees: 123,
  123. Time: 123,
  124. PrevHash: []byte("prevhash"),
  125. ValidationHash: []byte("validationhash"),
  126. TxsHash: []byte("txshash"),
  127. }
  128. h2 := &Header{}
  129. var mh codec.MsgpackHandle
  130. handle := &mh
  131. buf := bytes.NewBuffer(nil)
  132. enc := codec.NewEncoder(buf, handle)
  133. dec := codec.NewDecoder(buf, handle)
  134. b.StartTimer()
  135. for i := 0; i < b.N; i++ {
  136. buf.Reset()
  137. enc.Encode(h)
  138. dec.Decode(h2)
  139. if h2.Name != "Header" {
  140. b.Fatalf("wrong name")
  141. }
  142. }
  143. }