From 62a7657a65726509cce21a5e369309f96f098e3b Mon Sep 17 00:00:00 2001 From: He1pa <18012015693@163.com> Date: Tue, 29 Aug 2023 14:50:39 +0800 Subject: [PATCH 1/2] test: add benchmarks to existing lsp tests --- kclvm/Cargo.lock | 38 +++++---- kclvm/tools/src/LSP/Cargo.toml | 1 + .../tools/src/LSP/proc_macro_crate/Cargo.toml | 14 ++++ .../tools/src/LSP/proc_macro_crate/src/lib.rs | 32 ++++++++ kclvm/tools/src/LSP/src/lib.rs | 1 + kclvm/tools/src/LSP/src/main.rs | 1 + kclvm/tools/src/LSP/src/test_utils.rs | 78 +++++++++++++++++++ kclvm/tools/src/LSP/src/tests.rs | 35 ++++++++- kclvm/tools/src/LSP/src/util.rs | 9 +-- 9 files changed, 187 insertions(+), 22 deletions(-) create mode 100644 kclvm/tools/src/LSP/proc_macro_crate/Cargo.toml create mode 100644 kclvm/tools/src/LSP/proc_macro_crate/src/lib.rs create mode 100644 kclvm/tools/src/LSP/src/test_utils.rs diff --git a/kclvm/Cargo.lock b/kclvm/Cargo.lock index 46cff478f..396095451 100644 --- a/kclvm/Cargo.lock +++ b/kclvm/Cargo.lock @@ -160,7 +160,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -748,7 +748,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -1020,7 +1020,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -1237,7 +1237,7 @@ source = "git+https://github.com/TheDan64/inkwell?branch=master#4030f764f1c889f3 dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -1411,6 +1411,7 @@ dependencies = [ "lsp-server", "lsp-types", "parking_lot 0.12.1", + "proc_macro_crate", "ra_ap_vfs", "ra_ap_vfs-notify", "ropey", @@ -2358,6 +2359,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proc_macro_crate" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.29", +] + [[package]] name = "prost" version = "0.11.9" @@ -3061,7 +3071,7 @@ checksum = "b23f7ade6f110613c0d63858ddb8b94c1041f550eab58a16b371bdf2c9c80ab4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -3083,7 +3093,7 @@ checksum = "e168eaaf71e8f9bd6037feb05190485708e019f4fd87d161b3c0a0d37daf85e5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -3307,9 +3317,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.27" +version = "2.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" dependencies = [ "proc-macro2", "quote", @@ -3392,7 +3402,7 @@ checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -3554,7 +3564,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -3624,7 +3634,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -3672,7 +3682,7 @@ checksum = "30c49a6815b4f8379c36f06618bc1b80ca77aaf8a3fd4d8549dca6fdb016000f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", ] [[package]] @@ -3924,7 +3934,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", "wasm-bindgen-shared", ] @@ -3946,7 +3956,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.29", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/kclvm/tools/src/LSP/Cargo.toml b/kclvm/tools/src/LSP/Cargo.toml index eef407743..fee50965a 100644 --- a/kclvm/tools/src/LSP/Cargo.toml +++ b/kclvm/tools/src/LSP/Cargo.toml @@ -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 = "./proc_macro_crate"} diff --git a/kclvm/tools/src/LSP/proc_macro_crate/Cargo.toml b/kclvm/tools/src/LSP/proc_macro_crate/Cargo.toml new file mode 100644 index 000000000..a112f709b --- /dev/null +++ b/kclvm/tools/src/LSP/proc_macro_crate/Cargo.toml @@ -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"] } diff --git a/kclvm/tools/src/LSP/proc_macro_crate/src/lib.rs b/kclvm/tools/src/LSP/proc_macro_crate/src/lib.rs new file mode 100644 index 000000000..6452dc46b --- /dev/null +++ b/kclvm/tools/src/LSP/proc_macro_crate/src/lib.rs @@ -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() +} diff --git a/kclvm/tools/src/LSP/src/lib.rs b/kclvm/tools/src/LSP/src/lib.rs index 75677bf7f..2dadac9a3 100644 --- a/kclvm/tools/src/LSP/src/lib.rs +++ b/kclvm/tools/src/LSP/src/lib.rs @@ -16,3 +16,4 @@ mod goto_def; mod hover; mod quick_fix; mod request; +mod test_utils; diff --git a/kclvm/tools/src/LSP/src/main.rs b/kclvm/tools/src/LSP/src/main.rs index 196c693ac..1bb0a90c9 100644 --- a/kclvm/tools/src/LSP/src/main.rs +++ b/kclvm/tools/src/LSP/src/main.rs @@ -16,6 +16,7 @@ mod notification; mod quick_fix; mod request; mod state; +mod test_utils; mod to_lsp; mod util; diff --git a/kclvm/tools/src/LSP/src/test_utils.rs b/kclvm/tools/src/LSP/src/test_utils.rs new file mode 100644 index 000000000..4bfdf10c3 --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_utils.rs @@ -0,0 +1,78 @@ +use std::{ + fmt, + time::{Duration, Instant}, +}; + +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, + } +} diff --git a/kclvm/tools/src/LSP/src/tests.rs b/kclvm/tools/src/LSP/src/tests.rs index 1993a329c..24cfade5a 100644 --- a/kclvm/tools/src/LSP/src/tests.rs +++ b/kclvm/tools/src/LSP/src/tests.rs @@ -30,13 +30,12 @@ use lsp_types::Url; use lsp_types::WorkspaceEdit; use lsp_types::{Position, Range, TextDocumentContentChangeEvent}; use parking_lot::RwLock; -use ra_ap_vfs::AbsPathBuf; -use ra_ap_vfs::Vfs; +use proc_macro_crate::bench_test; use crate::completion::KCLCompletionItem; use crate::document_symbol::document_symbol; use crate::formatting::format; -use crate::from_lsp::{abs_path, file_path_from_url, text_range}; +use crate::from_lsp::{file_path_from_url, text_range}; use crate::hover::hover; use crate::quick_fix::quick_fix; use crate::to_lsp::kcl_diag_to_lsp_diags; @@ -88,6 +87,7 @@ fn compare_goto_res(res: Option, pos: (&String, u32, } #[test] +#[bench_test] fn diagnostics_test() { fn build_lsp_diag( pos: (u32, u32, u32, u32), @@ -230,6 +230,7 @@ fn diagnostics_test() { } #[test] +#[bench_test] fn quick_fix_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let mut test_file = path.clone(); @@ -322,6 +323,7 @@ fn quick_fix_test() { } #[test] +#[bench_test] fn goto_import_pkg_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let (file, program, prog_scope, _) = @@ -357,6 +359,7 @@ fn goto_import_pkg_test() { } #[test] +#[bench_test] fn goto_import_file_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -385,6 +388,7 @@ fn goto_import_file_test() { } #[test] +#[bench_test] fn goto_pkg_prefix_def_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -422,6 +426,7 @@ fn goto_pkg_prefix_def_test() { } #[test] +#[bench_test] fn goto_schema_def_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -445,6 +450,7 @@ fn goto_schema_def_test() { } #[test] +#[bench_test] fn goto_var_def_in_config_and_config_if_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -488,6 +494,7 @@ fn goto_var_def_in_config_and_config_if_test() { } #[test] +#[bench_test] fn goto_var_def_in_dict_comp_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -545,6 +552,7 @@ fn goto_dict_key_def_test() { } #[test] +#[bench_test] fn goto_schema_attr_def_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -568,6 +576,7 @@ fn goto_schema_attr_def_test() { } #[test] +#[bench_test] fn goto_schema_attr_def_test1() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -591,6 +600,7 @@ fn goto_schema_attr_def_test1() { } #[test] +#[bench_test] fn test_goto_identifier_names() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -638,6 +648,7 @@ fn test_goto_identifier_names() { } #[test] +#[bench_test] fn goto_identifier_def_test() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/goto_def_test/goto_def.k"); @@ -653,6 +664,7 @@ fn goto_identifier_def_test() { } #[test] +#[bench_test] fn goto_assign_type_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -674,6 +686,7 @@ fn goto_assign_type_test() { } #[test] +#[bench_test] fn goto_schema_attr_ty_def_test() { // test goto schema attr type definition: p1: pkg.Person let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -697,6 +710,7 @@ fn goto_schema_attr_ty_def_test() { } #[test] +#[bench_test] fn goto_schema_attr_ty_def_test1() { // test goto schema attr type definition: p2: [pkg.Person] let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -720,6 +734,7 @@ fn goto_schema_attr_ty_def_test1() { } #[test] +#[bench_test] fn goto_schema_attr_ty_def_test3() { // test goto schema attr type definition: p3: {str: pkg.Person} let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -743,6 +758,7 @@ fn goto_schema_attr_ty_def_test3() { } #[test] +#[bench_test] fn goto_schema_attr_ty_def_test4() { // test goto schema attr type definition(Person): p4: pkg.Person | pkg.Person1 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -766,6 +782,7 @@ fn goto_schema_attr_ty_def_test4() { } #[test] +#[bench_test] fn goto_schema_attr_ty_def_test5() { // test goto schema attr type definition(Person1): p4: pkg.Person | pkg.Person1 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -789,6 +806,7 @@ fn goto_schema_attr_ty_def_test5() { } #[test] +#[bench_test] fn goto_local_var_def_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -828,6 +846,7 @@ fn goto_local_var_def_test() { } #[test] +#[bench_test] fn test_apply_document_changes() { macro_rules! change { [$($sl:expr, $sc:expr; $el:expr, $ec:expr => $text:expr),+] => { @@ -908,6 +927,7 @@ fn test_apply_document_changes() { } #[test] +#[bench_test] fn var_completion_test() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/completion_test/dot/completion.k"); @@ -966,6 +986,7 @@ fn var_completion_test() { } #[test] +#[bench_test] fn dot_completion_test() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/completion_test/dot/completion.k"); @@ -1082,6 +1103,7 @@ fn dot_completion_test() { } #[test] +#[bench_test] fn schema_doc_hover_test() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -1132,6 +1154,7 @@ fn schema_doc_hover_test() { } #[test] +#[bench_test] fn schema_doc_hover_test1() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/hover_test/hover.k"); @@ -1162,6 +1185,7 @@ fn schema_doc_hover_test1() { } #[test] +#[bench_test] fn schema_attr_hover_test() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/hover_test/hover.k"); @@ -1235,6 +1259,7 @@ fn build_document_symbol( } #[test] +#[bench_test] fn document_symbol_test() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/document_symbol.k"); @@ -1265,6 +1290,7 @@ fn document_symbol_test() { } #[test] +#[bench_test] fn file_path_from_url_test() { if cfg!(windows) { let url = @@ -1279,6 +1305,7 @@ fn file_path_from_url_test() { } #[test] +#[bench_test] fn goto_import_external_file_test() { let path = PathBuf::from(".") .join("src") @@ -1327,6 +1354,7 @@ 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"; @@ -1399,6 +1427,7 @@ fn format_signle_file_test() { } #[test] +#[bench_test] fn format_range_test() { let (file, program, prog_scope, _) = compile_test_file("src/test_data/format/format_range.k"); let lsp_range = Range::new(Position::new(0, 0), Position::new(11, 0)); diff --git a/kclvm/tools/src/LSP/src/util.rs b/kclvm/tools/src/LSP/src/util.rs index 23bae4603..d6f180ec4 100644 --- a/kclvm/tools/src/LSP/src/util.rs +++ b/kclvm/tools/src/LSP/src/util.rs @@ -1,8 +1,3 @@ -use std::cell::RefCell; -use std::path::{Path, PathBuf}; -use std::rc::Rc; -use std::{fs, sync::Arc}; - use indexmap::{IndexMap, IndexSet}; use kclvm_ast::ast::{ ConfigEntry, Expr, Identifier, Node, NodeRef, PosTuple, Program, SchemaExpr, Stmt, Type, @@ -24,6 +19,10 @@ use lsp_types::Url; use parking_lot::{RwLock, RwLockReadGuard}; use ra_ap_vfs::{FileId, Vfs}; use serde::{de::DeserializeOwned, Serialize}; +use std::cell::RefCell; +use std::path::{Path, PathBuf}; +use std::rc::Rc; +use std::{fs, sync::Arc}; use crate::from_lsp; From 75b039e5a1c3b4943d5dfd71f0245ef9bcb3b868 Mon Sep 17 00:00:00 2001 From: He1pa <18012015693@163.com> Date: Wed, 30 Aug 2023 16:35:57 +0800 Subject: [PATCH 2/2] feat: move bench() method and bench test proc macro to toools/benches --- kclvm/tools/benches/benchmark.rs | 78 +++++++++++++++++++ .../proc_macro_crate/Cargo.toml | 0 .../proc_macro_crate/src/lib.rs | 0 kclvm/tools/src/LSP/Cargo.toml | 2 +- kclvm/tools/src/LSP/src/lib.rs | 1 - kclvm/tools/src/LSP/src/main.rs | 1 - kclvm/tools/src/LSP/src/test_utils.rs | 78 ------------------- kclvm/tools/src/LSP/src/tests.rs | 2 - 8 files changed, 79 insertions(+), 83 deletions(-) rename kclvm/tools/{src/LSP => benches}/proc_macro_crate/Cargo.toml (100%) rename kclvm/tools/{src/LSP => benches}/proc_macro_crate/src/lib.rs (100%) delete mode 100644 kclvm/tools/src/LSP/src/test_utils.rs diff --git a/kclvm/tools/benches/benchmark.rs b/kclvm/tools/benches/benchmark.rs index f9e7d64e6..0a8899c6e 100644 --- a/kclvm/tools/benches/benchmark.rs +++ b/kclvm/tools/benches/benchmark.rs @@ -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| { @@ -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, + } +} diff --git a/kclvm/tools/src/LSP/proc_macro_crate/Cargo.toml b/kclvm/tools/benches/proc_macro_crate/Cargo.toml similarity index 100% rename from kclvm/tools/src/LSP/proc_macro_crate/Cargo.toml rename to kclvm/tools/benches/proc_macro_crate/Cargo.toml diff --git a/kclvm/tools/src/LSP/proc_macro_crate/src/lib.rs b/kclvm/tools/benches/proc_macro_crate/src/lib.rs similarity index 100% rename from kclvm/tools/src/LSP/proc_macro_crate/src/lib.rs rename to kclvm/tools/benches/proc_macro_crate/src/lib.rs diff --git a/kclvm/tools/src/LSP/Cargo.toml b/kclvm/tools/src/LSP/Cargo.toml index fee50965a..7784630ae 100644 --- a/kclvm/tools/src/LSP/Cargo.toml +++ b/kclvm/tools/src/LSP/Cargo.toml @@ -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"} diff --git a/kclvm/tools/src/LSP/src/lib.rs b/kclvm/tools/src/LSP/src/lib.rs index 2dadac9a3..75677bf7f 100644 --- a/kclvm/tools/src/LSP/src/lib.rs +++ b/kclvm/tools/src/LSP/src/lib.rs @@ -16,4 +16,3 @@ mod goto_def; mod hover; mod quick_fix; mod request; -mod test_utils; diff --git a/kclvm/tools/src/LSP/src/main.rs b/kclvm/tools/src/LSP/src/main.rs index 1bb0a90c9..196c693ac 100644 --- a/kclvm/tools/src/LSP/src/main.rs +++ b/kclvm/tools/src/LSP/src/main.rs @@ -16,7 +16,6 @@ mod notification; mod quick_fix; mod request; mod state; -mod test_utils; mod to_lsp; mod util; diff --git a/kclvm/tools/src/LSP/src/test_utils.rs b/kclvm/tools/src/LSP/src/test_utils.rs deleted file mode 100644 index 4bfdf10c3..000000000 --- a/kclvm/tools/src/LSP/src/test_utils.rs +++ /dev/null @@ -1,78 +0,0 @@ -use std::{ - fmt, - time::{Duration, Instant}, -}; - -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, - } -} diff --git a/kclvm/tools/src/LSP/src/tests.rs b/kclvm/tools/src/LSP/src/tests.rs index 24cfade5a..6c7104fba 100644 --- a/kclvm/tools/src/LSP/src/tests.rs +++ b/kclvm/tools/src/LSP/src/tests.rs @@ -1305,7 +1305,6 @@ fn file_path_from_url_test() { } #[test] -#[bench_test] fn goto_import_external_file_test() { let path = PathBuf::from(".") .join("src") @@ -1354,7 +1353,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";