v2 / vlib / sync / bench / run_bench.v
89 lines · 81 sloc · 2.21 KB · bd465b5254c10ac2ef2d214bc7845e5cebfef946
Raw
1// Usage: v run run_bench.v
2// It will generate a `result.md` in current dir.
3import os
4import arrays
5import strings
6
7const compilers = [
8 'tcc',
9 'clang',
10 'gcc',
11]
12
13const run_iterations = 10
14
15const nobj = 10000000
16const run_settings = [
17 [1, 1, 0],
18 [1, 1, 100],
19 [4, 4, 0],
20 [4, 4, 100],
21]
22
23fn get_perf_from_result(result string) !f32 {
24 lines := result.split_into_lines()
25 for l in lines {
26 if l.contains('objects') && l.contains('(') && l.contains(')') {
27 f := l.find_between('(', ')').all_before('objs/µs').trim_space().f32()
28 return f
29 }
30 }
31 return error('run fail?')
32}
33
34fn main() {
35 mut perf_result := []f32{}
36
37 for cc in compilers {
38 // 1. compile
39 compile_cmd := 'v channel_bench_v.v -cc ${cc}'
40 println('compile_cmd: ${compile_cmd}')
41 compile_result := os.execute(compile_cmd)
42 if compile_result.exit_code != 0 {
43 panic('compile fail with "${compile_cmd}"')
44 }
45
46 // 2. run
47 for s in run_settings {
48 run_cmd := './channel_bench_v ${s[0]:-3} ${s[1]:-3} ${s[2]:-3} ${nobj}'
49 println('-----------------------------------------------------------')
50 mut iteration_result := []f32{}
51 for i in 0 .. run_iterations {
52 print('${i:3}: ${run_cmd}')
53 run_result := os.execute(run_cmd)
54 f := get_perf_from_result(run_result.output)!
55 iteration_result << f
56 println(' => ${f:.2} objs/µs')
57 }
58 avg := arrays.sum(iteration_result)! / run_iterations
59 perf_result << avg
60 }
61 }
62
63 // 3. output result
64 mut sb := strings.new_builder(8192)
65 sb.write_string('\n| nsend | nrec | buflen |')
66 for cc in compilers {
67 sb.write_string(' **V (${cc:-5})** |')
68 }
69 sb.writeln('')
70 sb.write_string('| :---: | :---:| :---: |')
71 for _ in 0 .. compilers.len {
72 sb.write_string(' :---: |')
73 }
74 sb.writeln('')
75 for i, s in run_settings {
76 sb.write_string('| ${s[0]:-3} | ${s[1]:-3} | ${s[2]:-3} |')
77 for j in 0 .. compilers.len {
78 sb.write_string(' ${perf_result[j * run_settings.len + i]:-5.2} |')
79 }
80 sb.writeln('')
81 }
82 sb.writeln('')
83 println('***********************************************************')
84 println('writing result to `result.md`...')
85 println('***********************************************************')
86 os.write_file('result.md', sb.str())!
87 println(sb.str())
88 os.rm('./channel_bench_v')!
89}
90