1 // $ cargo bench --features full --bench rust
3 // Syn only, useful for profiling:
4 // $ RUSTFLAGS='--cfg syn_only' cargo build --release --features full --bench rust
6 #![cfg_attr(not(syn_only), feature(rustc_private))]
7 #![recursion_limit = "1024"]
8 #![allow(clippy::cast_lossless, clippy::unnecessary_wraps)]
11 #[path = "../tests/macros/mod.rs"]
14 #[path = "../tests/common/mod.rs"]
16 #[path = "../tests/repo/mod.rs"]
20 use std
::time
::{Duration, Instant}
;
23 mod tokenstream_parse
{
24 use proc_macro2
::TokenStream
;
25 use std
::str::FromStr
;
27 pub fn bench(content
: &str) -> Result
<(), ()> {
28 TokenStream
::from_str(content
).map(drop
).map_err(drop
)
33 pub fn bench(content
: &str) -> Result
<(), ()> {
34 syn
::parse_file(content
).map(drop
).map_err(drop
)
40 extern crate rustc_data_structures
;
41 extern crate rustc_errors
;
42 extern crate rustc_parse
;
43 extern crate rustc_session
;
44 extern crate rustc_span
;
46 use rustc_data_structures
::sync
::Lrc
;
47 use rustc_errors
::{emitter::Emitter, Diagnostic, Handler}
;
48 use rustc_session
::parse
::ParseSess
;
49 use rustc_span
::source_map
::{FilePathMapping, SourceMap}
;
50 use rustc_span
::{edition::Edition, FileName}
;
52 pub fn bench(content
: &str) -> Result
<(), ()> {
55 impl Emitter
for SilentEmitter
{
56 fn emit_diagnostic(&mut self, _diag
: &Diagnostic
) {}
57 fn source_map(&self) -> Option
<&Lrc
<SourceMap
>> {
62 rustc_span
::create_session_if_not_set_then(Edition
::Edition2018
, |_
| {
63 let cm
= Lrc
::new(SourceMap
::new(FilePathMapping
::empty()));
64 let emitter
= Box
::new(SilentEmitter
);
65 let handler
= Handler
::with_emitter(false, None
, emitter
);
66 let sess
= ParseSess
::with_span_handler(handler
, cm
);
67 if let Err(diagnostic
) = rustc_parse
::parse_crate_from_source_str(
68 FileName
::Custom("bench".to_owned()),
82 pub fn bench(content
: &str) -> Result
<(), ()> {
88 fn exec(mut codepath
: impl FnMut(&str) -> Result
<(), ()>) -> Duration
{
89 let begin
= Instant
::now();
93 walkdir
::WalkDir
::new("tests/rust/src")
95 .filter_entry(repo
::base_dir_filter
)
97 let entry
= entry
.unwrap();
98 let path
= entry
.path();
102 let content
= fs
::read_to_string(path
).unwrap();
103 let ok
= codepath(&content
).is_ok();
104 success
+= ok
as usize;
107 eprintln
!("FAIL {}", path
.display());
111 assert_eq
!(success
, total
);
118 macro_rules
! testcases
{
119 ($
($
(#[$cfg:meta])* $name:ident,)*) => {
123 (stringify
!($name
), $name
::bench
as fn(&str) -> Result
<(), ()>),
129 #[cfg(not(syn_only))]
134 lines
+= content
.lines().count();
138 eprintln
!("\n{} lines in {} files", lines
, files
);
141 for (name
, f
) in testcases
!(
142 #[cfg(not(syn_only))]
144 #[cfg(not(syn_only))]
147 #[cfg(not(syn_only))]
150 eprint
!("{:20}", format
!("{}:", name
));
151 let elapsed
= exec(f
);
155 elapsed
.subsec_millis(),