2020-04-26 08:32:05 +02:00
|
|
|
import os
|
|
|
|
import flag
|
|
|
|
import scripting
|
|
|
|
import vgit
|
2019-09-28 13:17:16 +02:00
|
|
|
|
|
|
|
const (
|
2020-10-14 23:39:09 +02:00
|
|
|
tool_version = '0.0.5'
|
|
|
|
tool_description = " Compares V executable size and performance,
|
2021-03-24 22:37:10 +01:00
|
|
|
| between 2 commits from V's local git history.
|
2020-10-21 14:24:45 +02:00
|
|
|
| When only one commit is given, it is compared to master.
|
|
|
|
| ".strip_margin()
|
2019-09-28 13:17:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Context {
|
2021-03-24 22:37:10 +01:00
|
|
|
cwd string // current working folder
|
2019-09-28 13:17:16 +02:00
|
|
|
mut:
|
2020-04-20 17:39:25 +02:00
|
|
|
vgo vgit.VGitOptions
|
2020-01-08 21:45:47 +01:00
|
|
|
a string // the full path to the 'after' folder inside workdir
|
|
|
|
b string // the full path to the 'before' folder inside workdir
|
|
|
|
vc string // the full path to the vc folder inside workdir. It is used during bootstrapping v from the C source.
|
2019-09-28 13:17:16 +02:00
|
|
|
commit_before string // the git commit for the 'before' state
|
|
|
|
commit_after string // the git commit for the 'after' state
|
2021-03-24 22:37:10 +01:00
|
|
|
warmups int // how many times to execute a command before gathering stats
|
2019-10-10 19:10:41 +02:00
|
|
|
hyperfineopts string // use for additional CLI options that will be given to the hyperfine command
|
2020-01-08 21:45:47 +01:00
|
|
|
vflags string // other v options to pass to compared v commands
|
2019-09-28 13:17:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
fn new_context() Context {
|
|
|
|
return Context{
|
|
|
|
cwd: os.getwd()
|
|
|
|
commit_after: 'master'
|
|
|
|
warmups: 4
|
2019-09-28 13:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (c Context) compare_versions() {
|
|
|
|
// Input is validated at this point...
|
2019-12-20 21:09:05 +01:00
|
|
|
// Cleanup artifacts from previous runs of this tool:
|
2020-04-20 17:39:25 +02:00
|
|
|
scripting.chdir(c.vgo.workdir)
|
2020-01-08 21:45:47 +01:00
|
|
|
scripting.run('rm -rf "$c.a" "$c.b" "$c.vc" ')
|
2019-10-04 15:11:29 +02:00
|
|
|
// clone the VC source *just once per comparison*, and reuse it:
|
2020-04-20 17:39:25 +02:00
|
|
|
scripting.run('git clone --quiet "$c.vgo.vc_repo_url" "$c.vc" ')
|
2019-12-20 21:09:05 +01:00
|
|
|
println('Comparing V performance of commit $c.commit_before (before) vs commit $c.commit_after (after) ...')
|
2020-01-08 21:45:47 +01:00
|
|
|
c.prepare_v(c.b, c.commit_before)
|
|
|
|
c.prepare_v(c.a, c.commit_after)
|
2020-04-20 17:39:25 +02:00
|
|
|
scripting.chdir(c.vgo.workdir)
|
2020-01-08 21:45:47 +01:00
|
|
|
if c.vflags.len > 0 {
|
|
|
|
os.setenv('VFLAGS', c.vflags, true)
|
|
|
|
}
|
|
|
|
// The first is the baseline, against which all the others will be compared.
|
|
|
|
// It is the fastest, since hello_world.v has only a single println in it,
|
2020-04-26 13:49:31 +02:00
|
|
|
mut perf_files := []string{}
|
2021-03-24 22:37:10 +01:00
|
|
|
perf_files << c.compare_v_performance('source_hello', [
|
|
|
|
'vprod @DEBUG@ -o source.c examples/hello_world.v',
|
|
|
|
'vprod -o source.c examples/hello_world.v',
|
|
|
|
'v @DEBUG@ -o source.c examples/hello_world.v',
|
|
|
|
'v -o source.c examples/hello_world.v',
|
|
|
|
])
|
|
|
|
perf_files << c.compare_v_performance('source_v', ['vprod @DEBUG@ -o source.c @COMPILER@',
|
|
|
|
'vprod -o source.c @COMPILER@', 'v @DEBUG@ -o source.c @COMPILER@',
|
|
|
|
'v -o source.c @COMPILER@',
|
|
|
|
])
|
|
|
|
perf_files << c.compare_v_performance('binary_hello', [
|
|
|
|
'vprod -o hello examples/hello_world.v',
|
|
|
|
'v -o hello examples/hello_world.v',
|
|
|
|
])
|
|
|
|
perf_files << c.compare_v_performance('binary_v', ['vprod -o binary @COMPILER@',
|
|
|
|
'v -o binary @COMPILER@',
|
|
|
|
])
|
2020-01-08 21:45:47 +01:00
|
|
|
println('All performance files:')
|
|
|
|
for f in perf_files {
|
|
|
|
println(' $f')
|
2019-09-28 13:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn (c &Context) prepare_v(cdir string, commit string) {
|
2019-10-04 15:11:29 +02:00
|
|
|
mut cc := os.getenv('CC')
|
2020-01-08 21:45:47 +01:00
|
|
|
if cc == '' {
|
|
|
|
cc = 'cc'
|
|
|
|
}
|
|
|
|
mut vgit_context := vgit.VGitContext{
|
2020-10-14 23:39:09 +02:00
|
|
|
cc: cc
|
|
|
|
commit_v: commit
|
|
|
|
path_v: cdir
|
|
|
|
path_vc: c.vc
|
|
|
|
workdir: c.vgo.workdir
|
|
|
|
v_repo_url: c.vgo.v_repo_url
|
2020-04-20 17:39:25 +02:00
|
|
|
vc_repo_url: c.vgo.vc_repo_url
|
2020-01-08 21:45:47 +01:00
|
|
|
}
|
|
|
|
vgit_context.compile_oldv_if_needed()
|
|
|
|
scripting.chdir(cdir)
|
|
|
|
println('Making a v compiler in $cdir')
|
2020-10-14 23:39:09 +02:00
|
|
|
scripting.run('./v -cc $cc -o v $vgit_context.vvlocation')
|
2020-01-08 21:45:47 +01:00
|
|
|
println('Making a vprod compiler in $cdir')
|
2020-10-14 23:39:09 +02:00
|
|
|
scripting.run('./v -cc $cc -prod -o vprod $vgit_context.vvlocation')
|
2020-01-08 21:45:47 +01:00
|
|
|
println('Stripping and compressing cv v and vprod binaries in $cdir')
|
|
|
|
scripting.run('cp cv cv_stripped')
|
|
|
|
scripting.run('cp v v_stripped')
|
|
|
|
scripting.run('cp vprod vprod_stripped')
|
|
|
|
scripting.run('strip *_stripped')
|
|
|
|
scripting.run('cp cv_stripped cv_stripped_upxed')
|
|
|
|
scripting.run('cp v_stripped v_stripped_upxed')
|
|
|
|
scripting.run('cp vprod_stripped vprod_stripped_upxed')
|
|
|
|
scripting.run('upx -qqq --lzma cv_stripped_upxed')
|
|
|
|
scripting.run('upx -qqq --lzma v_stripped_upxed')
|
|
|
|
scripting.run('upx -qqq --lzma vprod_stripped_upxed')
|
|
|
|
scripting.show_sizes_of_files(['$cdir/cv', '$cdir/cv_stripped', '$cdir/cv_stripped_upxed'])
|
|
|
|
scripting.show_sizes_of_files(['$cdir/v', '$cdir/v_stripped', '$cdir/v_stripped_upxed'])
|
2021-03-24 22:37:10 +01:00
|
|
|
scripting.show_sizes_of_files(['$cdir/vprod', '$cdir/vprod_stripped',
|
|
|
|
'$cdir/vprod_stripped_upxed',
|
|
|
|
])
|
2020-03-06 18:53:29 +01:00
|
|
|
vversion := scripting.run('$cdir/v -version')
|
2020-10-14 23:39:09 +02:00
|
|
|
vcommit := scripting.run('git rev-parse --short --verify HEAD')
|
|
|
|
println('V version is: $vversion , local source commit: $vcommit')
|
2020-02-09 10:08:04 +01:00
|
|
|
if vgit_context.vvlocation == 'cmd/v' {
|
2020-05-17 21:39:10 +02:00
|
|
|
if os.exists('vlib/v/ast/ast.v') {
|
2020-10-14 23:39:09 +02:00
|
|
|
println('Source lines of the compiler: ' +
|
|
|
|
scripting.run('find cmd/v/ vlib/v/ -name "*.v" | grep -v /tests/ | xargs wc | tail -n -1'))
|
2020-05-17 21:39:10 +02:00
|
|
|
} else {
|
2020-10-14 23:39:09 +02:00
|
|
|
println('Source lines of the compiler: ' +
|
|
|
|
scripting.run('wc cmd/v/*.v vlib/compiler/*.v | tail -n -1'))
|
2020-05-17 21:39:10 +02:00
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
} else if vgit_context.vvlocation == 'v.v' {
|
2021-03-24 22:37:10 +01:00
|
|
|
println('Source lines of the compiler: ' +
|
|
|
|
scripting.run('wc v.v vlib/compiler/*.v | tail -n -1'))
|
2020-10-14 23:39:09 +02:00
|
|
|
} else {
|
2020-01-08 21:45:47 +01:00
|
|
|
println('Source lines of the compiler: ' + scripting.run('wc compiler/*.v | tail -n -1'))
|
2019-10-04 15:11:29 +02:00
|
|
|
}
|
2019-09-28 13:17:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
fn (c Context) compare_v_performance(label string, commands []string) string {
|
2019-09-28 13:17:16 +02:00
|
|
|
println('---------------------------------------------------------------------------------')
|
2020-01-08 21:45:47 +01:00
|
|
|
println('Compare v performance when doing the following commands ($label):')
|
2020-02-09 10:08:04 +01:00
|
|
|
mut source_location_a := ''
|
|
|
|
mut source_location_b := ''
|
|
|
|
if os.exists('$c.a/cmd/v') {
|
|
|
|
source_location_a = 'cmd/v'
|
|
|
|
} else {
|
|
|
|
source_location_a = if os.exists('$c.a/v.v') { 'v.v ' } else { 'compiler/ ' }
|
|
|
|
}
|
|
|
|
if os.exists('$c.b/cmd/v') {
|
|
|
|
source_location_b = 'cmd/v'
|
|
|
|
} else {
|
|
|
|
source_location_b = if os.exists('$c.b/v.v') { 'v.v ' } else { 'compiler/ ' }
|
|
|
|
}
|
2020-10-14 23:39:09 +02:00
|
|
|
timestamp_a, _ := vgit.line_to_timestamp_and_commit(scripting.run('cd $c.a/ ; git rev-list -n1 --timestamp HEAD'))
|
|
|
|
timestamp_b, _ := vgit.line_to_timestamp_and_commit(scripting.run('cd $c.b/ ; git rev-list -n1 --timestamp HEAD'))
|
2020-05-17 21:39:10 +02:00
|
|
|
debug_option_a := if timestamp_a > 1570877641 { '-cg ' } else { '-debug ' }
|
|
|
|
debug_option_b := if timestamp_b > 1570877641 { '-cg ' } else { '-debug ' }
|
2020-04-26 13:49:31 +02:00
|
|
|
mut hyperfine_commands_arguments := []string{}
|
2019-12-20 21:09:05 +01:00
|
|
|
for cmd in commands {
|
2020-01-08 21:45:47 +01:00
|
|
|
println(cmd)
|
2019-12-20 21:09:05 +01:00
|
|
|
}
|
|
|
|
for cmd in commands {
|
2021-04-20 19:18:53 +02:00
|
|
|
hyperfine_commands_arguments << ' \'cd ${c.b:-34s} ; ./$cmd \' '.replace_each([
|
2021-03-24 22:37:10 +01:00
|
|
|
'@COMPILER@',
|
|
|
|
source_location_b,
|
|
|
|
'@DEBUG@',
|
|
|
|
debug_option_b,
|
|
|
|
])
|
2019-12-20 21:09:05 +01:00
|
|
|
}
|
2020-01-08 21:45:47 +01:00
|
|
|
for cmd in commands {
|
2021-04-20 19:18:53 +02:00
|
|
|
hyperfine_commands_arguments << ' \'cd ${c.a:-34s} ; ./$cmd \' '.replace_each([
|
2021-03-24 22:37:10 +01:00
|
|
|
'@COMPILER@',
|
|
|
|
source_location_a,
|
|
|
|
'@DEBUG@',
|
|
|
|
debug_option_a,
|
|
|
|
])
|
2020-01-08 21:45:47 +01:00
|
|
|
}
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
2020-04-20 17:39:25 +02:00
|
|
|
cmd_stats_file := os.real_path([c.vgo.workdir, 'v_performance_stats_${label}.json'].join(os.path_separator))
|
2021-03-24 22:37:10 +01:00
|
|
|
comparison_cmd := 'hyperfine $c.hyperfineopts ' + '--export-json $cmd_stats_file ' +
|
|
|
|
'--time-unit millisecond ' + '--style full --warmup $c.warmups ' +
|
|
|
|
hyperfine_commands_arguments.join(' ')
|
2020-01-08 21:45:47 +01:00
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
2020-04-20 17:39:25 +02:00
|
|
|
if c.vgo.verbose {
|
2020-01-08 21:45:47 +01:00
|
|
|
println(comparison_cmd)
|
|
|
|
}
|
|
|
|
os.system(comparison_cmd)
|
2019-10-10 19:10:41 +02:00
|
|
|
println('The detailed performance comparison report was saved to: $cmd_stats_file .')
|
2019-09-28 13:17:16 +02:00
|
|
|
println('')
|
2020-01-08 21:45:47 +01:00
|
|
|
return cmd_stats_file
|
2019-09-28 13:17:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
fn main() {
|
2020-10-14 23:39:09 +02:00
|
|
|
scripting.used_tools_must_exist(['cp', 'rm', 'strip', 'make', 'git', 'upx', 'cc', 'wc', 'tail',
|
|
|
|
'find', 'xargs', 'hyperfine'])
|
2019-10-04 15:11:29 +02:00
|
|
|
mut context := new_context()
|
2019-09-28 13:17:16 +02:00
|
|
|
mut fp := flag.new_flag_parser(os.args)
|
2020-03-19 15:49:07 +01:00
|
|
|
fp.application(os.file_name(os.executable()))
|
2020-01-08 21:45:47 +01:00
|
|
|
fp.version(tool_version)
|
|
|
|
fp.description(tool_description)
|
2019-09-28 13:17:16 +02:00
|
|
|
fp.arguments_description('COMMIT_BEFORE [COMMIT_AFTER]')
|
|
|
|
fp.skip_executable()
|
2021-09-16 18:25:05 +02:00
|
|
|
fp.limit_free_args(1, 2) ?
|
2020-03-19 07:06:37 +01:00
|
|
|
context.vflags = fp.string('vflags', 0, '', 'Additional options to pass to the v commands, for example "-cc tcc"')
|
2020-10-14 23:39:09 +02:00
|
|
|
context.hyperfineopts = fp.string('hyperfine_options', 0, '', 'Additional options passed to hyperfine.
|
2020-04-07 12:38:43 +02:00
|
|
|
${flag.space}For example on linux, you may want to pass:
|
2020-10-14 23:39:09 +02:00
|
|
|
$flag.space--hyperfine_options "--prepare \'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches\'"
|
2020-01-08 21:45:47 +01:00
|
|
|
')
|
2020-04-20 17:39:25 +02:00
|
|
|
commits := vgit.add_common_tool_options(mut context.vgo, mut fp)
|
2019-09-28 13:17:16 +02:00
|
|
|
context.commit_before = commits[0]
|
2020-01-08 21:45:47 +01:00
|
|
|
if commits.len > 1 {
|
|
|
|
context.commit_after = commits[1]
|
|
|
|
}
|
2020-04-20 17:39:25 +02:00
|
|
|
context.b = vgit.normalized_workpath_for_commit(context.vgo.workdir, context.commit_before)
|
|
|
|
context.a = vgit.normalized_workpath_for_commit(context.vgo.workdir, context.commit_after)
|
|
|
|
context.vc = vgit.normalized_workpath_for_commit(context.vgo.workdir, 'vc')
|
|
|
|
if !os.is_dir(context.vgo.workdir) {
|
|
|
|
msg := 'Work folder: ' + context.vgo.workdir + ' , does not exist.'
|
2019-09-28 13:17:16 +02:00
|
|
|
eprintln(msg)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
context.compare_versions()
|
|
|
|
}
|