bench_parallel_test.go raw

   1  // Copyright (c) 2026 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  	"io"
  11  	"os"
  12  	"testing"
  13  )
  14  
  15  // smallTxBytes returns the serialization of a typical small transaction,
  16  // mirroring the payload used by BenchmarkDeserializeTxSmall.
  17  func smallTxBytes() []byte {
  18  	return []byte{
  19  		0x01, 0x00, 0x00, 0x00, // Version
  20  		0x01, // Varint for number of input transactions
  21  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
  25  		0xff, 0xff, 0xff, 0xff, // Previous output index
  26  		0x07,                                     // Varint for length of signature script
  27  		0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
  28  		0xff, 0xff, 0xff, 0xff, // Sequence
  29  		0x01,                                           // Varint for number of output transactions
  30  		0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
  31  		0x43, // Varint for length of pk script
  32  		0x41, // OP_DATA_65
  33  		0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
  34  		0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
  35  		0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
  36  		0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
  37  		0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
  38  		0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
  39  		0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
  40  		0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
  41  		0xee,                   // 65-byte signature
  42  		0xac,                   // OP_CHECKSIG
  43  		0x00, 0x00, 0x00, 0x00, // Lock time
  44  	}
  45  }
  46  
  47  // BenchmarkDeserializeTxSmallParallel measures small transaction decoding
  48  // with all CPUs decoding concurrently, which models many peers relaying
  49  // mempool transactions at once.  This stresses contention on the shared
  50  // script staging pool as well as the transient memory borrowed per decode.
  51  func BenchmarkDeserializeTxSmallParallel(b *testing.B) {
  52  	buf := smallTxBytes()
  53  
  54  	b.ReportAllocs()
  55  	b.ResetTimer()
  56  
  57  	b.RunParallel(func(pb *testing.PB) {
  58  		r := bytes.NewReader(buf)
  59  		var tx MsgTx
  60  		for pb.Next() {
  61  			r.Seek(0, 0)
  62  			if err := tx.Deserialize(r); err != nil {
  63  				b.Error(err)
  64  				return
  65  			}
  66  		}
  67  	})
  68  }
  69  
  70  // BenchmarkDeserializeTxLargeParallel measures concurrent decoding of a
  71  // very large transaction, exercising the staging buffer growth path from
  72  // every CPU at once.
  73  func BenchmarkDeserializeTxLargeParallel(b *testing.B) {
  74  	fi, err := os.Open("testdata/megatx.bin.bz2")
  75  	if err != nil {
  76  		b.Fatalf("Failed to read transaction data: %v", err)
  77  	}
  78  	defer fi.Close()
  79  	buf, err := io.ReadAll(bzip2.NewReader(fi))
  80  	if err != nil {
  81  		b.Fatalf("Failed to read transaction data: %v", err)
  82  	}
  83  
  84  	b.ReportAllocs()
  85  	b.ResetTimer()
  86  
  87  	b.RunParallel(func(pb *testing.PB) {
  88  		r := bytes.NewReader(buf)
  89  		var tx MsgTx
  90  		for pb.Next() {
  91  			r.Seek(0, 0)
  92  			if err := tx.Deserialize(r); err != nil {
  93  				b.Error(err)
  94  				return
  95  			}
  96  		}
  97  	})
  98  }
  99  
 100  // BenchmarkDeserializeBlockParallel measures concurrent decoding of a full
 101  // mainnet block from every CPU at once, which models parallel block
 102  // download during initial block sync.
 103  func BenchmarkDeserializeBlockParallel(b *testing.B) {
 104  	buf, err := os.ReadFile(
 105  		"testdata/block-00000000000000000021868c2cefc52a480d173c849412fe81c4e5ab806f94ab.blk",
 106  	)
 107  	if err != nil {
 108  		b.Fatalf("Failed to read block data: %v", err)
 109  	}
 110  
 111  	b.ReportAllocs()
 112  	b.ResetTimer()
 113  
 114  	b.RunParallel(func(pb *testing.PB) {
 115  		r := bytes.NewReader(buf)
 116  		var block MsgBlock
 117  		for pb.Next() {
 118  			r.Seek(0, 0)
 119  			if err := block.Deserialize(r); err != nil {
 120  				b.Error(err)
 121  				return
 122  			}
 123  		}
 124  	})
 125  }
 126