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
use criterion::{measurement::Measurement, BatchSize, Bencher};
use diem_types::transaction::SignedTransaction;
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 = 500;
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,
)
}
}
struct TransactionBenchState {
executor: FakeExecutor,
transactions: Vec<SignedTransaction>,
}
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| txn_gen.apply(&mut universe).0)
.collect();
Self {
executor,
transactions,
}
}
fn execute(self) {
self.executor
.execute_block(self.transactions)
.expect("VM should not fail to start");
}
}
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)
}