From bcb50c3d6de52207541969bfeba0701073c56232 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 5 Mar 2023 12:32:34 +0100 Subject: perf: add benchmarks for TokenStreamExt functions --- src/token_stream.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/token_stream.rs') diff --git a/src/token_stream.rs b/src/token_stream.rs index 043de89..5b755c9 100644 --- a/src/token_stream.rs +++ b/src/token_stream.rs @@ -163,8 +163,13 @@ mod index_path #[cfg(test)] mod tests { + extern crate test; + + use std::hint::black_box; + use proc_macro2::Span; - use quote::quote; + use quote::{format_ident, quote}; + use test::Bencher; use super::*; @@ -260,4 +265,45 @@ mod tests .to_string() ); } + + #[bench] + fn bench_find_all_ident(bencher: &mut Bencher) + { + let input_strem = quote! { + let foo = |abc| { + let x = "foo"; + + let y = a_foo; + + let foo = "Hello"; + + foo.to_string() + }; + }; + + bencher.iter(|| input_strem.find_all_ident(&black_box(format_ident!("foo")))); + } + + #[bench] + fn bench_replace_tokens(bencher: &mut Bencher) + { + let input_strem = quote! { + let foo = |abc| { + let x = "foo"; + + let y = a_foo; + + let foo = "Hello"; + + foo.to_string() + }; + }; + + bencher.iter(|| { + input_strem.replace_tokens( + black_box(&[[1].into(), [6, 11].into(), [6, 15].into()]), + black_box(&TokenTree::Ident(format_ident!("foobar"))), + ) + }); + } } -- cgit v1.2.3-18-g5258