Add a benchmark of TokenStream to File
diff --git a/Cargo.toml b/Cargo.toml
index a394b8d..abb5495 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -49,6 +49,11 @@
harness = false
required-features = ["full", "parsing"]
+[[bench]]
+name = "file"
+edition = "2018"
+required-features = ["full", "parsing"]
+
[package.metadata.docs.rs]
all-features = true
diff --git a/benches/file.rs b/benches/file.rs
new file mode 100644
index 0000000..5bd724f
--- /dev/null
+++ b/benches/file.rs
@@ -0,0 +1,28 @@
+// $ cargo bench --features full --bench file
+
+#![recursion_limit = "256"]
+#![feature(rustc_private, test)]
+
+extern crate test;
+
+#[macro_use]
+#[path = "../tests/macros/mod.rs"]
+mod macros;
+
+#[path = "../tests/common/mod.rs"]
+mod common;
+
+use test::Bencher;
+use proc_macro2::TokenStream;
+use std::fs;
+use std::str::FromStr;
+
+const FILE: &str = "tests/rust/src/libcore/str/mod.rs";
+
+#[bench]
+fn parse_file(b: &mut Bencher) {
+ common::clone_rust();
+ let content = fs::read_to_string(FILE).unwrap();
+ let tokens = TokenStream::from_str(&content).unwrap();
+ b.iter(|| syn::parse2::<syn::File>(tokens.clone()));
+}
diff --git a/benches/rust.rs b/benches/rust.rs
index 2a5005e..ae672a9 100644
--- a/benches/rust.rs
+++ b/benches/rust.rs
@@ -1,4 +1,4 @@
-// $ cargo bench --features full
+// $ cargo bench --features full --bench rust
#![recursion_limit = "256"]
#![feature(rustc_private)]
@@ -16,6 +16,7 @@
use proc_macro2::TokenStream;
use rustc_data_structures::sync::Lrc;
+use std::fs;
use std::str::FromStr;
use std::time::{Duration, Instant};
use syntax::edition::Edition;
@@ -70,7 +71,7 @@
if path.is_dir() {
return;
}
- let content = std::fs::read_to_string(path).unwrap();
+ let content = fs::read_to_string(path).unwrap();
let ok = codepath(&content).is_ok();
success += ok as usize;
total += 1;
@@ -100,7 +101,11 @@
for (name, f) in testcases!(tokenstream_parse, syn_parse, libsyntax_parse) {
eprint!("{:20}", format!("{}:", name));
let elapsed = exec(f);
- eprintln!("elapsed={}.{:03}s", elapsed.as_secs(), elapsed.subsec_millis());
+ eprintln!(
+ "elapsed={}.{:03}s",
+ elapsed.as_secs(),
+ elapsed.subsec_millis(),
+ );
}
eprintln!();
}