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.

168 lines
3.2 KiB

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