2019-12-23 11:09:22 +01:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
flag
|
2020-01-08 21:45:47 +01:00
|
|
|
scripting
|
|
|
|
vgit
|
2019-12-23 11:09:22 +01:00
|
|
|
)
|
2019-09-28 13:17:16 +02:00
|
|
|
|
|
|
|
const (
|
2020-01-08 21:45:47 +01:00
|
|
|
tool_version = '0.0.5'
|
|
|
|
tool_description = ' Compares V executable size and performance,
|
|
|
|
between 2 commits from V\'s local git history.
|
|
|
|
When only one commit is given, it is compared to master.
|
|
|
|
'
|
2019-09-28 13:17:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Context {
|
2020-01-08 21:45:47 +01:00
|
|
|
cwd string // current working folder
|
2019-09-28 13:17:16 +02:00
|
|
|
mut:
|
2020-03-13 09:45:40 +01:00
|
|
|
v_repo_url string // the url of the vc repository. It can be a local folder path, which is useful to eliminate network operations...
|
|
|
|
vc_repo_url string // the url of the vc repository. It can be a local folder path, which is useful to eliminate network operations...
|
2020-01-08 21:45:47 +01:00
|
|
|
workdir string // the working folder (typically /tmp), where the tool will write
|
|
|
|
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
|
2020-01-08 21:45:47 +01:00
|
|
|
warmups int // how many times to execute a command before gathering stats
|
|
|
|
verbose bool // whether to print even more stuff
|
|
|
|
show_help bool // whether to show the usage screen
|
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-02-17 20:31:23 +01:00
|
|
|
scripting.chdir(c.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-01-08 21:45:47 +01:00
|
|
|
scripting.run('git clone --quiet "$c.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)
|
|
|
|
scripting.chdir(c.workdir)
|
2019-09-28 13:17:16 +02:00
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
if c.vflags.len > 0 {
|
|
|
|
os.setenv('VFLAGS', c.vflags, true)
|
|
|
|
}
|
2020-02-17 20:31:23 +01:00
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
// 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,
|
|
|
|
mut perf_files := []string
|
|
|
|
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',
|
|
|
|
])
|
2020-02-17 20:31:23 +01:00
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
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',
|
|
|
|
])
|
2020-02-17 20:31:23 +01:00
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
perf_files << c.compare_v_performance('binary_v', [
|
|
|
|
'vprod -o binary @COMPILER@',
|
|
|
|
'v -o binary @COMPILER@',
|
|
|
|
])
|
|
|
|
|
|
|
|
println('All performance files:')
|
|
|
|
for f in perf_files {
|
|
|
|
println(' $f')
|
2019-09-28 13:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 21:45:47 +01: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{
|
|
|
|
cc: cc
|
|
|
|
workdir: c.workdir
|
|
|
|
commit_v: commit
|
|
|
|
path_v: cdir
|
|
|
|
path_vc: c.vc
|
|
|
|
v_repo_url: c.v_repo_url
|
|
|
|
vc_repo_url: c.vc_repo_url
|
|
|
|
}
|
|
|
|
vgit_context.compile_oldv_if_needed()
|
|
|
|
scripting.chdir(cdir)
|
|
|
|
println('Making a v compiler in $cdir')
|
|
|
|
scripting.run('./v -cc ${cc} -o v $vgit_context.vvlocation')
|
|
|
|
println('Making a vprod compiler in $cdir')
|
|
|
|
scripting.run('./v -cc ${cc} -prod -o vprod $vgit_context.vvlocation')
|
|
|
|
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'])
|
|
|
|
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-02-17 20:31:23 +01:00
|
|
|
vcommit := scripting.run('git rev-parse --short --verify HEAD')
|
2020-01-08 21:45:47 +01:00
|
|
|
println('V version is: ${vversion} , local source commit: ${vcommit}')
|
2020-02-09 10:08:04 +01:00
|
|
|
if vgit_context.vvlocation == 'cmd/v' {
|
|
|
|
println('Source lines of the compiler: ' + scripting.run('wc cmd/v/*.v vlib/compiler/*.v | tail -n -1'))
|
|
|
|
} else if vgit_context.vvlocation == 'v.v' {
|
2020-01-08 21:45:47 +01:00
|
|
|
println('Source lines of the compiler: ' + scripting.run('wc v.v vlib/compiler/*.v | tail -n -1'))
|
|
|
|
}else{
|
|
|
|
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-01-08 21:45:47 +01: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'))
|
|
|
|
debug_option_a := if timestamp_a > 1570877641 { '-g ' } else { '-debug ' }
|
|
|
|
debug_option_b := if timestamp_b > 1570877641 { '-g ' } else { '-debug ' }
|
2019-10-10 19:10:41 +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 {
|
2020-01-08 21:45:47 +01:00
|
|
|
hyperfine_commands_arguments << " \'cd ${c.b:-34s} ; ./$cmd \' ".replace_each(['@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 {
|
|
|
|
hyperfine_commands_arguments << " \'cd ${c.a:-34s} ; ./$cmd \' ".replace_each(['@COMPILER@', source_location_a, '@DEBUG@', debug_option_a])
|
|
|
|
}
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
2020-03-20 16:41:18 +01:00
|
|
|
cmd_stats_file := os.real_path([c.workdir, 'v_performance_stats_${label}.json'].join(os.path_separator))
|
2020-01-08 21:45:47 +01:00
|
|
|
comparison_cmd := 'hyperfine $c.hyperfineopts ' + '--export-json ${cmd_stats_file} ' + '--time-unit millisecond ' + '--style full --warmup $c.warmups ' + hyperfine_commands_arguments.join(' ')
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
if c.verbose {
|
|
|
|
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() {
|
|
|
|
scripting.used_tools_must_exist(['cp', 'rm', 'strip', 'make', 'git', 'upx', 'cc', 'wc', 'tail', '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()
|
2020-01-08 21:45:47 +01:00
|
|
|
fp.limit_free_args(1, 2)
|
2019-12-20 21:09:05 +01:00
|
|
|
|
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"')
|
|
|
|
context.hyperfineopts = fp.string('hyperfine_options', 0, '',
|
2020-01-08 21:45:47 +01:00
|
|
|
'Additional options passed to hyperfine.
|
2020-04-07 12:38:43 +02:00
|
|
|
${flag.space}For example on linux, you may want to pass:
|
|
|
|
${flag.space}--hyperfine_options "--prepare \'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches\'"
|
2020-01-08 21:45:47 +01:00
|
|
|
')
|
|
|
|
commits := vgit.add_common_tool_options(mut context, 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]
|
|
|
|
}
|
|
|
|
context.b = vgit.normalized_workpath_for_commit(context.workdir, context.commit_before)
|
|
|
|
context.a = vgit.normalized_workpath_for_commit(context.workdir, context.commit_after)
|
|
|
|
context.vc = vgit.normalized_workpath_for_commit(context.workdir, 'vc')
|
|
|
|
if !os.is_dir(context.workdir) {
|
2019-09-28 13:17:16 +02:00
|
|
|
msg := 'Work folder: ' + context.workdir + ' , does not exist.'
|
|
|
|
eprintln(msg)
|
|
|
|
exit(2)
|
|
|
|
}
|
2020-02-17 20:31:23 +01:00
|
|
|
|
2019-09-28 13:17:16 +02:00
|
|
|
context.compare_versions()
|
|
|
|
}
|