11//! Benchmark operations like highlighting or goto definition.
22
3- use std:: { env, path:: Path , str:: FromStr , sync:: Arc , time:: Instant } ;
3+ use std:: { env, path:: PathBuf , str:: FromStr , sync:: Arc , time:: Instant } ;
44
55use anyhow:: { bail, format_err, Result } ;
66use ra_db:: {
@@ -15,6 +15,14 @@ use crate::{
1515 print_memory_usage,
1616} ;
1717
18+ pub struct BenchCmd {
19+ pub path : PathBuf ,
20+ pub what : BenchWhat ,
21+ pub memory_usage : bool ,
22+ pub load_output_dirs : bool ,
23+ pub with_proc_macro : bool ,
24+ }
25+
1826pub enum BenchWhat {
1927 Highlight { path : AbsPathBuf } ,
2028 Complete ( Position ) ,
@@ -42,72 +50,68 @@ impl FromStr for Position {
4250 }
4351}
4452
45- pub fn analysis_bench (
46- verbosity : Verbosity ,
47- path : & Path ,
48- what : BenchWhat ,
49- memory_usage : bool ,
50- load_output_dirs : bool ,
51- with_proc_macro : bool ,
52- ) -> Result < ( ) > {
53- ra_prof:: init ( ) ;
54-
55- let start = Instant :: now ( ) ;
56- eprint ! ( "loading: " ) ;
57- let ( mut host, vfs) = load_cargo ( path, load_output_dirs, with_proc_macro) ?;
58- eprintln ! ( "{:?}\n " , start. elapsed( ) ) ;
59-
60- let file_id = {
61- let path = match & what {
62- BenchWhat :: Highlight { path } => path,
63- BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => & pos. path ,
64- } ;
65- let path = path. clone ( ) . into ( ) ;
66- vfs. file_id ( & path) . ok_or_else ( || format_err ! ( "Can't find {}" , path) ) ?
67- } ;
68-
69- match & what {
70- BenchWhat :: Highlight { .. } => {
71- let res = do_work ( & mut host, file_id, |analysis| {
72- analysis. diagnostics ( file_id, true ) . unwrap ( ) ;
73- analysis. highlight_as_html ( file_id, false ) . unwrap ( )
74- } ) ;
75- if verbosity. is_verbose ( ) {
76- println ! ( "\n {}" , res) ;
77- }
78- }
79- BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => {
80- let is_completion = matches ! ( what, BenchWhat :: Complete ( ..) ) ;
53+ impl BenchCmd {
54+ pub fn run ( self , verbosity : Verbosity ) -> Result < ( ) > {
55+ ra_prof:: init ( ) ;
56+
57+ let start = Instant :: now ( ) ;
58+ eprint ! ( "loading: " ) ;
59+ let ( mut host, vfs) = load_cargo ( & self . path , self . load_output_dirs , self . with_proc_macro ) ?;
60+ eprintln ! ( "{:?}\n " , start. elapsed( ) ) ;
8161
82- let offset = host
83- . analysis ( )
84- . file_line_index ( file_id) ?
85- . offset ( LineCol { line : pos. line - 1 , col_utf16 : pos. column } ) ;
86- let file_position = FilePosition { file_id, offset } ;
62+ let file_id = {
63+ let path = match & self . what {
64+ BenchWhat :: Highlight { path } => path,
65+ BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => & pos. path ,
66+ } ;
67+ let path = path. clone ( ) . into ( ) ;
68+ vfs. file_id ( & path) . ok_or_else ( || format_err ! ( "Can't find {}" , path) ) ?
69+ } ;
8770
88- if is_completion {
89- let options = CompletionConfig :: default ( ) ;
71+ match & self . what {
72+ BenchWhat :: Highlight { .. } => {
9073 let res = do_work ( & mut host, file_id, |analysis| {
91- analysis. completions ( & options, file_position)
74+ analysis. diagnostics ( file_id, true ) . unwrap ( ) ;
75+ analysis. highlight_as_html ( file_id, false ) . unwrap ( )
9276 } ) ;
9377 if verbosity. is_verbose ( ) {
94- println ! ( "\n {:#? }" , res) ;
78+ println ! ( "\n {}" , res) ;
9579 }
96- } else {
97- let res =
98- do_work ( & mut host, file_id, |analysis| analysis. goto_definition ( file_position) ) ;
99- if verbosity. is_verbose ( ) {
100- println ! ( "\n {:#?}" , res) ;
80+ }
81+ BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => {
82+ let is_completion = matches ! ( self . what, BenchWhat :: Complete ( ..) ) ;
83+
84+ let offset = host
85+ . analysis ( )
86+ . file_line_index ( file_id) ?
87+ . offset ( LineCol { line : pos. line - 1 , col_utf16 : pos. column } ) ;
88+ let file_position = FilePosition { file_id, offset } ;
89+
90+ if is_completion {
91+ let options = CompletionConfig :: default ( ) ;
92+ let res = do_work ( & mut host, file_id, |analysis| {
93+ analysis. completions ( & options, file_position)
94+ } ) ;
95+ if verbosity. is_verbose ( ) {
96+ println ! ( "\n {:#?}" , res) ;
97+ }
98+ } else {
99+ let res = do_work ( & mut host, file_id, |analysis| {
100+ analysis. goto_definition ( file_position)
101+ } ) ;
102+ if verbosity. is_verbose ( ) {
103+ println ! ( "\n {:#?}" , res) ;
104+ }
101105 }
102106 }
103107 }
104- }
105108
106- if memory_usage {
107- print_memory_usage ( host, vfs) ;
108- }
109+ if self . memory_usage {
110+ print_memory_usage ( host, vfs) ;
111+ }
109112
110- Ok ( ( ) )
113+ Ok ( ( ) )
114+ }
111115}
112116
113117fn do_work < F : Fn ( & Analysis ) -> T , T > ( host : & mut AnalysisHost , file_id : FileId , work : F ) -> T {
0 commit comments