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