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

test: add benchmarks to existing lsp tests #685

Merged
merged 2 commits into from
Aug 30, 2023
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
38 changes: 24 additions & 14 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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,
}
}
14 changes: 14 additions & 0 deletions kclvm/tools/benches/proc_macro_crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "proc_macro_crate"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.7"
quote = "1"
syn = { version = "2.0.29", features = ["full","extra-traits"] }
32 changes: 32 additions & 0 deletions kclvm/tools/benches/proc_macro_crate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn bench_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut input_fn = parse_macro_input!(item as ItemFn);

let fn_name = &input_fn.sig.ident;
let fn_body = &input_fn.block;

let timing_code = quote! {
{
let start_time = std::time::Instant::now();
let result = #fn_body;
let end_time = std::time::Instant::now();
let time = (end_time - start_time).as_micros();
println!("{} took {} μs", stringify!(#fn_name), (end_time - start_time).as_micros());
// 200 ms
assert!(time < 200000, "Bench mark test failed");
result
}
};

input_fn.block = Box::new(syn::parse2(timing_code).unwrap());

let output = quote! {
#input_fn
};

output.into()
}
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +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 = "../../benches/proc_macro_crate"}
Loading
Loading