Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add benchmarks for ASM & AOT VM #77

Merged
merged 3 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ matrix:
- git
- build-essential
env: SUITE=ci-generated
- rust: 1.35.0
addons:
apt:
packages:
- git
- build-essential
env: SUITE=check
- rust: 1.35.0
addons:
apt:
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ test:
test-all-features:
cargo test --all --features=asm -- --nocapture

check:
cargo check --all --all-targets --all-features

cov:
cargo clean
cargo build --tests --all --features=asm
Expand Down
10 changes: 5 additions & 5 deletions benches/bits_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ckb_vm::bits;
use criterion::Criterion;

#[inline(always)]
pub fn roundup_via_remainder(x: usize, round: usize) -> usize {
pub fn roundup_via_remainder(x: u64, round: u64) -> u64 {
let remainder = x % round;
if remainder > 0 {
x - remainder + round
Expand All @@ -15,13 +15,13 @@ pub fn roundup_via_remainder(x: usize, round: usize) -> usize {
}

#[inline(always)]
pub fn rounddown_via_remainder(x: usize, round: usize) -> usize {
pub fn rounddown_via_remainder(x: u64, round: u64) -> u64 {
let remainder = x % round;
x - remainder
}

#[inline(always)]
pub fn roundup_via_multiplication(x: usize, round: usize) -> usize {
pub fn roundup_via_multiplication(x: u64, round: u64) -> u64 {
if x == 0 {
0
} else {
Expand All @@ -30,11 +30,11 @@ pub fn roundup_via_multiplication(x: usize, round: usize) -> usize {
}

#[inline(always)]
pub fn rounddown_via_multiplication(x: usize, round: usize) -> usize {
pub fn rounddown_via_multiplication(x: u64, round: u64) -> u64 {
x / round * round
}

const ROUNDS: &[usize] = &[1, 2, 4, 8, 16, 32];
const ROUNDS: &[u64] = &[1, 2, 4, 8, 16, 32];

macro_rules! round_bench {
($f:expr) => {
Expand Down
74 changes: 73 additions & 1 deletion benches/vm_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
extern crate criterion;

use bytes::Bytes;
#[cfg(all(unix, target_pointer_width = "64", feature = "asm"))]
use ckb_vm::machine::{aot::AotCompilingMachine, asm::AsmMachine};
use ckb_vm::{run, SparseMemory};
use criterion::Criterion;
use std::fs::File;
Expand All @@ -24,5 +26,75 @@ fn interpret_benchmark(c: &mut Criterion) {
});
}

criterion_group!(benches, interpret_benchmark);
#[cfg(all(unix, target_pointer_width = "64", feature = "asm"))]
fn asm_benchmark(c: &mut Criterion) {
c.bench_function("interpret secp256k1_bench via assembly", |b| {
let mut file = File::open("benches/data/secp256k1_bench").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

let buffer = Bytes::from(buffer);
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();

b.iter(|| {
let mut machine = AsmMachine::default();
machine.load_program(&buffer, &args[..]).unwrap();
machine.run().unwrap()
});
});
}

#[cfg(all(unix, target_pointer_width = "64", feature = "asm"))]
fn aot_benchmark(c: &mut Criterion) {
c.bench_function("aot secp256k1_bench", |b| {
let mut file = File::open("benches/data/secp256k1_bench").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

let buffer = Bytes::from(buffer);
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();
let mut aot_machine = AotCompilingMachine::load(&buffer.clone(), None).unwrap();
let result = aot_machine.compile().unwrap();

b.iter(|| {
let mut machine = AsmMachine::default_with_aot_code(&result);
machine.load_program(&buffer, &args[..]).unwrap();
machine.run().unwrap()
});
});
}

#[cfg(all(unix, target_pointer_width = "64", feature = "asm"))]
fn aot_compiling_benchmark(c: &mut Criterion) {
c.bench_function("compiling secp256k1_bench for aot", |b| {
let mut file = File::open("benches/data/secp256k1_bench").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

let buffer = Bytes::from(buffer);

b.iter(|| {
AotCompilingMachine::load(&buffer.clone(), None)
.unwrap()
.compile()
.unwrap()
});
});
}

criterion_group!(
benches,
interpret_benchmark,
asm_benchmark,
aot_benchmark,
aot_compiling_benchmark
);
criterion_main!(benches);