vile / bench / compare_blip.vsh
101 lines · 97 sloc · 3.06 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1#!/usr/bin/env -S v run
2
3// Build both implementations into one executable with identical compiler flags.
4// Blip is read from a local checkout; this script never writes into that tree.
5import os
6import strconv
7import time
8
9fn positive_argument(text string, name string) !int {
10 value := strconv.parse_int(text, 10, 32)!
11 if value < 1 {
12 return error('${name} must be positive')
13 }
14 return int(value)
15}
16
17fn main() {
18 usage := 'Usage: v run bench/compare_blip.vsh [bytes=65536] [samples=7] [blip_path=~/git/blip] [--dev]'
19 mut positional := []string{}
20 mut production := true
21 for arg in os.args[1..] {
22 if arg in ['--help', '-h'] {
23 println(usage)
24 return
25 }
26 if arg == '--dev' {
27 production = false
28 } else {
29 positional << arg
30 }
31 }
32 if positional.len > 3 {
33 eprintln(usage)
34 exit(1)
35 }
36 size := positive_argument(if positional.len > 0 { positional[0] } else { '65536' }, 'bytes') or {
37 eprintln('invalid bytes: ${err}')
38 exit(1)
39 }
40 samples := positive_argument(if positional.len > 1 { positional[1] } else { '7' }, 'samples') or {
41 eprintln('invalid samples: ${err}')
42 exit(1)
43 }
44 if samples > 101 {
45 eprintln('samples must be between 1 and 101')
46 exit(1)
47 }
48 blip_path := os.real_path(if positional.len > 2 {
49 os.expand_tilde_to_home(positional[2])
50 } else {
51 os.join_path(os.home_dir(), 'git', 'blip')
52 })
53 if !os.is_file(os.join_path(blip_path, 'vlib', 'blip', 'reader', 'mod.v')) {
54 eprintln('Blip reader not found in ${blip_path}; pass the checkout as the third argument')
55 exit(1)
56 }
57 compare(blip_path, size, samples, production) or {
58 eprintln(err)
59 exit(1)
60 }
61}
62
63fn compare(blip_path string, size int, samples int, production bool) ! {
64 root := @VMODROOT
65 benchmark_dir := os.join_path(os.temp_dir(), 'vile-vs-blip-${os.getpid()}-${time.sys_mono_now()}')
66 os.mkdir(benchmark_dir)!
67 defer {
68 os.rmdir_all(benchmark_dir) or { eprintln('could not remove ${benchmark_dir}: ${err}') }
69 }
70 binary := os.join_path(benchmark_dir, 'compare_blip' + $if windows { '.exe' } $else { '' })
71 module_path := '${root}|${blip_path}|@vlib|@vmodules'
72 mode := if production { '-prod' } else { '' }
73 version := os.exec([@VEXE, 'version'])
74 if version.exit_code != 0 {
75 return error(version.output)
76 }
77 println('Compiler: ${version.output.trim_space()}')
78 println('Flags: -cc gcc ${mode}')
79 println('Blip source: ${blip_path}')
80 revision := os.exec(['git', '-C', blip_path, 'rev-parse', '--short', 'HEAD'])
81 if revision.exit_code == 0 {
82 println('Blip HEAD: ${revision.output.trim_space()} (uses working-tree source)')
83 }
84 println('Building comparison...')
85 // Blip requires V3 module resolution; refuse a fallback to another backend.
86 os.setenv('V_MACOS_V3_NO_FALLBACK', '1', true)
87 mut command := [@VEXE, '-cc', 'gcc']
88 if production {
89 command << '-prod'
90 }
91 command << ['-path', module_path, '-o', binary, os.join_path(root, 'bench', 'compare')]
92 built := os.exec(command)
93 if built.exit_code != 0 {
94 return error(built.output)
95 }
96 result := os.exec([binary, size.str(), samples.str()])
97 print(result.output)
98 if result.exit_code != 0 {
99 return error('comparison exited with status ${result.exit_code}')
100 }
101}
102