Skip to content

Commit

Permalink
feat: move bench() method and bench test proc macro to toools/benches
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Aug 30, 2023
1 parent 22fe768 commit 0e75a26
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 83 deletions.
78 changes: 78 additions & 0 deletions kclvm/tools/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kclvm_query::override_file;
use kclvm_tools::format::{format, FormatOptions};
use std::{
fmt,
time::{Duration, Instant},
};

pub fn criterion_benchmark_override(c: &mut Criterion) {
c.bench_function("override", |b| {
Expand Down Expand Up @@ -29,3 +33,77 @@ criterion_group!(
criterion_benchmark_format
);
criterion_main!(benches);

pub struct StopWatch {
time: Instant,
}

pub struct StopWatchSpan {
pub time: Duration,
}

impl StopWatch {
pub fn start() -> StopWatch {
let time = Instant::now();
StopWatch { time }
}

pub fn elapsed(&mut self) -> StopWatchSpan {
let time = self.time.elapsed();

StopWatchSpan { time }
}
}

impl fmt::Display for StopWatchSpan {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.2?}", self.time)?;
Ok(())
}
}

/// Utility for writing benchmark tests.
///
/// If you need to benchmark the entire test, you can directly add the macro `#[bench_test]` like this:
/// ```
/// #[test]
/// #[bench_test]
/// fn benchmark_foo() {
/// actual_work(analysis)
/// }
/// ```
///
/// If you need to skip some preparation stages and only test some parts of test, you can use the `bench()` method.
/// A benchmark test looks like this:
///
/// ```
/// #[test]
/// fn benchmark_foo() {
/// let data = bench_fixture::some_fixture();
/// let analysis = some_setup();
///
/// {
/// let _b = bench("foo");
/// actual_work(analysis)
/// };
/// }
/// ```
///
///
pub fn bench(label: &'static str) -> impl Drop {
struct Bencher {
sw: StopWatch,
label: &'static str,
}

impl Drop for Bencher {
fn drop(&mut self) {
eprintln!("{}: {}", self.label, self.sw.elapsed());
}
}

Bencher {
sw: StopWatch::start(),
label,
}
}
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ salsa = { version = "0.16.1", default-features = false }
serde_json = { version = "1.0", default-features = false }
parking_lot = { version = "0.12.0", default-features = false }
rustc-hash = { version = "1.1.0", default-features = false }
proc_macro_crate = {path = "./proc_macro_crate"}
proc_macro_crate = {path = "../../benches/proc_macro_crate"}
1 change: 0 additions & 1 deletion kclvm/tools/src/LSP/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ mod goto_def;
mod hover;
mod quick_fix;
mod request;
mod test_utils;
1 change: 0 additions & 1 deletion kclvm/tools/src/LSP/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ mod notification;
mod quick_fix;
mod request;
mod state;
mod test_utils;
mod to_lsp;
mod util;

Expand Down
78 changes: 0 additions & 78 deletions kclvm/tools/src/LSP/src/test_utils.rs

This file was deleted.

2 changes: 0 additions & 2 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,6 @@ fn file_path_from_url_test() {
}

#[test]
#[bench_test]
fn goto_import_external_file_test() {
let path = PathBuf::from(".")
.join("src")
Expand Down Expand Up @@ -1283,7 +1282,6 @@ fn goto_import_external_file_test() {
}

#[test]
#[bench_test]
fn format_signle_file_test() {
const FILE_INPUT_SUFFIX: &str = ".input";
const FILE_OUTPUT_SUFFIX: &str = ".golden";
Expand Down

0 comments on commit 0e75a26

Please sign in to comment.