v2 / cmd / tools / measure / parser_speed.v
103 lines · 95 sloc · 2.83 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import os
2import time
3import v.ast
4import v.pref
5import v.parser
6import v.errors
7import v.scanner
8import term
9import file_lists
10
11const skip_tests = os.getenv('SKIP_TESTS').bool()
12const fuzzer_mode = os.getenv('VFUZZER').bool()
13const comments_mode = scanner.CommentsMode.from(os.getenv('SCANNER_MODE')) or {
14 scanner.CommentsMode.skip_comments
15}
16
17fn main() {
18 if !fuzzer_mode {
19 dump(comments_mode)
20 }
21 all_files := file_lists.expand_files(os.args#[1..])!
22 process_files(all_files)!
23}
24
25fn hline() {
26 if fuzzer_mode {
27 return
28 }
29 println('---------------------------------------------------------------------------------------------------------------------------------------------------')
30}
31
32fn theader() {
33 if fuzzer_mode {
34 return
35 }
36 println(' Time Tokens Bytes Lines Bytes/Token Errors')
37}
38
39fn process_files(files []string) ! {
40 nthreads := 1 // TODO
41 mut pref_ := pref.new_preferences()
42 pref_.is_fmt = true
43 pref_.skip_warnings = true
44 pref_.output_mode = .silent
45 mut sw := time.new_stopwatch()
46 mut total_us := i64(0)
47 mut total_bytes := i64(0)
48 mut total_tokens := i64(0)
49 mut total_lines := i64(0)
50 mut total_errors := i64(0)
51 mut total_files := i64(0)
52 for f in files {
53 mut table := ast.new_table()
54 if f == '' {
55 continue
56 }
57 if skip_tests && f.ends_with('_test.v') {
58 continue
59 }
60 // do not measure the scanning, but only the parsing:
61 mut p := new_parser(f, comments_mode, table, pref_)
62 if fuzzer_mode {
63 p.scanner.max_eofs = 200
64 }
65
66 sw.restart()
67 ast_file := p.parse()
68 f_us := sw.elapsed().microseconds()
69
70 total_us += f_us
71 total_bytes += p.scanner.text.len
72 total_tokens += p.scanner.all_tokens.len
73 total_lines += ast_file.nr_lines
74 total_errors += p.errors.len
75 if !fuzzer_mode {
76 println('${f_us:10}us ${p.scanner.all_tokens.len:10} ${p.scanner.text.len:10} ${ast_file.nr_lines:10} ${(f64(p.scanner.text.len) / p.scanner.all_tokens.len):13.3} ${p.errors.len:10} ${f}')
77 }
78 total_files++
79 }
80 hline()
81 theader()
82 hline()
83 speed_mb_s := term.colorize(term.bright_yellow, '${(f64(total_bytes) / total_us):6.3f} MB/s')
84 speed_lines_s := term.colorize(term.bright_yellow,
85 '${(1_000_000 * f64(total_lines) / total_us):10.1f} lines/s')
86 println('${total_us:10}us ${total_tokens:10} ${total_bytes:10} ${total_lines:10} ${(f64(total_bytes) / total_tokens):13.3} ${total_errors:10} Parser speed: ${speed_mb_s}, ${speed_lines_s}, ${nthreads:3} thread(s), ${total_files:5} files.')
87}
88
89fn new_parser(path string, comments_mode scanner.CommentsMode, table &ast.Table, pref_ &pref.Preferences) &parser.Parser {
90 mut p := &parser.Parser{
91 scanner: scanner.new_scanner_file(path, -1, comments_mode, pref_) or { panic(err) }
92 table: table
93 pref: pref_
94 scope: &ast.Scope{
95 start_pos: 0
96 parent: table.global_scope
97 }
98 errors: []errors.Error{}
99 warnings: []errors.Warning{}
100 }
101 p.set_path(path)
102 return p
103}
104