-
Notifications
You must be signed in to change notification settings - Fork 622
/
v2.rs
311 lines (259 loc) · 9.16 KB
/
v2.rs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::time::Duration;
use std::{fmt, iter, ops};
use near_crypto::{InMemorySigner, KeyType};
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::{
Action, CreateAccountAction, DeleteAccountAction, SignedTransaction, TransferAction,
};
use near_primitives::types::{AccountId, Gas};
use rand::rngs::ThreadRng;
use rand::Rng;
use crate::cases::ratio_to_gas;
use crate::testbed::RuntimeTestbed;
use crate::testbed_runners::{end_count, get_account_id, start_count, Config, GasMetric};
use crate::{Cost, CostTable};
static ALL_COSTS: &[(Cost, fn(&mut Ctx) -> GasCost)] = &[
(Cost::ActionReceiptCreation, action_receipt_creation),
(Cost::ActionSirReceiptCreation, action_sir_receipt_creation),
(Cost::ActionTransfer, action_transfer),
(Cost::ActionCreateAccount, action_create_account),
(Cost::ActionDeleteAccount, action_delete_account),
];
pub fn run(config: Config) -> CostTable {
let mut ctx = Ctx::new(&config);
let mut res = CostTable::default();
for (cost, f) in ALL_COSTS.iter().copied() {
let skip = match &ctx.config.metrics_to_measure {
None => false,
Some(costs) => !costs.contains(&format!("{:?}", cost)),
};
if skip {
continue;
}
let value = f(&mut ctx);
res.add(cost, value.to_gas())
}
res
}
fn action_receipt_creation(ctx: &mut Ctx) -> GasCost {
if let Some(cached) = ctx.cached.action_receipt_creation.clone() {
return cached;
}
let mut testbed = ctx.test_bed();
let mut make_transaction = |tb: &mut TestBed| -> SignedTransaction {
let sender = tb.random_account();
let receiver = tb.random_account();
tb.transaction_from_actions(sender, receiver, vec![])
};
let cost = testbed.average_transaction_cost(&mut make_transaction);
ctx.cached.action_receipt_creation = Some(cost.clone());
cost
}
fn action_sir_receipt_creation(ctx: &mut Ctx) -> GasCost {
if let Some(cached) = ctx.cached.action_sir_receipt_creation.clone() {
return cached;
}
let mut testbed = ctx.test_bed();
let mut make_transaction = |tb: &mut TestBed<'_>| -> SignedTransaction {
let sender = tb.random_account();
let receiver = sender.clone();
tb.transaction_from_actions(sender, receiver, vec![])
};
let cost = testbed.average_transaction_cost(&mut make_transaction);
ctx.cached.action_sir_receipt_creation = Some(cost.clone());
cost
}
fn action_transfer(ctx: &mut Ctx) -> GasCost {
let mut testbed = ctx.test_bed();
let mut make_transaction = |tb: &mut TestBed<'_>| -> SignedTransaction {
let sender = tb.random_account();
let receiver = tb.random_account();
tb.transaction_from_actions(
sender,
receiver,
vec![Action::Transfer(TransferAction { deposit: 1 })],
)
};
testbed.average_transaction_cost(&mut make_transaction)
}
fn action_create_account(ctx: &mut Ctx) -> GasCost {
let total_cost = {
let mut testbed = ctx.test_bed();
let mut make_transaction = |tb: &mut TestBed<'_>| -> SignedTransaction {
let existing_account = tb.random_account();
let new_account =
AccountId::try_from(format!("{}_{}", existing_account, tb.rng().gen::<u64>()))
.unwrap();
tb.transaction_from_actions(
existing_account,
new_account,
vec![
Action::CreateAccount(CreateAccountAction {}),
Action::Transfer(TransferAction { deposit: 10u128.pow(26) }),
],
)
};
testbed.average_transaction_cost(&mut make_transaction)
};
let base_cost = action_receipt_creation(ctx);
total_cost - base_cost
}
fn action_delete_account(ctx: &mut Ctx) -> GasCost {
let total_cost = {
let mut testbed = ctx.test_bed();
let mut deleted_accounts = HashSet::new();
let mut beneficiaries = HashSet::new();
let mut make_transaction = |tb: &mut TestBed<'_>| -> SignedTransaction {
let existing_account = tb
.random_accounts()
.find(|it| !deleted_accounts.contains(it) && !beneficiaries.contains(it))
.unwrap();
deleted_accounts.insert(existing_account.clone());
let beneficiary_id =
tb.random_accounts().find(|it| !deleted_accounts.contains(it)).unwrap();
beneficiaries.insert(beneficiary_id.clone());
tb.transaction_from_actions(
existing_account.clone(),
existing_account,
vec![Action::DeleteAccount(DeleteAccountAction { beneficiary_id })],
)
};
testbed.average_transaction_cost(&mut make_transaction)
};
let base_cost = action_sir_receipt_creation(ctx);
total_cost - base_cost
}
#[derive(Default)]
struct CachedCosts {
action_receipt_creation: Option<GasCost>,
action_sir_receipt_creation: Option<GasCost>,
}
struct Ctx<'a> {
config: &'a Config,
cached: CachedCosts,
}
impl<'a> Ctx<'a> {
fn new(config: &'a Config) -> Self {
let cached = CachedCosts::default();
Self { cached, config }
}
fn test_bed(&mut self) -> TestBed<'_> {
let inner = RuntimeTestbed::from_state_dump(&self.config.state_dump_path);
TestBed { config: &self.config, inner, nonces: HashMap::new() }
}
}
struct TestBed<'a> {
config: &'a Config,
inner: RuntimeTestbed,
nonces: HashMap<AccountId, u64>,
}
impl TestBed<'_> {
fn rng(&mut self) -> ThreadRng {
rand::thread_rng()
}
fn nonce(&mut self, account_id: &AccountId) -> u64 {
let nonce = self.nonces.entry(account_id.clone()).or_default();
*nonce += 1;
*nonce
}
fn random_account(&mut self) -> AccountId {
self.random_accounts().next().unwrap()
}
fn random_accounts<'a>(&'a mut self) -> impl Iterator<Item = AccountId> + 'a {
let active_accounts = self.config.active_accounts;
let mut rng = self.rng();
iter::repeat_with(move || {
let account_index = rng.gen_range(0, active_accounts);
get_account_id(account_index)
})
}
fn transaction_from_actions(
&mut self,
sender: AccountId,
receiver: AccountId,
actions: Vec<Action>,
) -> SignedTransaction {
let signer = InMemorySigner::from_seed(sender.clone(), KeyType::ED25519, sender.as_ref());
let nonce = self.nonce(&sender);
SignedTransaction::from_actions(
nonce as u64,
sender.clone(),
receiver,
&signer,
actions,
CryptoHash::default(),
)
}
fn average_transaction_cost(
&mut self,
make_transaction: &mut dyn FnMut(&mut Self) -> SignedTransaction,
) -> GasCost {
let allow_failures = false;
if self.config.warmup_iters_per_block > 0 {
let block_size = 100;
for _ in 0..self.config.warmup_iters_per_block {
let block: Vec<_> =
iter::repeat_with(|| make_transaction(self)).take(block_size).collect();
self.inner.process_block(&block, allow_failures);
}
self.inner.process_blocks_until_no_receipts(allow_failures);
}
let mut total = 0;
let mut n = 0;
for _ in 0..self.config.iter_per_block {
let block_size = 100;
let block: Vec<_> =
iter::repeat_with(|| make_transaction(self)).take(block_size).collect();
let start = start_count(self.config.metric);
self.inner.process_block(&block, allow_failures);
self.inner.process_blocks_until_no_receipts(allow_failures);
let measured = end_count(self.config.metric, &start);
total += measured;
n += block_size as u64;
}
GasCost { value: total / n, metric: self.config.metric }
}
}
#[derive(Clone)]
struct GasCost {
value: u64,
metric: GasMetric,
}
impl fmt::Debug for GasCost {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.metric {
GasMetric::ICount => write!(f, "{}i", self.value),
GasMetric::Time => fmt::Debug::fmt(&Duration::from_nanos(self.value), f),
}
}
}
impl ops::Sub for GasCost {
type Output = GasCost;
fn sub(self, rhs: GasCost) -> Self::Output {
assert_eq!(self.metric, rhs.metric);
GasCost { value: self.value - rhs.value, metric: self.metric }
}
}
impl GasCost {
fn to_gas(self) -> Gas {
ratio_to_gas(self.metric, self.value.into())
}
}
#[test]
fn smoke() {
use nearcore::get_default_home;
let config = Config {
warmup_iters_per_block: 1,
iter_per_block: 2,
active_accounts: 20000,
block_sizes: vec![100],
state_dump_path: get_default_home().into(),
metric: GasMetric::Time,
vm_kind: near_vm_runner::VMKind::Wasmer0,
metrics_to_measure: None,
};
let table = run(config);
eprintln!("{}", table);
}