Skip to content

Commit

Permalink
Use criterion for benchmarking (tikv#30)
Browse files Browse the repository at this point in the history
Signed-off-by: koushiro <[email protected]>
  • Loading branch information
koushiro authored Jul 6, 2020
1 parent a50530a commit 342c070
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install Rust nightly toolchain
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
toolchain: stable
override: true

- name: Run cargo test
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ prost = { version = "0.6", optional = true }
prost-derive = { version = "0.6", optional = true }

[dev-dependencies]
criterion = "0.3"
rand = "0.7.2"

[build-dependencies]
Expand All @@ -46,5 +47,10 @@ required-features = ["protobuf"]
name = "multithread_flamegraph"
required-features = ["flamegraph"]

[[bench]]
name = "collector"
path = "benches/collector.rs"
harness = false

[package.metadata.docs.rs]
all-features = true
43 changes: 43 additions & 0 deletions benches/collector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use criterion::{criterion_group, criterion_main, Criterion};
use pprof::{Collector, StackHashCounter};

fn bench_write_to_collector(c: &mut Criterion) {
c.bench_function("write_to_collector", |b| {
let mut collector = Collector::new().unwrap();

const SIZE: usize = 1000;

let mut vec: Vec<u64> = Vec::with_capacity(SIZE);
for _ in 0..vec.capacity() {
vec.push(rand::random());
}

b.iter(|| {
vec.iter().for_each(|item| {
collector.add(*item, 1).unwrap();
})
})
});

c.bench_function("write_into_stack_hash_counter", |b| {
let mut collector = StackHashCounter::default();

const SIZE: usize = 1000;

let mut vec: Vec<u64> = Vec::with_capacity(SIZE);
for _ in 0..vec.capacity() {
vec.push(rand::random());
}

b.iter(|| {
vec.iter().for_each(|item| {
collector.add(*item, 1);
})
});
});
}

criterion_group!(benches, bench_write_to_collector);
criterion_main!(benches);
37 changes: 0 additions & 37 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ mod tests {
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::ffi::c_void;
use test::Bencher;

#[test]
fn stack_hash_counter() {
Expand Down Expand Up @@ -409,40 +408,4 @@ mod tests {
}
}
}

#[bench]
fn write_into_collector(b: &mut Bencher) {
let mut collector = Collector::new().unwrap();

const SIZE: usize = 1000;

let mut vec: Vec<u64> = Vec::with_capacity(SIZE);
for _ in 0..vec.capacity() {
vec.push(rand::random());
}

b.iter(|| {
vec.iter().for_each(|item| {
collector.add(item.clone(), 1).unwrap();
});
});
}

#[bench]
fn write_into_stack_hash_counter(b: &mut Bencher) {
let mut collector = StackHashCounter::default();

const SIZE: usize = 1000;

let mut vec: Vec<u64> = Vec::with_capacity(SIZE);
for _ in 0..vec.capacity() {
vec.push(rand::random());
}

b.iter(|| {
vec.iter().for_each(|item| {
collector.add(item.clone(), 1);
});
});
}
}
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
//!};
//! ```

#![cfg_attr(test, feature(test))]

#[cfg(test)]
extern crate test;

/// Define the MAX supported stack depth. TODO: make this variable mutable.
pub const MAX_DEPTH: usize = 32;

Expand All @@ -39,6 +34,7 @@ mod profiler;
mod report;
mod timer;

pub use self::collector::{Collector, StackHashCounter};
pub use self::error::{Error, Result};
pub use self::frames::{Frames, Symbol};
pub use self::profiler::ProfilerGuard;
Expand Down

0 comments on commit 342c070

Please sign in to comment.