lib/startup_time/app.rb in startup-time-1.0.0 vs lib/startup_time/app.rb in startup-time-1.1.0

- old
+ new

@@ -1,20 +1,22 @@ # frozen_string_literal: true require 'benchmark' +require 'bundler/setup' require 'komenda' require 'shellwords' # for Array#shelljoin require 'tty/table' module StartupTime # StartupTime::App - the entry point for the app. # selects an action based on the command-line options and runs it. class App EXPECTED_OUTPUT = /\AHello, world!\r?\n\z/ + include FileUtils # for `sh` include Util # for `which` - include Services.mixin %i[builder ids_to_groups options selected_tests] + include Services.mixin %i[builder ids_to_groups selected_tests] def initialize(args = ARGV) @options = Options.new(args) @verbosity = @options.verbosity @times = [] @@ -63,11 +65,11 @@ time(id, test) end sorted = @times.sort_by { |result| result[:time] } - if options.format == :json + if @options.format == :json require 'json' puts sorted.to_json elsif !sorted.empty? pairs = sorted.map { |result| [result[:name], '%.02f' % result[:time]] } table = TTY::Table.new(['Test', 'Time (ms)'], pairs) @@ -92,20 +94,51 @@ # otherwise, skip the test def time(id, test) args = Array(test[:command]) if args.size == 1 # native executable + compiler = test[:compiler] || id cmd = File.absolute_path(args.first) - return unless File.exist? cmd + return unless File.exist?(cmd) else # interpreter + source - cmd = which(args.first) + compiler = args.first + cmd = which(compiler) return unless cmd end + # dump the compiler/interpreter's version if running in verbose mode + if @verbosity == :verbose + puts + puts "test: #{id}" + + # false (don't print the program's version); otherwise, a command + # (template string) to execute to dump the version + version = test[:version] + + unless version == false + version ||= '%{compiler} --version' + + # the compiler may have been uninstalled since the target was + # built, so make sure it still exists + if (compiler_path = which(compiler)) + version_command = version % { compiler: compiler_path } + sh version_command + end + end + end + argv0 = args.shift command = [cmd, *args] + unless @verbosity == :quiet + if @verbosity == :verbose + puts "command: #{command.shelljoin}" + else + print '.' + end + end + # make sure the command produces the expected output result = Komenda.run(command) output = result.output if result.error? @@ -116,20 +149,16 @@ abort "invalid output for #{id}: #{output.inspect}" end times = [] - unless @verbosity == :quiet - if @verbosity == :verbose - puts "#{id}: #{command.shelljoin}" - else - print '.' - end - end - - @options.rounds.times do - times << Benchmark.realtime do - system([cmd, argv0], *args, out: File::NULL) + # the bundler environment slows down ruby and breaks truffle-ruby, + # so make sure it's disabled for the benchmark + Bundler.with_clean_env do + @options.rounds.times do + times << Benchmark.realtime do + system([cmd, argv0], *args, out: File::NULL) + end end end @times << { id: id,