Use CmdArgs pattern for bench & analysis stats
This commit is contained in:
@@ -8,7 +8,7 @@ use std::{env, fmt::Write, path::PathBuf};
|
|||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use pico_args::Arguments;
|
use pico_args::Arguments;
|
||||||
use ra_ssr::{SsrPattern, SsrRule};
|
use ra_ssr::{SsrPattern, SsrRule};
|
||||||
use rust_analyzer::cli::{BenchWhat, Position, Verbosity};
|
use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
|
||||||
use vfs::AbsPathBuf;
|
use vfs::AbsPathBuf;
|
||||||
|
|
||||||
pub(crate) struct Args {
|
pub(crate) struct Args {
|
||||||
@@ -24,23 +24,8 @@ pub(crate) enum Command {
|
|||||||
Highlight {
|
Highlight {
|
||||||
rainbow: bool,
|
rainbow: bool,
|
||||||
},
|
},
|
||||||
Stats {
|
AnalysisStats(AnalysisStatsCmd),
|
||||||
randomize: bool,
|
Bench(BenchCmd),
|
||||||
parallel: bool,
|
|
||||||
memory_usage: bool,
|
|
||||||
only: Option<String>,
|
|
||||||
with_deps: bool,
|
|
||||||
path: PathBuf,
|
|
||||||
load_output_dirs: bool,
|
|
||||||
with_proc_macro: bool,
|
|
||||||
},
|
|
||||||
Bench {
|
|
||||||
memory_usage: bool,
|
|
||||||
path: PathBuf,
|
|
||||||
what: BenchWhat,
|
|
||||||
load_output_dirs: bool,
|
|
||||||
with_proc_macro: bool,
|
|
||||||
},
|
|
||||||
Diagnostics {
|
Diagnostics {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
load_output_dirs: bool,
|
load_output_dirs: bool,
|
||||||
@@ -199,7 +184,7 @@ ARGS:
|
|||||||
trailing.pop().unwrap().into()
|
trailing.pop().unwrap().into()
|
||||||
};
|
};
|
||||||
|
|
||||||
Command::Stats {
|
Command::AnalysisStats(AnalysisStatsCmd {
|
||||||
randomize,
|
randomize,
|
||||||
parallel,
|
parallel,
|
||||||
memory_usage,
|
memory_usage,
|
||||||
@@ -208,7 +193,7 @@ ARGS:
|
|||||||
path,
|
path,
|
||||||
load_output_dirs,
|
load_output_dirs,
|
||||||
with_proc_macro,
|
with_proc_macro,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
"analysis-bench" => {
|
"analysis-bench" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
@@ -256,7 +241,13 @@ ARGS:
|
|||||||
let memory_usage = matches.contains("--memory-usage");
|
let memory_usage = matches.contains("--memory-usage");
|
||||||
let load_output_dirs = matches.contains("--load-output-dirs");
|
let load_output_dirs = matches.contains("--load-output-dirs");
|
||||||
let with_proc_macro = matches.contains("--with-proc-macro");
|
let with_proc_macro = matches.contains("--with-proc-macro");
|
||||||
Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro }
|
Command::Bench(BenchCmd {
|
||||||
|
memory_usage,
|
||||||
|
path,
|
||||||
|
what,
|
||||||
|
load_output_dirs,
|
||||||
|
with_proc_macro,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
"diagnostics" => {
|
"diagnostics" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
|||||||
@@ -33,36 +33,8 @@ fn main() -> Result<()> {
|
|||||||
args::Command::Parse { no_dump } => cli::parse(no_dump)?,
|
args::Command::Parse { no_dump } => cli::parse(no_dump)?,
|
||||||
args::Command::Symbols => cli::symbols()?,
|
args::Command::Symbols => cli::symbols()?,
|
||||||
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
|
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
|
||||||
args::Command::Stats {
|
args::Command::AnalysisStats(cmd) => cmd.run(args.verbosity)?,
|
||||||
randomize,
|
args::Command::Bench(cmd) => cmd.run(args.verbosity)?,
|
||||||
parallel,
|
|
||||||
memory_usage,
|
|
||||||
only,
|
|
||||||
with_deps,
|
|
||||||
path,
|
|
||||||
load_output_dirs,
|
|
||||||
with_proc_macro,
|
|
||||||
} => cli::analysis_stats(
|
|
||||||
args.verbosity,
|
|
||||||
memory_usage,
|
|
||||||
path.as_ref(),
|
|
||||||
only.as_ref().map(String::as_ref),
|
|
||||||
with_deps,
|
|
||||||
randomize,
|
|
||||||
parallel,
|
|
||||||
load_output_dirs,
|
|
||||||
with_proc_macro,
|
|
||||||
)?,
|
|
||||||
args::Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro } => {
|
|
||||||
cli::analysis_bench(
|
|
||||||
args.verbosity,
|
|
||||||
path.as_ref(),
|
|
||||||
what,
|
|
||||||
memory_usage,
|
|
||||||
load_output_dirs,
|
|
||||||
with_proc_macro,
|
|
||||||
)?
|
|
||||||
}
|
|
||||||
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
|
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
|
||||||
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
|
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ use ra_ide::Analysis;
|
|||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{AstNode, SourceFile};
|
use ra_syntax::{AstNode, SourceFile};
|
||||||
|
|
||||||
pub use analysis_bench::{analysis_bench, BenchWhat, Position};
|
pub use analysis_bench::{BenchCmd, BenchWhat, Position};
|
||||||
pub use analysis_stats::analysis_stats;
|
pub use analysis_stats::AnalysisStatsCmd;
|
||||||
pub use diagnostics::diagnostics;
|
pub use diagnostics::diagnostics;
|
||||||
pub use load_cargo::load_cargo;
|
pub use load_cargo::load_cargo;
|
||||||
pub use ssr::{apply_ssr_rules, search_for_patterns};
|
pub use ssr::{apply_ssr_rules, search_for_patterns};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Benchmark operations like highlighting or goto definition.
|
//! Benchmark operations like highlighting or goto definition.
|
||||||
|
|
||||||
use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant};
|
use std::{env, path::PathBuf, str::FromStr, sync::Arc, time::Instant};
|
||||||
|
|
||||||
use anyhow::{bail, format_err, Result};
|
use anyhow::{bail, format_err, Result};
|
||||||
use ra_db::{
|
use ra_db::{
|
||||||
@@ -15,6 +15,14 @@ use crate::{
|
|||||||
print_memory_usage,
|
print_memory_usage,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub struct BenchCmd {
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub what: BenchWhat,
|
||||||
|
pub memory_usage: bool,
|
||||||
|
pub load_output_dirs: bool,
|
||||||
|
pub with_proc_macro: bool,
|
||||||
|
}
|
||||||
|
|
||||||
pub enum BenchWhat {
|
pub enum BenchWhat {
|
||||||
Highlight { path: AbsPathBuf },
|
Highlight { path: AbsPathBuf },
|
||||||
Complete(Position),
|
Complete(Position),
|
||||||
@@ -42,23 +50,17 @@ impl FromStr for Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn analysis_bench(
|
impl BenchCmd {
|
||||||
verbosity: Verbosity,
|
pub fn run(self, verbosity: Verbosity) -> Result<()> {
|
||||||
path: &Path,
|
|
||||||
what: BenchWhat,
|
|
||||||
memory_usage: bool,
|
|
||||||
load_output_dirs: bool,
|
|
||||||
with_proc_macro: bool,
|
|
||||||
) -> Result<()> {
|
|
||||||
ra_prof::init();
|
ra_prof::init();
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
eprint!("loading: ");
|
eprint!("loading: ");
|
||||||
let (mut host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?;
|
let (mut host, vfs) = load_cargo(&self.path, self.load_output_dirs, self.with_proc_macro)?;
|
||||||
eprintln!("{:?}\n", start.elapsed());
|
eprintln!("{:?}\n", start.elapsed());
|
||||||
|
|
||||||
let file_id = {
|
let file_id = {
|
||||||
let path = match &what {
|
let path = match &self.what {
|
||||||
BenchWhat::Highlight { path } => path,
|
BenchWhat::Highlight { path } => path,
|
||||||
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path,
|
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path,
|
||||||
};
|
};
|
||||||
@@ -66,7 +68,7 @@ pub fn analysis_bench(
|
|||||||
vfs.file_id(&path).ok_or_else(|| format_err!("Can't find {}", path))?
|
vfs.file_id(&path).ok_or_else(|| format_err!("Can't find {}", path))?
|
||||||
};
|
};
|
||||||
|
|
||||||
match &what {
|
match &self.what {
|
||||||
BenchWhat::Highlight { .. } => {
|
BenchWhat::Highlight { .. } => {
|
||||||
let res = do_work(&mut host, file_id, |analysis| {
|
let res = do_work(&mut host, file_id, |analysis| {
|
||||||
analysis.diagnostics(file_id, true).unwrap();
|
analysis.diagnostics(file_id, true).unwrap();
|
||||||
@@ -77,7 +79,7 @@ pub fn analysis_bench(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
|
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
|
||||||
let is_completion = matches!(what, BenchWhat::Complete(..));
|
let is_completion = matches!(self.what, BenchWhat::Complete(..));
|
||||||
|
|
||||||
let offset = host
|
let offset = host
|
||||||
.analysis()
|
.analysis()
|
||||||
@@ -94,8 +96,9 @@ pub fn analysis_bench(
|
|||||||
println!("\n{:#?}", res);
|
println!("\n{:#?}", res);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let res =
|
let res = do_work(&mut host, file_id, |analysis| {
|
||||||
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_position));
|
analysis.goto_definition(file_position)
|
||||||
|
});
|
||||||
if verbosity.is_verbose() {
|
if verbosity.is_verbose() {
|
||||||
println!("\n{:#?}", res);
|
println!("\n{:#?}", res);
|
||||||
}
|
}
|
||||||
@@ -103,11 +106,12 @@ pub fn analysis_bench(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if memory_usage {
|
if self.memory_usage {
|
||||||
print_memory_usage(host, vfs);
|
print_memory_usage(host, vfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T {
|
fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
//! errors.
|
//! errors.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
path::Path,
|
path::PathBuf,
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
time::{SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -39,34 +39,36 @@ impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn analysis_stats(
|
pub struct AnalysisStatsCmd {
|
||||||
verbosity: Verbosity,
|
pub randomize: bool,
|
||||||
memory_usage: bool,
|
pub parallel: bool,
|
||||||
path: &Path,
|
pub memory_usage: bool,
|
||||||
only: Option<&str>,
|
pub only: Option<String>,
|
||||||
with_deps: bool,
|
pub with_deps: bool,
|
||||||
randomize: bool,
|
pub path: PathBuf,
|
||||||
parallel: bool,
|
pub load_output_dirs: bool,
|
||||||
load_output_dirs: bool,
|
pub with_proc_macro: bool,
|
||||||
with_proc_macro: bool,
|
}
|
||||||
) -> Result<()> {
|
|
||||||
|
impl AnalysisStatsCmd {
|
||||||
|
pub fn run(self, verbosity: Verbosity) -> Result<()> {
|
||||||
let mut rng = {
|
let mut rng = {
|
||||||
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
|
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
|
||||||
Rand32::new(seed)
|
Rand32::new(seed)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut db_load_sw = StopWatch::start().memory(memory_usage);
|
let mut db_load_sw = self.stop_watch();
|
||||||
let (host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?;
|
let (host, vfs) = load_cargo(&self.path, self.load_output_dirs, self.with_proc_macro)?;
|
||||||
let db = host.raw_database();
|
let db = host.raw_database();
|
||||||
eprintln!("Database loaded {}", db_load_sw.elapsed());
|
eprintln!("Database loaded {}", db_load_sw.elapsed());
|
||||||
|
|
||||||
let mut analysis_sw = StopWatch::start().memory(memory_usage);
|
let mut analysis_sw = self.stop_watch();
|
||||||
let mut num_crates = 0;
|
let mut num_crates = 0;
|
||||||
let mut visited_modules = FxHashSet::default();
|
let mut visited_modules = FxHashSet::default();
|
||||||
let mut visit_queue = Vec::new();
|
let mut visit_queue = Vec::new();
|
||||||
|
|
||||||
let mut krates = Crate::all(db);
|
let mut krates = Crate::all(db);
|
||||||
if randomize {
|
if self.randomize {
|
||||||
shuffle(&mut rng, &mut krates);
|
shuffle(&mut rng, &mut krates);
|
||||||
}
|
}
|
||||||
for krate in krates {
|
for krate in krates {
|
||||||
@@ -75,13 +77,13 @@ pub fn analysis_stats(
|
|||||||
let file_id = file_id.original_file(db);
|
let file_id = file_id.original_file(db);
|
||||||
let source_root = db.file_source_root(file_id);
|
let source_root = db.file_source_root(file_id);
|
||||||
let source_root = db.source_root(source_root);
|
let source_root = db.source_root(source_root);
|
||||||
if !source_root.is_library || with_deps {
|
if !source_root.is_library || self.with_deps {
|
||||||
num_crates += 1;
|
num_crates += 1;
|
||||||
visit_queue.push(module);
|
visit_queue.push(module);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if randomize {
|
if self.randomize {
|
||||||
shuffle(&mut rng, &mut visit_queue);
|
shuffle(&mut rng, &mut visit_queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,18 +116,18 @@ pub fn analysis_stats(
|
|||||||
eprintln!("Total functions: {}", funcs.len());
|
eprintln!("Total functions: {}", funcs.len());
|
||||||
eprintln!("Item Collection: {}", analysis_sw.elapsed());
|
eprintln!("Item Collection: {}", analysis_sw.elapsed());
|
||||||
|
|
||||||
if randomize {
|
if self.randomize {
|
||||||
shuffle(&mut rng, &mut funcs);
|
shuffle(&mut rng, &mut funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut bar = match verbosity {
|
let mut bar = match verbosity {
|
||||||
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
|
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
|
||||||
_ if parallel => ProgressReport::hidden(),
|
_ if self.parallel => ProgressReport::hidden(),
|
||||||
_ => ProgressReport::new(funcs.len() as u64),
|
_ => ProgressReport::new(funcs.len() as u64),
|
||||||
};
|
};
|
||||||
|
|
||||||
if parallel {
|
if self.parallel {
|
||||||
let mut inference_sw = StopWatch::start().memory(memory_usage);
|
let mut inference_sw = self.stop_watch();
|
||||||
let snap = Snap(db.snapshot());
|
let snap = Snap(db.snapshot());
|
||||||
funcs
|
funcs
|
||||||
.par_iter()
|
.par_iter()
|
||||||
@@ -138,7 +140,7 @@ pub fn analysis_stats(
|
|||||||
eprintln!("Parallel Inference: {}", inference_sw.elapsed());
|
eprintln!("Parallel Inference: {}", inference_sw.elapsed());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut inference_sw = StopWatch::start().memory(memory_usage);
|
let mut inference_sw = self.stop_watch();
|
||||||
bar.tick();
|
bar.tick();
|
||||||
let mut num_exprs = 0;
|
let mut num_exprs = 0;
|
||||||
let mut num_exprs_unknown = 0;
|
let mut num_exprs_unknown = 0;
|
||||||
@@ -154,7 +156,7 @@ pub fn analysis_stats(
|
|||||||
.filter_map(|it| it.name(db))
|
.filter_map(|it| it.name(db))
|
||||||
.chain(Some(f.name(db)))
|
.chain(Some(f.name(db)))
|
||||||
.join("::");
|
.join("::");
|
||||||
if let Some(only_name) = only {
|
if let Some(only_name) = self.only.as_deref() {
|
||||||
if name.to_string() != only_name && full_name != only_name {
|
if name.to_string() != only_name && full_name != only_name {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -192,7 +194,7 @@ pub fn analysis_stats(
|
|||||||
num_exprs_partially_unknown += 1;
|
num_exprs_partially_unknown += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if only.is_some() && verbosity.is_spammy() {
|
if self.only.is_some() && verbosity.is_spammy() {
|
||||||
// in super-verbose mode for just one function, we print every single expression
|
// in super-verbose mode for just one function, we print every single expression
|
||||||
let (_, sm) = db.body_with_source_map(f_id.into());
|
let (_, sm) = db.body_with_source_map(f_id.into());
|
||||||
let src = sm.expr_syntax(expr_id);
|
let src = sm.expr_syntax(expr_id);
|
||||||
@@ -301,11 +303,16 @@ pub fn analysis_stats(
|
|||||||
report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
|
report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
|
||||||
}
|
}
|
||||||
|
|
||||||
if memory_usage {
|
if self.memory_usage {
|
||||||
print_memory_usage(host, vfs);
|
print_memory_usage(host, vfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stop_watch(&self) -> StopWatch {
|
||||||
|
StopWatch::start().memory(self.memory_usage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
|
fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user