| 1 | // Usage: v run run_bench.v |
| 2 | // It will generate a `result.md` in current dir. |
| 3 | import os |
| 4 | import arrays |
| 5 | import strings |
| 6 | |
| 7 | const compilers = [ |
| 8 | 'tcc', |
| 9 | 'clang', |
| 10 | 'gcc', |
| 11 | ] |
| 12 | |
| 13 | const run_iterations = 10 |
| 14 | |
| 15 | const nobj = 10000000 |
| 16 | const run_settings = [ |
| 17 | [1, 1, 0], |
| 18 | [1, 1, 100], |
| 19 | [4, 4, 0], |
| 20 | [4, 4, 100], |
| 21 | ] |
| 22 | |
| 23 | fn 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 | |
| 34 | fn 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 | |