Skip to content

Instantly share code, notes, and snippets.

@gcoonrod
Created October 24, 2017 16:07
Show Gist options
  • Save gcoonrod/6486e05ddd4fb6d02e4adf9ec77981cb to your computer and use it in GitHub Desktop.
Save gcoonrod/6486e05ddd4fb6d02e4adf9ec77981cb to your computer and use it in GitHub Desktop.
Script for storing timing information from build runs
#!/usr/local/bin/ruby
require 'open3'
require 'optparse'
require 'sqlite3'
require 'terminal-table'
db = SQLite3::Database.new 'timing.db'
options = {}
options[:ds_directory] = 'BUILD_DIR'
options[:build_script] = 'build.sh'
OptionParser.new do |opts|
opts.banner = 'Usage: runner [options]'
opts.on('-h', '--help', 'Print command options.') do |help|
puts opts
exit
end
opts.on('-H', '--testhelp', 'Print Test Help') do |h|
options[:test_help] = h
end
opts.on('-C', '--class [CLASS]', 'Test class to run') do |test_class|
options[:test_class] = test_class
end
opts.on('-M', '--method [METHOD]', 'Test method to run') do |test_method|
options[:test_method] = test_method
end
opts.on('-d', '--debug', 'Debug local tests') do |debug|
options[:debug] = debug
end
opts.on('-D', '--debug-external', 'Debug external instances') do |debug_external|
options[:debug_external] = debug_external
end
opts.on('-V', '--verbose', 'Enable verbose logging.') do |verbose|
options[:verbose] = verbose
end
opts.on('-A', '--print-averages', 'Print current averages') do |averages|
options[:averages] = averages
end
opts.on('-P', '--precommit', 'Run precommit') do |precommit|
options[:precommit] = precommit
end
end.parse!
def build_args(options)
argument_str = ''
if options[:test_help]
argument_str = 'testhelp'
return argument_str
end
if options[:precommit]
argument_str = 'precommitnocoverage'
return argument_str
end
if options[:test_class]
argument_str = argument_str + "-Dtest.classes=#{options[:test_class]} "
end
if options[:test_method]
argument_str = argument_str + "-Dtest.methods=#{options[:test_method]} "
end
if options[:debug]
argument_str = argument_str + '-Dtest.remote.debug.port=5005 '
end
if options[:debug_external]
argument_str = argument_str + '-Dtest.debug.external.instances=true '
end
argument_str
end
def init_db(db)
db.execute <<-SQL
create table if not exists timings (
script varchar(256),
start_time text,
seconds float
);
SQL
end
def print_averages(db)
rows = db.execute <<-SQL
SELECT script, COUNT(script), AVG(seconds)
FROM timings
GROUP BY script
SQL
table = Terminal::Table.new :rows => rows
table.headings = ['Script', 'Runs', 'Avg Run Time']
puts table
end
init_db db
test_command = "#{options[:ds_directory]}/#{options[:build_script]} #{build_args options}"
if options[:verbose]
puts "Options were: #{options}"
puts "Args were: #{build_args options}"
puts "Executing command: #{test_command}"
end
if options[:averages]
print_averages db
exit
end
start_time = Time.now
Open3.popen2e(test_command) do |stdin, stdout_stderr, wait_thread|
Thread.new do
stdout_stderr.each {|l| puts l}
end
stdin.close
wait_thread.value
end
end_time = Time.now
run_time = end_time - start_time
db.execute("INSERT INTO timings (script, start_time, seconds) VALUES ('#{test_command}', CURRENT_TIMESTAMP, #{run_time})")
db.execute("SELECT AVG(seconds), COUNT(seconds) FROM timings WHERE script = '#{test_command}'") do |row|
puts "Command took #{run_time} seconds. Average is #{row[0]} over #{row[1]} runs."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment