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