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.

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