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
use criterion::{measurement::Measurement, BatchSize, Bencher};
use diem_crypto::HashValue;
use diem_types::{
block_metadata::BlockMetadata,
on_chain_config::{OnChainConfig, ValidatorSet},
transaction::Transaction,
};
use diem_vm::{DiemVM, VMExecutor};
use language_e2e_tests::{
account_universe::{log_balance_strategy, AUTransactionGen, AccountUniverseGen},
executor::FakeExecutor,
gas_costs::TXN_RESERVED,
};
use proptest::{
collection::vec,
strategy::{Strategy, ValueTree},
test_runner::TestRunner,
};
#[derive(Clone, Debug)]
pub struct TransactionBencher<S> {
num_accounts: usize,
num_transactions: usize,
strategy: S,
}
impl<S> TransactionBencher<S>
where
S: Strategy,
S::Value: AUTransactionGen,
{
pub const DEFAULT_NUM_ACCOUNTS: usize = 100;
pub const DEFAULT_NUM_TRANSACTIONS: usize = 1000;
pub fn new(strategy: S) -> Self {
Self {
num_accounts: Self::DEFAULT_NUM_ACCOUNTS,
num_transactions: Self::DEFAULT_NUM_TRANSACTIONS,
strategy,
}
}
pub fn num_accounts(&mut self, num_accounts: usize) -> &mut Self {
self.num_accounts = num_accounts;
self
}
pub fn num_transactions(&mut self, num_transactions: usize) -> &mut Self {
self.num_transactions = num_transactions;
self
}
pub fn bench<M: Measurement>(&self, b: &mut Bencher<M>) {
b.iter_batched(
|| {
TransactionBenchState::with_size(
&self.strategy,
self.num_accounts,
self.num_transactions,
)
},
|state| state.execute(),
BatchSize::LargeInput,
)
}
pub fn bench_parallel<M: Measurement>(&self, b: &mut Bencher<M>) {
b.iter_batched(
|| {
TransactionBenchState::with_size_parallel(
&self.strategy,
self.num_accounts,
self.num_transactions,
)
},
|state| state.execute(),
BatchSize::LargeInput,
)
}
}
struct TransactionBenchState {
executor: FakeExecutor,
transactions: Vec<Transaction>,
}
impl TransactionBenchState {
fn with_size<S>(strategy: S, num_accounts: usize, num_transactions: usize) -> Self
where
S: Strategy,
S::Value: AUTransactionGen,
{
Self::with_universe(
strategy,
universe_strategy(num_accounts, num_transactions),
num_transactions,
)
}
fn with_universe<S>(
strategy: S,
universe_strategy: impl Strategy<Value = AccountUniverseGen>,
num_transactions: usize,
) -> Self
where
S: Strategy,
S::Value: AUTransactionGen,
{
let mut runner = TestRunner::default();
let universe = universe_strategy
.new_tree(&mut runner)
.expect("creating a new value should succeed")
.current();
let mut executor = FakeExecutor::from_genesis_file();
let mut universe = universe.setup_gas_cost_stability(&mut executor);
let transaction_gens = vec(strategy, num_transactions)
.new_tree(&mut runner)
.expect("creating a new value should succeed")
.current();
let transactions = transaction_gens
.into_iter()
.map(|txn_gen| Transaction::UserTransaction(txn_gen.apply(&mut universe).0))
.collect();
Self {
executor,
transactions,
}
}
fn execute(self) {
DiemVM::execute_block(self.transactions, self.executor.get_state_view())
.expect("VM should not fail to start");
}
fn with_size_parallel<S>(strategy: S, num_accounts: usize, num_transactions: usize) -> Self
where
S: Strategy,
S::Value: AUTransactionGen,
{
let mut state = Self::with_universe(
strategy,
universe_strategy(num_accounts, num_transactions),
num_transactions,
);
state.executor.enable_parallel_execution();
let validator_set = ValidatorSet::fetch_config(state.executor.get_state_view())
.expect("Unable to retrieve the validator set from storage");
let new_block = BlockMetadata::new(
HashValue::zero(),
0,
1,
vec![],
*validator_set.payload()[0].account_address(),
);
state
.transactions
.insert(0, Transaction::BlockMetadata(new_block));
state
}
}
fn universe_strategy(
num_accounts: usize,
num_transactions: usize,
) -> impl Strategy<Value = AccountUniverseGen> {
let max_balance = TXN_RESERVED * num_transactions as u64 * 5;
let balance_strategy = log_balance_strategy(max_balance);
AccountUniverseGen::strategy(num_accounts, balance_strategy)
}