-
Notifications
You must be signed in to change notification settings - Fork 4
/
overhead_test.go
235 lines (191 loc) 路 6.63 KB
/
overhead_test.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package benchmarking
import (
"fmt"
"math"
"testing"
"time"
gossamertypes "github.com/ChainSafe/gossamer/dot/types"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
)
var (
signer = signature.TestKeyringPairAlice
)
func BenchmarkOverheadBlockExecutionWeight(t *testing.B) {
// todo set heapPages and dbCache when Gossamer starts supporting db caching
runtime := wazero_runtime.NewBenchInstanceWithTrie(t, Config.WasmRuntime, trie.NewEmptyTrie())
defer runtime.Stop()
instance, err := newBenchmarkingInstance(runtime, Config.Overhead.Repeat)
if err != nil {
t.Fatalf("failed to create benchmarking instance: [%v]", err)
}
err = instance.BuildGenesisConfig()
if err != nil {
t.Fatal(err)
}
stats := benchBlock(t, instance, Config.Overhead)
fmt.Println(stats.String())
if Config.GenerateWeightFiles {
if err := generateOverheadWeightFile("../constants/block_execution_weight.go", stats); err != nil {
t.Fatalf("failed to generate weight file: %v", err)
}
}
}
func BenchmarkOverheadBaseExtrinsicWeight(t *testing.B) {
// todo set heapPages and dbCache when Gossamer starts supporting db caching
runtime := wazero_runtime.NewBenchInstanceWithTrie(t, Config.WasmRuntime, trie.NewEmptyTrie())
defer runtime.Stop()
instance, err := newBenchmarkingInstance(runtime, Config.Overhead.Repeat)
if err != nil {
t.Fatalf("failed to create benchmarking instance: [%v]", err)
}
err = instance.BuildGenesisConfig()
if err != nil {
t.Fatal(err)
}
stats := benchExtrinsic(t, instance, Config.Overhead)
fmt.Println(stats.String())
if Config.GenerateWeightFiles {
if err := generateOverheadWeightFile("../constants/base_extrinsic_weight.go", stats); err != nil {
t.Fatalf("failed to generate weight file: %v", err)
}
}
}
func benchBlock(b *testing.B, instance *Instance, config overheadConfig) StatsResult {
// Build the block
block, err := buildBlock(instance, false, config.MaxExtPerBlock)
if err != nil {
b.Fatalf("failed to build empty block: [%v]", err)
}
// Measure the block
results, err := measureBlock(instance, config, block)
if err != nil {
b.Fatal(err)
}
// Create the stats
stats, err := NewStatsResult(results)
if err != nil {
b.Fatal(err)
}
return stats
}
func benchExtrinsic(b *testing.B, instance *Instance, config overheadConfig) StatsResult {
// Build an Empty block
baseEmptyBlock, err := buildBlock(instance, false, config.MaxExtPerBlock)
if err != nil {
b.Fatalf("failed to build empty block: [%v]", err)
}
// Measure the Empty block
baseResults, err := measureBlock(instance, config, baseEmptyBlock)
if err != nil {
b.Fatal(err)
}
// Create the Empty block stats
baseStats, err := NewStatsResult(baseResults)
if err != nil {
b.Fatal(err)
}
// Build a Block with maximum possible Extrinsics
block, err := buildBlock(instance, true, config.MaxExtPerBlock)
if err != nil {
b.Fatalf("failed to build block with extrinsics: [%v]", err)
}
totalExtrinsics := len(block.Body)
// Measure the block
extrinsicResults, err := measureBlock(instance, config, block)
if err != nil {
b.Fatal(err)
}
for i, extrinsicResult := range extrinsicResults {
// Subtract the base stats
extrinsicResult = math.Max(extrinsicResult-baseStats.Mean, 0)
// Divide by the total extrinsics
extrinsicResult = math.Ceil(extrinsicResult / float64(totalExtrinsics))
extrinsicResults[i] = extrinsicResult
}
// Create the stats
stats, err := NewStatsResult(extrinsicResults)
if err != nil {
b.Fatal(err)
}
return stats
}
func buildBlock(instance *Instance, hasExtrinsics bool, maxExtrinsicsPerBlock int) (gossamertypes.Block, error) {
inherentData, err := timestampInherentData(dateTime)
if err != nil {
return gossamertypes.Block{}, fmt.Errorf("failed to create inherent data: [%v]", err)
}
blockBuilder := NewBlockBuilder(instance, inherentData)
err = blockBuilder.StartSimulation(blockNumber)
if err != nil {
return gossamertypes.Block{}, fmt.Errorf("failed to start simulation: [%v]", err)
}
err = blockBuilder.ApplyInherentExtrinsics()
if err != nil {
return gossamertypes.Block{}, fmt.Errorf("failed to create inherent extrinsics: [%v]", err)
}
if hasExtrinsics {
fmt.Println("Building block, this takes some time...")
for i := 0; i < maxExtrinsicsPerBlock; i++ {
signatureOptions := ctypes.SignatureOptions{
BlockHash: ctypes.Hash(parentHash),
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
GenesisHash: ctypes.Hash(parentHash),
Nonce: ctypes.NewUCompactFromUInt(uint64(i)),
SpecVersion: ctypes.U32(instance.version.SpecVersion),
Tip: ctypes.NewUCompactFromUInt(0),
TransactionVersion: ctypes.U32(instance.version.TransactionVersion),
}
extrinsic, err := instance.newSignedExtrinsic(signer, signatureOptions, "System.remark", []byte{})
if err != nil {
return gossamertypes.Block{}, fmt.Errorf("failed to sign extrinsic: [%v]", err)
}
limitReached, err := blockBuilder.AddExtrinsic(extrinsic)
if err != nil {
return gossamertypes.Block{}, fmt.Errorf("failed to add extrinsic: [%v]", err)
}
if limitReached {
break
}
}
}
block, err := blockBuilder.FinishSimulation()
if err != nil {
return gossamertypes.Block{}, fmt.Errorf("failed to finish simulation: [%v]", err)
}
return block, nil
}
func measureBlock(instance *Instance, config overheadConfig, block gossamertypes.Block) ([]float64, error) {
encodedBlock, err := scale.Marshal(block)
if err != nil {
return nil, fmt.Errorf("failed to encode block: [%v]", err)
}
// Store the DB Snapshot with only Genesis Block
(*instance.storage).DbStoreSnapshot()
fmt.Println(fmt.Sprintf("Running [%d] warmups", config.Warmup))
for i := 0; i < config.Warmup; i++ {
(*instance.storage).DbRestoreSnapshot()
_, err := instance.runtime.Exec("Core_execute_block", encodedBlock)
if err != nil {
return nil, fmt.Errorf("failed to warmup execute block: [%v]", err)
}
}
var results []float64
fmt.Println(fmt.Sprintf("Executing block [%d] times", config.Repeat))
for i := 0; i < config.Repeat; i++ {
(*instance.storage).DbRestoreSnapshot()
start := time.Now().UnixNano()
_, err := instance.runtime.Exec("Core_execute_block", encodedBlock)
if err != nil {
return nil, fmt.Errorf("failed to execute block: [%v]", err)
}
end := time.Now().UnixNano()
results = append(results, float64(end-start))
}
// Restore the DB Snapshot
(*instance.storage).DbRestoreSnapshot()
return results, nil
}