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/lib.rs | 1 + src/token_stream.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 35d975f..0ec71be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ //! Macros utilizing the [OpenGL API and Extension Registry]. //! //! [OpenGL API and Extension Registry]: https://github.com/KhronosGroup/OpenGL-Registry +#![cfg_attr(test, feature(test))] #![deny(clippy::all, clippy::pedantic, missing_docs)] use once_cell::sync::Lazy; use opengl_registry::Registry; 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