bench_test.go raw

   1  // Copyright (c) 2013-2016 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package wire
   6  
   7  import (
   8  	"bytes"
   9  	"compress/bzip2"
  10  	"fmt"
  11  	"io"
  12  	"net"
  13  	"os"
  14  	"testing"
  15  
  16  	"github.com/btcsuite/btcd/chainhash/v2"
  17  )
  18  
  19  // genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
  20  // the main network, regression test network, and test network (version 3).
  21  var genesisCoinbaseTx = MsgTx{
  22  	Version: 1,
  23  	TxIn: []*TxIn{
  24  		{
  25  			PreviousOutPoint: OutPoint{
  26  				Hash:  chainhash.Hash{},
  27  				Index: 0xffffffff,
  28  			},
  29  			SignatureScript: []byte{
  30  				0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x45, /* |.......E| */
  31  				0x54, 0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, /* |The Time| */
  32  				0x73, 0x20, 0x30, 0x33, 0x2f, 0x4a, 0x61, 0x6e, /* |s 03/Jan| */
  33  				0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, /* |/2009 Ch| */
  34  				0x61, 0x6e, 0x63, 0x65, 0x6c, 0x6c, 0x6f, 0x72, /* |ancellor| */
  35  				0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, /* | on brin| */
  36  				0x6b, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x65, 0x63, /* |k of sec|*/
  37  				0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, /* |ond bail| */
  38  				0x6f, 0x75, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, /* |out for |*/
  39  				0x62, 0x61, 0x6e, 0x6b, 0x73, /* |banks| */
  40  			},
  41  			Sequence: 0xffffffff,
  42  		},
  43  	},
  44  	TxOut: []*TxOut{
  45  		{
  46  			Value: 0x12a05f200,
  47  			PkScript: []byte{
  48  				0x41, 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, /* |A.g....U| */
  49  				0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, /* |H'.g..q0| */
  50  				0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, /* |..\..(.9| */
  51  				0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, /* |..yb...a| */
  52  				0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, /* |..I..?L.| */
  53  				0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, /* |8..U....| */
  54  				0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, /* |..\8M...| */
  55  				0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, /* |.W.Lp+k.| */
  56  				0x1d, 0x5f, 0xac, /* |._.| */
  57  			},
  58  		},
  59  	},
  60  	LockTime: 0,
  61  }
  62  
  63  // BenchmarkWriteVarInt1 performs a benchmark on how long it takes to write
  64  // a single byte variable length integer.
  65  func BenchmarkWriteVarInt1(b *testing.B) {
  66  	b.ReportAllocs()
  67  
  68  	for i := 0; i < b.N; i++ {
  69  		WriteVarInt(io.Discard, 0, 1)
  70  	}
  71  }
  72  
  73  // BenchmarkWriteVarInt3 performs a benchmark on how long it takes to write
  74  // a three byte variable length integer.
  75  func BenchmarkWriteVarInt3(b *testing.B) {
  76  	b.ReportAllocs()
  77  
  78  	for i := 0; i < b.N; i++ {
  79  		WriteVarInt(io.Discard, 0, 65535)
  80  	}
  81  }
  82  
  83  // BenchmarkWriteVarInt5 performs a benchmark on how long it takes to write
  84  // a five byte variable length integer.
  85  func BenchmarkWriteVarInt5(b *testing.B) {
  86  	b.ReportAllocs()
  87  
  88  	for i := 0; i < b.N; i++ {
  89  		WriteVarInt(io.Discard, 0, 4294967295)
  90  	}
  91  }
  92  
  93  // BenchmarkWriteVarInt9 performs a benchmark on how long it takes to write
  94  // a nine byte variable length integer.
  95  func BenchmarkWriteVarInt9(b *testing.B) {
  96  	b.ReportAllocs()
  97  
  98  	for i := 0; i < b.N; i++ {
  99  		WriteVarInt(io.Discard, 0, 18446744073709551615)
 100  	}
 101  }
 102  
 103  // BenchmarkReadVarInt1 performs a benchmark on how long it takes to read
 104  // a single byte variable length integer.
 105  func BenchmarkReadVarInt1(b *testing.B) {
 106  	b.ReportAllocs()
 107  
 108  	buf := []byte{0x01}
 109  	r := bytes.NewReader(buf)
 110  	for i := 0; i < b.N; i++ {
 111  		r.Seek(0, 0)
 112  		ReadVarInt(r, 0)
 113  	}
 114  }
 115  
 116  // BenchmarkReadVarInt3 performs a benchmark on how long it takes to read
 117  // a three byte variable length integer.
 118  func BenchmarkReadVarInt3(b *testing.B) {
 119  	b.ReportAllocs()
 120  
 121  	buf := []byte{0x0fd, 0xff, 0xff}
 122  	r := bytes.NewReader(buf)
 123  	for i := 0; i < b.N; i++ {
 124  		r.Seek(0, 0)
 125  		ReadVarInt(r, 0)
 126  	}
 127  }
 128  
 129  // BenchmarkReadVarInt5 performs a benchmark on how long it takes to read
 130  // a five byte variable length integer.
 131  func BenchmarkReadVarInt5(b *testing.B) {
 132  	b.ReportAllocs()
 133  
 134  	buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff}
 135  	r := bytes.NewReader(buf)
 136  	for i := 0; i < b.N; i++ {
 137  		r.Seek(0, 0)
 138  		ReadVarInt(r, 0)
 139  	}
 140  }
 141  
 142  // BenchmarkReadVarInt9 performs a benchmark on how long it takes to read
 143  // a nine byte variable length integer.
 144  func BenchmarkReadVarInt9(b *testing.B) {
 145  	b.ReportAllocs()
 146  
 147  	buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
 148  	r := bytes.NewReader(buf)
 149  	for i := 0; i < b.N; i++ {
 150  		r.Seek(0, 0)
 151  		ReadVarInt(r, 0)
 152  	}
 153  }
 154  
 155  // BenchmarkWriteVarIntBuf1 performs a benchmark on how long it takes to write
 156  // a single byte variable length integer.
 157  func BenchmarkWriteVarIntBuf1(b *testing.B) {
 158  	b.ReportAllocs()
 159  
 160  	buffer := binarySerializer.Borrow()
 161  	for i := 0; i < b.N; i++ {
 162  		WriteVarIntBuf(io.Discard, 0, 1, buffer)
 163  	}
 164  	binarySerializer.Return(buffer)
 165  }
 166  
 167  // BenchmarkWriteVarIntBuf3 performs a benchmark on how long it takes to write
 168  // a three byte variable length integer.
 169  func BenchmarkWriteVarIntBuf3(b *testing.B) {
 170  	b.ReportAllocs()
 171  
 172  	buffer := binarySerializer.Borrow()
 173  	for i := 0; i < b.N; i++ {
 174  		WriteVarIntBuf(io.Discard, 0, 65535, buffer)
 175  	}
 176  	binarySerializer.Return(buffer)
 177  }
 178  
 179  // BenchmarkWriteVarIntBuf5 performs a benchmark on how long it takes to write
 180  // a five byte variable length integer.
 181  func BenchmarkWriteVarIntBuf5(b *testing.B) {
 182  	b.ReportAllocs()
 183  
 184  	buffer := binarySerializer.Borrow()
 185  	for i := 0; i < b.N; i++ {
 186  		WriteVarIntBuf(io.Discard, 0, 4294967295, buffer)
 187  	}
 188  	binarySerializer.Return(buffer)
 189  }
 190  
 191  // BenchmarkWriteVarIntBuf9 performs a benchmark on how long it takes to write
 192  // a nine byte variable length integer.
 193  func BenchmarkWriteVarIntBuf9(b *testing.B) {
 194  	b.ReportAllocs()
 195  
 196  	buffer := binarySerializer.Borrow()
 197  	for i := 0; i < b.N; i++ {
 198  		WriteVarIntBuf(io.Discard, 0, 18446744073709551615, buffer)
 199  	}
 200  	binarySerializer.Return(buffer)
 201  }
 202  
 203  // BenchmarkReadVarIntBuf1 performs a benchmark on how long it takes to read
 204  // a single byte variable length integer.
 205  func BenchmarkReadVarIntBuf1(b *testing.B) {
 206  	b.ReportAllocs()
 207  
 208  	buffer := binarySerializer.Borrow()
 209  	buf := []byte{0x01}
 210  	r := bytes.NewReader(buf)
 211  	for i := 0; i < b.N; i++ {
 212  		r.Seek(0, 0)
 213  		ReadVarIntBuf(r, 0, buffer)
 214  	}
 215  	binarySerializer.Return(buffer)
 216  }
 217  
 218  // BenchmarkReadVarIntBuf3 performs a benchmark on how long it takes to read
 219  // a three byte variable length integer.
 220  func BenchmarkReadVarIntBuf3(b *testing.B) {
 221  	b.ReportAllocs()
 222  
 223  	buffer := binarySerializer.Borrow()
 224  	buf := []byte{0x0fd, 0xff, 0xff}
 225  	r := bytes.NewReader(buf)
 226  	for i := 0; i < b.N; i++ {
 227  		r.Seek(0, 0)
 228  		ReadVarIntBuf(r, 0, buffer)
 229  	}
 230  	binarySerializer.Return(buffer)
 231  }
 232  
 233  // BenchmarkReadVarIntBuf5 performs a benchmark on how long it takes to read
 234  // a five byte variable length integer.
 235  func BenchmarkReadVarIntBuf5(b *testing.B) {
 236  	b.ReportAllocs()
 237  
 238  	buffer := binarySerializer.Borrow()
 239  	buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff}
 240  	r := bytes.NewReader(buf)
 241  	for i := 0; i < b.N; i++ {
 242  		r.Seek(0, 0)
 243  		ReadVarIntBuf(r, 0, buffer)
 244  	}
 245  	binarySerializer.Return(buffer)
 246  }
 247  
 248  // BenchmarkReadVarIntBuf9 performs a benchmark on how long it takes to read
 249  // a nine byte variable length integer.
 250  func BenchmarkReadVarIntBuf9(b *testing.B) {
 251  	b.ReportAllocs()
 252  
 253  	buffer := binarySerializer.Borrow()
 254  	buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
 255  	r := bytes.NewReader(buf)
 256  	for i := 0; i < b.N; i++ {
 257  		r.Seek(0, 0)
 258  		ReadVarIntBuf(r, 0, buffer)
 259  	}
 260  	binarySerializer.Return(buffer)
 261  }
 262  
 263  // BenchmarkReadVarStr4 performs a benchmark on how long it takes to read a
 264  // four byte variable length string.
 265  func BenchmarkReadVarStr4(b *testing.B) {
 266  	b.ReportAllocs()
 267  
 268  	buf := []byte{0x04, 't', 'e', 's', 't'}
 269  	r := bytes.NewReader(buf)
 270  	for i := 0; i < b.N; i++ {
 271  		r.Seek(0, 0)
 272  		ReadVarString(r, 0)
 273  	}
 274  }
 275  
 276  // BenchmarkReadVarStr10 performs a benchmark on how long it takes to read a
 277  // ten byte variable length string.
 278  func BenchmarkReadVarStr10(b *testing.B) {
 279  	b.ReportAllocs()
 280  
 281  	buf := []byte{0x0a, 't', 'e', 's', 't', '0', '1', '2', '3', '4', '5'}
 282  	r := bytes.NewReader(buf)
 283  	for i := 0; i < b.N; i++ {
 284  		r.Seek(0, 0)
 285  		ReadVarString(r, 0)
 286  	}
 287  }
 288  
 289  // BenchmarkWriteVarStr4 performs a benchmark on how long it takes to write a
 290  // four byte variable length string.
 291  func BenchmarkWriteVarStr4(b *testing.B) {
 292  	b.ReportAllocs()
 293  
 294  	for i := 0; i < b.N; i++ {
 295  		WriteVarString(io.Discard, 0, "test")
 296  	}
 297  }
 298  
 299  // BenchmarkWriteVarStr10 performs a benchmark on how long it takes to write a
 300  // ten byte variable length string.
 301  func BenchmarkWriteVarStr10(b *testing.B) {
 302  	b.ReportAllocs()
 303  
 304  	for i := 0; i < b.N; i++ {
 305  		WriteVarString(io.Discard, 0, "test012345")
 306  	}
 307  }
 308  
 309  // BenchmarkReadVarStrBuf4 performs a benchmark on how long it takes to read a
 310  // four byte variable length string.
 311  func BenchmarkReadVarStrBuf4(b *testing.B) {
 312  	b.ReportAllocs()
 313  
 314  	buffer := binarySerializer.Borrow()
 315  	buf := []byte{0x04, 't', 'e', 's', 't'}
 316  	r := bytes.NewReader(buf)
 317  	for i := 0; i < b.N; i++ {
 318  		r.Seek(0, 0)
 319  		readVarStringBuf(r, 0, buffer)
 320  	}
 321  	binarySerializer.Return(buffer)
 322  }
 323  
 324  // BenchmarkReadVarStrBuf10 performs a benchmark on how long it takes to read a
 325  // ten byte variable length string.
 326  func BenchmarkReadVarStrBuf10(b *testing.B) {
 327  	b.ReportAllocs()
 328  
 329  	buffer := binarySerializer.Borrow()
 330  	buf := []byte{0x0a, 't', 'e', 's', 't', '0', '1', '2', '3', '4', '5'}
 331  	r := bytes.NewReader(buf)
 332  	for i := 0; i < b.N; i++ {
 333  		r.Seek(0, 0)
 334  		readVarStringBuf(r, 0, buf)
 335  	}
 336  	binarySerializer.Return(buffer)
 337  }
 338  
 339  // BenchmarkWriteVarStrBuf4 performs a benchmark on how long it takes to write a
 340  // four byte variable length string.
 341  func BenchmarkWriteVarStrBuf4(b *testing.B) {
 342  	b.ReportAllocs()
 343  
 344  	buf := binarySerializer.Borrow()
 345  	for i := 0; i < b.N; i++ {
 346  		writeVarStringBuf(io.Discard, 0, "test", buf)
 347  	}
 348  	binarySerializer.Return(buf)
 349  }
 350  
 351  // BenchmarkWriteVarStrBuf10 performs a benchmark on how long it takes to write
 352  // a ten byte variable length string.
 353  func BenchmarkWriteVarStrBuf10(b *testing.B) {
 354  	b.ReportAllocs()
 355  
 356  	buf := binarySerializer.Borrow()
 357  	for i := 0; i < b.N; i++ {
 358  		writeVarStringBuf(io.Discard, 0, "test012345", buf)
 359  	}
 360  	binarySerializer.Return(buf)
 361  }
 362  
 363  // BenchmarkReadOutPoint performs a benchmark on how long it takes to read a
 364  // transaction output point.
 365  func BenchmarkReadOutPoint(b *testing.B) {
 366  	b.ReportAllocs()
 367  
 368  	buffer := binarySerializer.Borrow()
 369  	buf := []byte{
 370  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 371  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 372  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 373  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
 374  		0xff, 0xff, 0xff, 0xff, // Previous output index
 375  	}
 376  	r := bytes.NewReader(buf)
 377  	var op OutPoint
 378  	for i := 0; i < b.N; i++ {
 379  		r.Seek(0, 0)
 380  		readOutPointBuf(r, 0, 0, &op, buffer)
 381  	}
 382  	binarySerializer.Return(buffer)
 383  }
 384  
 385  // BenchmarkWriteOutPoint performs a benchmark on how long it takes to write a
 386  // transaction output point.
 387  func BenchmarkWriteOutPoint(b *testing.B) {
 388  	b.ReportAllocs()
 389  
 390  	op := &OutPoint{
 391  		Hash:  chainhash.Hash{},
 392  		Index: 0,
 393  	}
 394  	for i := 0; i < b.N; i++ {
 395  		WriteOutPoint(io.Discard, 0, 0, op)
 396  	}
 397  }
 398  
 399  // BenchmarkWriteOutPointBuf performs a benchmark on how long it takes to write a
 400  // transaction output point.
 401  func BenchmarkWriteOutPointBuf(b *testing.B) {
 402  	b.ReportAllocs()
 403  
 404  	buf := binarySerializer.Borrow()
 405  	op := &OutPoint{
 406  		Hash:  chainhash.Hash{},
 407  		Index: 0,
 408  	}
 409  	for i := 0; i < b.N; i++ {
 410  		writeOutPointBuf(io.Discard, 0, 0, op, buf)
 411  	}
 412  	binarySerializer.Return(buf)
 413  }
 414  
 415  // BenchmarkReadTxOut performs a benchmark on how long it takes to read a
 416  // transaction output.
 417  func BenchmarkReadTxOut(b *testing.B) {
 418  	b.ReportAllocs()
 419  
 420  	buf := []byte{
 421  		0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
 422  		0x43, // Varint for length of pk script
 423  		0x41, // OP_DATA_65
 424  		0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
 425  		0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
 426  		0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
 427  		0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
 428  		0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
 429  		0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
 430  		0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
 431  		0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
 432  		0xee, // 65-byte signature
 433  		0xac, // OP_CHECKSIG
 434  	}
 435  	r := bytes.NewReader(buf)
 436  	var txOut TxOut
 437  	for i := 0; i < b.N; i++ {
 438  		r.Seek(0, 0)
 439  		ReadTxOut(r, 0, 0, &txOut)
 440  	}
 441  }
 442  
 443  // BenchmarkReadTxOutBuf performs a benchmark on how long it takes to read a
 444  // transaction output.
 445  func BenchmarkReadTxOutBuf(b *testing.B) {
 446  	b.ReportAllocs()
 447  
 448  	ar := borrowScriptArena()
 449  	buffer := binarySerializer.Borrow()
 450  	buf := []byte{
 451  		0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
 452  		0x43, // Varint for length of pk script
 453  		0x41, // OP_DATA_65
 454  		0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
 455  		0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
 456  		0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
 457  		0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
 458  		0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
 459  		0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
 460  		0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
 461  		0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
 462  		0xee, // 65-byte signature
 463  		0xac, // OP_CHECKSIG
 464  	}
 465  	r := bytes.NewReader(buf)
 466  	var txOut TxOut
 467  	for i := 0; i < b.N; i++ {
 468  		r.Seek(0, 0)
 469  		ar.rewind()
 470  		readTxOutBuf(r, 0, 0, &txOut, buffer, ar)
 471  	}
 472  	binarySerializer.Return(buffer)
 473  	ar.release()
 474  }
 475  
 476  // BenchmarkWriteTxOut performs a benchmark on how long it takes to write
 477  // a transaction output.
 478  func BenchmarkWriteTxOut(b *testing.B) {
 479  	b.ReportAllocs()
 480  
 481  	txOut := blockOne.Transactions[0].TxOut[0]
 482  	for i := 0; i < b.N; i++ {
 483  		WriteTxOut(io.Discard, 0, 0, txOut)
 484  	}
 485  }
 486  
 487  // BenchmarkWriteTxOutBuf performs a benchmark on how long it takes to write
 488  // a transaction output.
 489  func BenchmarkWriteTxOutBuf(b *testing.B) {
 490  	b.ReportAllocs()
 491  
 492  	buf := binarySerializer.Borrow()
 493  	txOut := blockOne.Transactions[0].TxOut[0]
 494  	for i := 0; i < b.N; i++ {
 495  		WriteTxOutBuf(io.Discard, 0, 0, txOut, buf)
 496  	}
 497  	binarySerializer.Return(buf)
 498  }
 499  
 500  // BenchmarkReadTxIn performs a benchmark on how long it takes to read a
 501  // transaction input.
 502  func BenchmarkReadTxIn(b *testing.B) {
 503  	b.ReportAllocs()
 504  
 505  	ar := borrowScriptArena()
 506  	buffer := binarySerializer.Borrow()
 507  	buf := []byte{
 508  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 509  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 510  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 511  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
 512  		0xff, 0xff, 0xff, 0xff, // Previous output index
 513  		0x07,                                     // Varint for length of signature script
 514  		0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
 515  		0xff, 0xff, 0xff, 0xff, // Sequence
 516  	}
 517  	r := bytes.NewReader(buf)
 518  	var txIn TxIn
 519  	for i := 0; i < b.N; i++ {
 520  		r.Seek(0, 0)
 521  		ar.rewind()
 522  		readTxInBuf(r, 0, 0, &txIn, buffer, ar)
 523  	}
 524  	binarySerializer.Return(buffer)
 525  	ar.release()
 526  }
 527  
 528  // BenchmarkWriteTxIn performs a benchmark on how long it takes to write
 529  // a transaction input.
 530  func BenchmarkWriteTxIn(b *testing.B) {
 531  	b.ReportAllocs()
 532  
 533  	buf := binarySerializer.Borrow()
 534  	txIn := blockOne.Transactions[0].TxIn[0]
 535  	for i := 0; i < b.N; i++ {
 536  		writeTxInBuf(io.Discard, 0, 0, txIn, buf)
 537  	}
 538  	binarySerializer.Return(buf)
 539  }
 540  
 541  // BenchmarkDeserializeTx performs a benchmark on how long it takes to
 542  // deserialize a small transaction.
 543  func BenchmarkDeserializeTxSmall(b *testing.B) {
 544  	buf := []byte{
 545  		0x01, 0x00, 0x00, 0x00, // Version
 546  		0x01, // Varint for number of input transactions
 547  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 548  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 549  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 550  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //  // Previous output hash
 551  		0xff, 0xff, 0xff, 0xff, // Previous output index
 552  		0x07,                                     // Varint for length of signature script
 553  		0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
 554  		0xff, 0xff, 0xff, 0xff, // Sequence
 555  		0x01,                                           // Varint for number of output transactions
 556  		0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
 557  		0x43, // Varint for length of pk script
 558  		0x41, // OP_DATA_65
 559  		0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
 560  		0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
 561  		0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
 562  		0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
 563  		0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
 564  		0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
 565  		0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
 566  		0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
 567  		0xee,                   // 65-byte signature
 568  		0xac,                   // OP_CHECKSIG
 569  		0x00, 0x00, 0x00, 0x00, // Lock time
 570  	}
 571  
 572  	b.ReportAllocs()
 573  	b.ResetTimer()
 574  
 575  	r := bytes.NewReader(buf)
 576  	var tx MsgTx
 577  	for i := 0; i < b.N; i++ {
 578  		r.Seek(0, 0)
 579  		tx.Deserialize(r)
 580  	}
 581  }
 582  
 583  // BenchmarkDeserializeTxLarge performs a benchmark on how long it takes to
 584  // deserialize a very large transaction.
 585  func BenchmarkDeserializeTxLarge(b *testing.B) {
 586  
 587  	// tx bb41a757f405890fb0f5856228e23b715702d714d59bf2b1feb70d8b2b4e3e08
 588  	// from the main block chain.
 589  	fi, err := os.Open("testdata/megatx.bin.bz2")
 590  	if err != nil {
 591  		b.Fatalf("Failed to read transaction data: %v", err)
 592  	}
 593  	defer fi.Close()
 594  	buf, err := io.ReadAll(bzip2.NewReader(fi))
 595  	if err != nil {
 596  		b.Fatalf("Failed to read transaction data: %v", err)
 597  	}
 598  
 599  	b.ReportAllocs()
 600  	b.ResetTimer()
 601  
 602  	r := bytes.NewReader(buf)
 603  	var tx MsgTx
 604  	for i := 0; i < b.N; i++ {
 605  		r.Seek(0, 0)
 606  		tx.Deserialize(r)
 607  	}
 608  }
 609  
 610  func BenchmarkDeserializeBlock(b *testing.B) {
 611  	buf, err := os.ReadFile(
 612  		"testdata/block-00000000000000000021868c2cefc52a480d173c849412fe81c4e5ab806f94ab.blk",
 613  	)
 614  	if err != nil {
 615  		b.Fatalf("Failed to read block data: %v", err)
 616  	}
 617  
 618  	b.ReportAllocs()
 619  	b.ResetTimer()
 620  
 621  	r := bytes.NewReader(buf)
 622  	var block MsgBlock
 623  	for i := 0; i < b.N; i++ {
 624  		r.Seek(0, 0)
 625  		block.Deserialize(r)
 626  	}
 627  }
 628  
 629  func BenchmarkSerializeBlock(b *testing.B) {
 630  	buf, err := os.ReadFile(
 631  		"testdata/block-00000000000000000021868c2cefc52a480d173c849412fe81c4e5ab806f94ab.blk",
 632  	)
 633  	if err != nil {
 634  		b.Fatalf("Failed to read block data: %v", err)
 635  	}
 636  
 637  	var block MsgBlock
 638  	err = block.Deserialize(bytes.NewReader(buf))
 639  	if err != nil {
 640  		panic(err.Error())
 641  	}
 642  
 643  	b.ReportAllocs()
 644  	b.ResetTimer()
 645  
 646  	for i := 0; i < b.N; i++ {
 647  		block.Serialize(io.Discard)
 648  	}
 649  }
 650  
 651  // BenchmarkSerializeTx performs a benchmark on how long it takes to serialize
 652  // a transaction.
 653  func BenchmarkSerializeTx(b *testing.B) {
 654  	b.ReportAllocs()
 655  
 656  	tx := blockOne.Transactions[0]
 657  	for i := 0; i < b.N; i++ {
 658  		tx.Serialize(io.Discard)
 659  
 660  	}
 661  }
 662  
 663  // BenchmarkSerializeTxSmall performs a benchmark on how long it takes to
 664  // serialize a transaction.
 665  func BenchmarkSerializeTxSmall(b *testing.B) {
 666  	buf := []byte{
 667  		0x01, 0x00, 0x00, 0x00, // Version
 668  		0x01, // Varint for number of input transactions
 669  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 670  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 671  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 672  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //  // Previous output hash
 673  		0xff, 0xff, 0xff, 0xff, // Previous output index
 674  		0x07,                                     // Varint for length of signature script
 675  		0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
 676  		0xff, 0xff, 0xff, 0xff, // Sequence
 677  		0x01,                                           // Varint for number of output transactions
 678  		0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
 679  		0x43, // Varint for length of pk script
 680  		0x41, // OP_DATA_65
 681  		0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
 682  		0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
 683  		0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
 684  		0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
 685  		0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
 686  		0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
 687  		0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
 688  		0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
 689  		0xee,                   // 65-byte signature
 690  		0xac,                   // OP_CHECKSIG
 691  		0x00, 0x00, 0x00, 0x00, // Lock time
 692  	}
 693  
 694  	var tx MsgTx
 695  	tx.Deserialize(bytes.NewReader(buf))
 696  
 697  	b.ReportAllocs()
 698  	b.ResetTimer()
 699  
 700  	for i := 0; i < b.N; i++ {
 701  		tx.Serialize(io.Discard)
 702  	}
 703  }
 704  
 705  // BenchmarkSerializeTxLarge performs a benchmark on how long it takes to
 706  // serialize a transaction.
 707  func BenchmarkSerializeTxLarge(b *testing.B) {
 708  	// tx bb41a757f405890fb0f5856228e23b715702d714d59bf2b1feb70d8b2b4e3e08
 709  	// from the main block chain.
 710  	fi, err := os.Open("testdata/megatx.bin.bz2")
 711  	if err != nil {
 712  		b.Fatalf("Failed to read transaction data: %v", err)
 713  	}
 714  	defer fi.Close()
 715  	buf, err := io.ReadAll(bzip2.NewReader(fi))
 716  	if err != nil {
 717  		b.Fatalf("Failed to read transaction data: %v", err)
 718  	}
 719  
 720  	var tx MsgTx
 721  	tx.Deserialize(bytes.NewReader(buf))
 722  
 723  	b.ReportAllocs()
 724  	b.ResetTimer()
 725  
 726  	for i := 0; i < b.N; i++ {
 727  		tx.Serialize(io.Discard)
 728  	}
 729  }
 730  
 731  // BenchmarkReadBlockHeader performs a benchmark on how long it takes to
 732  // deserialize a block header.
 733  func BenchmarkReadBlockHeader(b *testing.B) {
 734  	b.ReportAllocs()
 735  
 736  	buf := []byte{
 737  		0x01, 0x00, 0x00, 0x00, // Version 1
 738  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
 739  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
 740  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
 741  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
 742  		0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
 743  		0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
 744  		0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
 745  		0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
 746  		0x29, 0xab, 0x5f, 0x49, // Timestamp
 747  		0xff, 0xff, 0x00, 0x1d, // Bits
 748  		0xf3, 0xe0, 0x01, 0x00, // Nonce
 749  		0x00, // TxnCount Varint
 750  	}
 751  	r := bytes.NewReader(buf)
 752  	var header BlockHeader
 753  	for i := 0; i < b.N; i++ {
 754  		r.Seek(0, 0)
 755  		readBlockHeader(r, 0, &header)
 756  	}
 757  }
 758  
 759  // BenchmarkReadBlockHeaderBuf performs a benchmark on how long it takes to
 760  // deserialize a block header.
 761  func BenchmarkReadBlockHeaderBuf(b *testing.B) {
 762  	b.ReportAllocs()
 763  
 764  	buffer := binarySerializer.Borrow()
 765  	buf := []byte{
 766  		0x01, 0x00, 0x00, 0x00, // Version 1
 767  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
 768  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
 769  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
 770  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
 771  		0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
 772  		0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
 773  		0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
 774  		0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
 775  		0x29, 0xab, 0x5f, 0x49, // Timestamp
 776  		0xff, 0xff, 0x00, 0x1d, // Bits
 777  		0xf3, 0xe0, 0x01, 0x00, // Nonce
 778  		0x00, // TxnCount Varint
 779  	}
 780  	r := bytes.NewReader(buf)
 781  	var header BlockHeader
 782  	for i := 0; i < b.N; i++ {
 783  		r.Seek(0, 0)
 784  		readBlockHeaderBuf(r, 0, &header, buffer)
 785  	}
 786  	binarySerializer.Return(buffer)
 787  }
 788  
 789  // BenchmarkWriteBlockHeader performs a benchmark on how long it takes to
 790  // serialize a block header.
 791  func BenchmarkWriteBlockHeader(b *testing.B) {
 792  	b.ReportAllocs()
 793  
 794  	header := blockOne.Header
 795  	for i := 0; i < b.N; i++ {
 796  		writeBlockHeader(io.Discard, 0, &header)
 797  	}
 798  }
 799  
 800  // BenchmarkWriteBlockHeaderBuf performs a benchmark on how long it takes to
 801  // serialize a block header.
 802  func BenchmarkWriteBlockHeaderBuf(b *testing.B) {
 803  	b.ReportAllocs()
 804  
 805  	buf := binarySerializer.Borrow()
 806  	header := blockOne.Header
 807  	for i := 0; i < b.N; i++ {
 808  		writeBlockHeaderBuf(io.Discard, 0, &header, buf)
 809  	}
 810  	binarySerializer.Return(buf)
 811  }
 812  
 813  // BenchmarkDecodeGetHeaders performs a benchmark on how long it takes to
 814  // decode a getheaders message with the maximum number of block locator hashes.
 815  func BenchmarkDecodeGetHeaders(b *testing.B) {
 816  	b.ReportAllocs()
 817  
 818  	// Create a message with the maximum number of block locators.
 819  	pver := ProtocolVersion
 820  	var m MsgGetHeaders
 821  	for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
 822  		hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
 823  		if err != nil {
 824  			b.Fatalf("NewHashFromStr: unexpected error: %v", err)
 825  		}
 826  		m.AddBlockLocatorHash(hash)
 827  	}
 828  
 829  	// Serialize it so the bytes are available to test the decode below.
 830  	var bb bytes.Buffer
 831  	if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
 832  		b.Fatalf("MsgGetHeaders.BtcEncode: unexpected error: %v", err)
 833  	}
 834  	buf := bb.Bytes()
 835  
 836  	r := bytes.NewReader(buf)
 837  	var msg MsgGetHeaders
 838  	b.ResetTimer()
 839  	for i := 0; i < b.N; i++ {
 840  		r.Seek(0, 0)
 841  		msg.BtcDecode(r, pver, LatestEncoding)
 842  	}
 843  }
 844  
 845  // BenchmarkDecodeHeaders performs a benchmark on how long it takes to
 846  // decode a headers message with the maximum number of headers.
 847  func BenchmarkDecodeHeaders(b *testing.B) {
 848  	b.ReportAllocs()
 849  
 850  	// Create a message with the maximum number of headers.
 851  	pver := ProtocolVersion
 852  	var m MsgHeaders
 853  	for i := 0; i < MaxBlockHeadersPerMsg; i++ {
 854  		hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
 855  		if err != nil {
 856  			b.Fatalf("NewHashFromStr: unexpected error: %v", err)
 857  		}
 858  		m.AddBlockHeader(NewBlockHeader(1, hash, hash, 0, uint32(i)))
 859  	}
 860  
 861  	// Serialize it so the bytes are available to test the decode below.
 862  	var bb bytes.Buffer
 863  	if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
 864  		b.Fatalf("MsgHeaders.BtcEncode: unexpected error: %v", err)
 865  	}
 866  	buf := bb.Bytes()
 867  
 868  	r := bytes.NewReader(buf)
 869  	var msg MsgHeaders
 870  	b.ResetTimer()
 871  	for i := 0; i < b.N; i++ {
 872  		r.Seek(0, 0)
 873  		msg.BtcDecode(r, pver, LatestEncoding)
 874  	}
 875  }
 876  
 877  // BenchmarkDecodeGetBlocks performs a benchmark on how long it takes to
 878  // decode a getblocks message with the maximum number of block locator hashes.
 879  func BenchmarkDecodeGetBlocks(b *testing.B) {
 880  	b.ReportAllocs()
 881  
 882  	// Create a message with the maximum number of block locators.
 883  	pver := ProtocolVersion
 884  	var m MsgGetBlocks
 885  	for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
 886  		hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
 887  		if err != nil {
 888  			b.Fatalf("NewHashFromStr: unexpected error: %v", err)
 889  		}
 890  		m.AddBlockLocatorHash(hash)
 891  	}
 892  
 893  	// Serialize it so the bytes are available to test the decode below.
 894  	var bb bytes.Buffer
 895  	if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
 896  		b.Fatalf("MsgGetBlocks.BtcEncode: unexpected error: %v", err)
 897  	}
 898  	buf := bb.Bytes()
 899  
 900  	r := bytes.NewReader(buf)
 901  	var msg MsgGetBlocks
 902  	b.ResetTimer()
 903  	for i := 0; i < b.N; i++ {
 904  		r.Seek(0, 0)
 905  		msg.BtcDecode(r, pver, LatestEncoding)
 906  	}
 907  }
 908  
 909  // BenchmarkDecodeAddr performs a benchmark on how long it takes to decode an
 910  // addr message with the maximum number of addresses.
 911  func BenchmarkDecodeAddr(b *testing.B) {
 912  	b.ReportAllocs()
 913  
 914  	// Create a message with the maximum number of addresses.
 915  	pver := ProtocolVersion
 916  	ip := net.ParseIP("127.0.0.1")
 917  	ma := NewMsgAddr()
 918  	for port := uint16(0); port < MaxAddrPerMsg; port++ {
 919  		ma.AddAddress(NewNetAddressIPPort(ip, port, SFNodeNetwork))
 920  	}
 921  
 922  	// Serialize it so the bytes are available to test the decode below.
 923  	var bb bytes.Buffer
 924  	if err := ma.BtcEncode(&bb, pver, LatestEncoding); err != nil {
 925  		b.Fatalf("MsgAddr.BtcEncode: unexpected error: %v", err)
 926  	}
 927  	buf := bb.Bytes()
 928  
 929  	r := bytes.NewReader(buf)
 930  	var msg MsgAddr
 931  	b.ResetTimer()
 932  	for i := 0; i < b.N; i++ {
 933  		r.Seek(0, 0)
 934  		msg.BtcDecode(r, pver, LatestEncoding)
 935  	}
 936  }
 937  
 938  // BenchmarkDecodeInv performs a benchmark on how long it takes to decode an inv
 939  // message with the maximum number of entries.
 940  func BenchmarkDecodeInv(b *testing.B) {
 941  	// Create a message with the maximum number of entries.
 942  	pver := ProtocolVersion
 943  	var m MsgInv
 944  	for i := 0; i < MaxInvPerMsg; i++ {
 945  		hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
 946  		if err != nil {
 947  			b.Fatalf("NewHashFromStr: unexpected error: %v", err)
 948  		}
 949  		m.AddInvVect(NewInvVect(InvTypeBlock, hash))
 950  	}
 951  
 952  	// Serialize it so the bytes are available to test the decode below.
 953  	var bb bytes.Buffer
 954  	if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
 955  		b.Fatalf("MsgInv.BtcEncode: unexpected error: %v", err)
 956  	}
 957  	buf := bb.Bytes()
 958  
 959  	b.ReportAllocs()
 960  	b.ResetTimer()
 961  
 962  	r := bytes.NewReader(buf)
 963  	var msg MsgInv
 964  	b.ResetTimer()
 965  	for i := 0; i < b.N; i++ {
 966  		r.Seek(0, 0)
 967  		msg.BtcDecode(r, pver, LatestEncoding)
 968  	}
 969  }
 970  
 971  // BenchmarkDecodeNotFound performs a benchmark on how long it takes to decode
 972  // a notfound message with the maximum number of entries.
 973  func BenchmarkDecodeNotFound(b *testing.B) {
 974  	b.ReportAllocs()
 975  
 976  	// Create a message with the maximum number of entries.
 977  	pver := ProtocolVersion
 978  	var m MsgNotFound
 979  	for i := 0; i < MaxInvPerMsg; i++ {
 980  		hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
 981  		if err != nil {
 982  			b.Fatalf("NewHashFromStr: unexpected error: %v", err)
 983  		}
 984  		m.AddInvVect(NewInvVect(InvTypeBlock, hash))
 985  	}
 986  
 987  	// Serialize it so the bytes are available to test the decode below.
 988  	var bb bytes.Buffer
 989  	if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
 990  		b.Fatalf("MsgNotFound.BtcEncode: unexpected error: %v", err)
 991  	}
 992  	buf := bb.Bytes()
 993  
 994  	r := bytes.NewReader(buf)
 995  	var msg MsgNotFound
 996  	b.ResetTimer()
 997  	for i := 0; i < b.N; i++ {
 998  		r.Seek(0, 0)
 999  		msg.BtcDecode(r, pver, LatestEncoding)
1000  	}
1001  }
1002  
1003  // BenchmarkDecodeMerkleBlock performs a benchmark on how long it takes to
1004  // decode a reasonably sized merkleblock message.
1005  func BenchmarkDecodeMerkleBlock(b *testing.B) {
1006  	b.ReportAllocs()
1007  
1008  	// Create a message with random data.
1009  	pver := ProtocolVersion
1010  	var m MsgMerkleBlock
1011  	hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", 10000))
1012  	if err != nil {
1013  		b.Fatalf("NewHashFromStr: unexpected error: %v", err)
1014  	}
1015  	m.Header = *NewBlockHeader(1, hash, hash, 0, uint32(10000))
1016  	for i := 0; i < 105; i++ {
1017  		hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
1018  		if err != nil {
1019  			b.Fatalf("NewHashFromStr: unexpected error: %v", err)
1020  		}
1021  		m.AddTxHash(hash)
1022  		if i%8 == 0 {
1023  			m.Flags = append(m.Flags, uint8(i))
1024  		}
1025  	}
1026  
1027  	// Serialize it so the bytes are available to test the decode below.
1028  	var bb bytes.Buffer
1029  	if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
1030  		b.Fatalf("MsgMerkleBlock.BtcEncode: unexpected error: %v", err)
1031  	}
1032  	buf := bb.Bytes()
1033  
1034  	r := bytes.NewReader(buf)
1035  	var msg MsgMerkleBlock
1036  	b.ResetTimer()
1037  	for i := 0; i < b.N; i++ {
1038  		r.Seek(0, 0)
1039  		msg.BtcDecode(r, pver, LatestEncoding)
1040  	}
1041  }
1042  
1043  // BenchmarkTxHash performs a benchmark on how long it takes to hash a
1044  // transaction.
1045  func BenchmarkTxHash(b *testing.B) {
1046  	b.ReportAllocs()
1047  
1048  	for i := 0; i < b.N; i++ {
1049  		genesisCoinbaseTx.TxHash()
1050  	}
1051  }
1052  
1053  // BenchmarkDoubleHashB performs a benchmark on how long it takes to perform a
1054  // double hash returning a byte slice.
1055  func BenchmarkDoubleHashB(b *testing.B) {
1056  	b.ReportAllocs()
1057  
1058  	var buf bytes.Buffer
1059  	if err := genesisCoinbaseTx.Serialize(&buf); err != nil {
1060  		b.Errorf("Serialize: unexpected error: %v", err)
1061  		return
1062  	}
1063  	txBytes := buf.Bytes()
1064  
1065  	b.ResetTimer()
1066  	for i := 0; i < b.N; i++ {
1067  		_ = chainhash.DoubleHashB(txBytes)
1068  	}
1069  }
1070  
1071  // BenchmarkDoubleHashH performs a benchmark on how long it takes to perform
1072  // a double hash returning a chainhash.Hash.
1073  func BenchmarkDoubleHashH(b *testing.B) {
1074  	b.ReportAllocs()
1075  
1076  	var buf bytes.Buffer
1077  	if err := genesisCoinbaseTx.Serialize(&buf); err != nil {
1078  		b.Errorf("Serialize: unexpected error: %v", err)
1079  		return
1080  	}
1081  	txBytes := buf.Bytes()
1082  
1083  	b.ResetTimer()
1084  	for i := 0; i < b.N; i++ {
1085  		_ = chainhash.DoubleHashH(txBytes)
1086  	}
1087  }
1088