From edaf24a2ce230a2adf85ae1ab39a0bad80001b0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 11 Jul 2016 12:15:22 +0200 Subject: [PATCH] EVM bin benches --- evmbin/Cargo.toml | 8 ++++++ evmbin/benches/mod.rs | 61 +++++++++++++++++++++++++++++++++++++++++++ evmbin/src/main.rs | 29 +++++++++++++++++--- 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 evmbin/benches/mod.rs diff --git a/evmbin/Cargo.toml b/evmbin/Cargo.toml index 3e531f5d3..8ec687687 100644 --- a/evmbin/Cargo.toml +++ b/evmbin/Cargo.toml @@ -4,6 +4,14 @@ description = "Parity's EVM implementation" version = "0.1.0" authors = ["Ethcore "] +[lib] +name = "evm" +path = "./src/main.rs" + +[[bin]] +name = "evm" +path = "./src/main.rs" + [dependencies] rustc-serialize = "0.3" docopt = { version = "0.6" } diff --git a/evmbin/benches/mod.rs b/evmbin/benches/mod.rs new file mode 100644 index 000000000..3013a170a --- /dev/null +++ b/evmbin/benches/mod.rs @@ -0,0 +1,61 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! benchmarking for EVM +//! should be started with: +//! ```bash +//! multirust run nightly cargo bench +//! ``` + +#![feature(test)] + +extern crate test; +extern crate ethcore; +extern crate evm; +extern crate ethcore_util; +extern crate rustc_serialize; + +use self::test::{Bencher, black_box}; + +use evm::run_vm; +use ethcore::action_params::ActionParams; +use ethcore_util::{U256, Uint}; +use rustc_serialize::hex::FromHex; + +#[bench] +fn simple_loop_usize(b: &mut Bencher) { + simple_loop(U256::from(::std::usize::MAX), b) +} + +#[bench] +fn simple_loop_u256(b: &mut Bencher) { + simple_loop(!U256::zero(), b) +} + +fn simple_loop(gas: U256, b: &mut Bencher) { + let code = black_box( + "606060405260005b620f42408112156019575b6001016007565b600081905550600680602b6000396000f3606060405200".from_hex().unwrap() + ); + + b.iter(|| { + let mut params = ActionParams::default(); + params.gas = gas; + params.code = Some(code.clone()); + + run_vm(params) + }); +} + diff --git a/evmbin/src/main.rs b/evmbin/src/main.rs index 3fa06d004..240a02ccf 100644 --- a/evmbin/src/main.rs +++ b/evmbin/src/main.rs @@ -25,7 +25,7 @@ extern crate ethcore_util as util; mod ext; -use std::time::Instant; +use std::time::{Instant, Duration}; use std::str::FromStr; use docopt::Docopt; use util::{U256, FromHex, Uint, Bytes}; @@ -58,6 +58,15 @@ fn main() { params.code = Some(args.code()); params.data = args.data(); + let result = run_vm(params); + println!("Gas used: {:?}", result.gas_used); + println!("Output: {:?}", result.output); + println!("Time: {}.{:.9}s", result.time.as_secs(), result.time.subsec_nanos()); +} + +/// Execute VM with given `ActionParams` +pub fn run_vm(params: ActionParams) -> ExecutionResults { + let initial_gas = params.gas; let factory = Factory::new(VMType::Interpreter); let mut vm = factory.create(params.gas); let mut ext = ext::FakeExt::default(); @@ -66,9 +75,21 @@ fn main() { let gas_left = vm.exec(params, &mut ext).finalize(ext).expect("OK"); let duration = start.elapsed(); - println!("Gas used: {:?}", args.gas() - gas_left); - println!("Output: {:?}", ""); - println!("Time: {}.{:.9}s", duration.as_secs(), duration.subsec_nanos()); + ExecutionResults { + gas_used: initial_gas - gas_left, + output: Vec::new(), + time: duration, + } +} + +/// VM execution results +pub struct ExecutionResults { + /// Used gas + pub gas_used: U256, + /// Output as bytes + pub output: Vec, + /// Time Taken + pub time: Duration, } #[derive(Debug, RustcDecodable)]