-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathtransaction_bench_test.go
More file actions
176 lines (163 loc) · 4.97 KB
/
transaction_bench_test.go
File metadata and controls
176 lines (163 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package solana
import (
"testing"
)
// buildBenchInstructions creates a realistic set of instructions for benchmarking
// NewTransaction. Each instruction references a distinct program ID and mixes
// signer/writable/readonly accounts so the builder exercises all header-counting
// and indexing paths.
//
// numInstructions: how many instructions in the transaction.
// accountsPerIx: how many AccountMeta entries each instruction references.
//
// (first is always a signer; second is always writable; rest readonly)
func buildBenchInstructions(numInstructions, accountsPerIx int) ([]Instruction, Hash) {
// Pre-generate a pool of unique accounts so instructions share some accounts
// (realistic — the same fee payer / writable state account appears in many ixs)
// while still producing a meaningful total.
poolSize := numInstructions * accountsPerIx / 2
if poolSize < accountsPerIx {
poolSize = accountsPerIx
}
pool := make([]PublicKey, poolSize)
for i := range pool {
pool[i] = newUniqueKey()
}
feePayer := pool[0]
instructions := make([]Instruction, numInstructions)
for i := 0; i < numInstructions; i++ {
accounts := make(AccountMetaSlice, accountsPerIx)
for j := 0; j < accountsPerIx; j++ {
pk := pool[(i*accountsPerIx+j)%len(pool)]
var isSigner, isWritable bool
switch j {
case 0:
// fee payer — signer, writable
pk = feePayer
isSigner = true
isWritable = true
case 1:
isWritable = true
}
accounts[j] = &AccountMeta{
PublicKey: pk,
IsSigner: isSigner,
IsWritable: isWritable,
}
}
instructions[i] = NewInstruction(
newUniqueKey(), // distinct program ID per instruction
accounts,
[]byte{byte(i), 0xAA, 0xBB, 0xCC},
)
}
return instructions, Hash{1, 2, 3, 4, 5}
}
// buildBenchInstructionsWithLookups is like buildBenchInstructions but also
// prepares an address-lookup-table so most of the accounts get compiled into
// ATL lookups instead of the static key list. This stresses the path where
// lookupsWritableKeys / lookupsReadOnlyKeys are non-empty.
func buildBenchInstructionsWithLookups(numInstructions, accountsPerIx int) ([]Instruction, Hash, map[PublicKey]PublicKeySlice) {
instructions, blockhash := buildBenchInstructions(numInstructions, accountsPerIx)
// Collect all non-signer, non-program accounts into a single ATL.
seen := make(map[PublicKey]struct{})
var tableAccounts PublicKeySlice
for _, ix := range instructions {
for _, am := range ix.Accounts() {
if am.IsSigner {
continue
}
if _, ok := seen[am.PublicKey]; ok {
continue
}
seen[am.PublicKey] = struct{}{}
tableAccounts = append(tableAccounts, am.PublicKey)
if len(tableAccounts) == 256 {
break
}
}
if len(tableAccounts) == 256 {
break
}
}
tableKey := newUniqueKey()
tables := map[PublicKey]PublicKeySlice{
tableKey: tableAccounts,
}
return instructions, blockhash, tables
}
// Realistic transaction shapes for solana-go benchmarks.
// Upper bound is ~10 instructions / ~30 accounts per ix — beyond that
// becomes a synthetic stress shape that doesn't represent real traffic.
var benchTxShapes = []struct {
name string
numInstructions int
accountsPerIx int
}{
{"small_2ix_5accts", 2, 5},
{"medium_5ix_15accts", 5, 15},
{"large_10ix_30accts", 10, 30},
}
func BenchmarkNewTransaction(b *testing.B) {
for _, tc := range benchTxShapes {
tc := tc
b.Run(tc.name, func(b *testing.B) {
instructions, blockhash := buildBenchInstructions(tc.numInstructions, tc.accountsPerIx)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tx, err := NewTransaction(instructions, blockhash)
if err != nil {
b.Fatal(err)
}
_ = tx
}
})
}
}
func BenchmarkNewTransaction_WithLookupTable(b *testing.B) {
for _, tc := range benchTxShapes {
tc := tc
b.Run(tc.name, func(b *testing.B) {
instructions, blockhash, tables := buildBenchInstructionsWithLookups(tc.numInstructions, tc.accountsPerIx)
opt := TransactionAddressTables(tables)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tx, err := NewTransaction(instructions, blockhash, opt)
if err != nil {
b.Fatal(err)
}
_ = tx
}
})
}
}
func BenchmarkTransaction_NumWriteableAccounts(b *testing.B) {
// Legacy (non-versioned) takes the AccountMetaList path.
b.Run("legacy_large", func(b *testing.B) {
instructions, blockhash := buildBenchInstructions(10, 30)
tx, err := NewTransaction(instructions, blockhash)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = tx.NumWriteableAccounts()
}
})
// V0 with ATL takes the static-key-scan path (the O(n^2) candidate).
b.Run("v0_large", func(b *testing.B) {
instructions, blockhash, tables := buildBenchInstructionsWithLookups(10, 30)
tx, err := NewTransaction(instructions, blockhash, TransactionAddressTables(tables))
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = tx.NumWriteableAccounts()
}
})
}