v2 / cmd / tools / measure / scanner_speed.v
76 lines · 71 sloc · 2.15 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import os
2import time
3import term
4import v.scanner
5import file_lists
6import v.pref
7
8const skip_tests = os.getenv('SKIP_TESTS').bool()
9const fuzzer_mode = os.getenv('VFUZZER').bool()
10const comments_mode = scanner.CommentsMode.from(os.getenv('SCANNER_MODE')) or {
11 scanner.CommentsMode.skip_comments
12}
13
14fn main() {
15 if !fuzzer_mode {
16 dump(comments_mode)
17 }
18 all_files := file_lists.expand_files(os.args#[1..])!
19 process_files(all_files)!
20}
21
22fn hline() {
23 if fuzzer_mode {
24 return
25 }
26 println('----------------------------------------------------------------------------------------------------------------------------------------------------')
27}
28
29fn theader() {
30 if fuzzer_mode {
31 return
32 }
33 println(' Time Tokens Bytes Lines Bytes/Token Errors')
34}
35
36fn process_files(files []string) ! {
37 nthreads := 1 // TODO
38 mut pref_ := pref.new_preferences()
39 pref_.is_fmt = true
40 pref_.skip_warnings = true
41 pref_.output_mode = .silent
42 mut sw := time.new_stopwatch()
43 mut total_us := i64(0)
44 mut total_bytes := i64(0)
45 mut total_tokens := i64(0)
46 mut total_lines := i64(0)
47 mut total_errors := i64(0)
48 mut total_files := i64(0)
49 for f in files {
50 if f == '' {
51 continue
52 }
53 if skip_tests && f.ends_with('_test.v') {
54 continue
55 }
56 total_files++
57 sw.restart()
58 s := scanner.new_scanner_file(f, -1, comments_mode, pref_)!
59 f_us := sw.elapsed().microseconds()
60 total_us += f_us
61 total_bytes += s.text.len
62 total_tokens += s.all_tokens.len
63 total_lines += s.nr_lines
64 total_errors += s.errors.len
65 if !fuzzer_mode {
66 println('${f_us:10}us ${s.all_tokens.len:10} ${s.text.len:10} ${s.nr_lines:10} ${(f64(s.text.len) / s.all_tokens.len):13.3f} ${s.errors.len:10} ${f}')
67 }
68 }
69 hline()
70 theader()
71 hline()
72 speed_mb_s := term.colorize(term.bright_yellow, '${(f64(total_bytes) / total_us):6.3f} MB/s')
73 speed_lines_s := term.colorize(term.bright_yellow,
74 '${(1_000_000 * f64(total_lines) / total_us):10.1f} lines/s')
75 println('${total_us:10}us ${total_tokens:10} ${total_bytes:10} ${total_lines:10} ${(f64(total_bytes) / total_tokens):13.3} ${total_errors:10} Scanner speed: ${speed_mb_s}, ${speed_lines_s}, ${nthreads:3} thread(s), ${total_files:5} files.')
76}
77