1 | // $ cargo bench --features full,test --bench file |
2 | |
3 | #![feature (rustc_private, test)] |
4 | #![recursion_limit = "1024" ] |
5 | #![allow ( |
6 | clippy::items_after_statements, |
7 | clippy::manual_let_else, |
8 | clippy::match_like_matches_macro, |
9 | clippy::missing_panics_doc, |
10 | clippy::must_use_candidate, |
11 | clippy::uninlined_format_args |
12 | )] |
13 | |
14 | extern crate test; |
15 | |
16 | #[macro_use ] |
17 | #[path = "../tests/macros/mod.rs" ] |
18 | mod macros; |
19 | |
20 | #[allow (dead_code)] |
21 | #[path = "../tests/repo/mod.rs" ] |
22 | mod repo; |
23 | |
24 | use proc_macro2::{Span, TokenStream}; |
25 | use std::fs; |
26 | use std::str::FromStr; |
27 | use syn::parse::{ParseStream, Parser}; |
28 | use test::Bencher; |
29 | |
30 | const FILE: &str = "tests/rust/library/core/src/str/mod.rs" ; |
31 | |
32 | fn get_tokens() -> TokenStream { |
33 | repo::clone_rust(); |
34 | let content = fs::read_to_string(FILE).unwrap(); |
35 | TokenStream::from_str(&content).unwrap() |
36 | } |
37 | |
38 | #[bench] |
39 | fn baseline(b: &mut Bencher) { |
40 | let tokens = get_tokens(); |
41 | b.iter(|| drop(tokens.clone())); |
42 | } |
43 | |
44 | #[bench] |
45 | fn create_token_buffer(b: &mut Bencher) { |
46 | let tokens = get_tokens(); |
47 | fn immediate_fail(_input: ParseStream) -> syn::Result<()> { |
48 | Err(syn::Error::new(Span::call_site(), "" )) |
49 | } |
50 | b.iter(|| immediate_fail.parse2(tokens.clone())); |
51 | } |
52 | |
53 | #[bench] |
54 | fn parse_file(b: &mut Bencher) { |
55 | let tokens = get_tokens(); |
56 | b.iter(|| syn::parse2::<syn::File>(tokens.clone())); |
57 | } |
58 | |