Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Benchmark this system's performance from a developer's perspective.
#
# * Configure and compile Ruby
# * Run the test suite of several medium sized Ruby applications
# * Run the database-heavy test-suite of a Rails application
# * Test the launch times of several common applications
class DeveloperBenchmark
require 'fileutils'
require 'sqlite3'
def self.run
test_runner = DeveloperBenchmark.new
test_runner.run_tests
end
def run_tests
welcome
setup
@results << "SQLite3 Torture Test,"
@results << "Rails Test,"
@results << "Ruby Unit Tests,"
@results << "Ruby 1.9 Compilation Test,"
@results << "Application Launch Test\n"
@results << "#{sqlite_database_test},"
@results << "#{run_rails_tests},"
@results << "#{run_ruby_tmdb_unit_tests_test},"
@results << "#{compile_ruby_source_test},"
@results << "#{application_launch_test}\n"
File.open(File.expand_path("~/Desktop/#{Time.now.to_i}-developer-benchmark.csv"), 'w') {|f| f.write(@results) }
cleanup
`say "Benchmark completed."`
end
def welcome
puts
puts " Welcome to the developer's benchmark for OS X!"
puts " This program runs a series of tests that allows"
puts " you to quantify how responsive & powerful your"
puts " development machine is."
puts
puts " You get 100 bonus points for using this in an Apple"
puts " store!"
puts
puts "Starting tests:"
end
def setup
@tmp_dir = File.expand_path("~/Desktop/benchmark_tmp")
`mkdir #{@tmp_dir}` unless(File.exists?(@tmp_dir))
@first_run = true
@results = ""
end
def cleanup
FileUtils.rm_rf(@tmp_dir)
end
# This test writes and then reads a large amount of data to
# a SQLite3 Database. This is a fairly good test of storage performance
# and it will stress your computer's RAM as well.
def sqlite_database_test
puts "Running SQLite disk torture test..."
start = Time.now
db = SQLite3::Database.new( @tmp_dir + "/test.db" )
db.execute( "create table books ( title varchar(256), content text )" )
10000.times do |x|
db.execute( "insert into books values ( \"Book number #{x}\", \"#{"this is content" * x}\")" )
end
rows = db.execute( "select * from books" )
return (Time.now - start).to_f
end
# This test creates a blank Rails application, adds
# several simple scaffolds, migrates the database and
# then runs the tests. The total time is returned.
def run_rails_tests
puts "Running Rails test..."
rails_dir = @tmp_dir + "/rails"
timer = execute_and_time("rails _2.3.5_ #{rails_dir}")
commands = [
"script/generate scaffold Book title:string content:text",
"script/generate scaffold Article title:string content:text",
"script/generate scaffold Recipe title:string content:text",
"script/generate scaffold Song title:string content:text",
"script/generate scaffold Mantra title:string content:text",
"script/generate scaffold Resume title:string content:text",
"rake db:migrate",
"rake test"
]
commands.each do |command|
timer += execute_and_time("cd #{rails_dir}; #{command}")
end
return timer
end
# Install the ruby-tmdb gem, then download the source from
# GitHub and run it's test suite. The gem needs to be installed
# before we can run the tests to ensure the test denpendencies
# are met. The source is downloaded via SVN for simplicity.
def run_ruby_tmdb_unit_tests_test
if(@first_run)
puts "Installing ruby-tmdb gem..."
`gem install ruby-tmdb`
end
checkout_url = "http://github.com/aarongough/ruby-tmdb.git"
source_dir = @tmp_dir + "/ruby-tmdb"
unless(File.exists?(source_dir))
puts "Downloading ruby-tmdb source, this may take a minute..."
`cd #{@tmp_dir}; git clone #{checkout_url}`
File.open(source_dir + "/test/setup/tmdb_api_key.txt", 'w') {|f| f.write("ab223badef35566") }
end
puts "Running ruby-tmdb tests..."
return execute_and_time("cd #{source_dir}; rake test")
end
# Download and compile the source code for Ruby 1.9 and time
# how long the compilation process takes (the download time
# is not included).
def compile_ruby_source_test
ruby_url = "ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p0.tar.gz"
source_file = @tmp_dir + "/ruby_source.tar.gz"
source_dir = @tmp_dir + "/ruby-1.9.2-p0"
unless(File.exists?(source_dir))
unless(File.exists?(source_file))
puts "Downloading Ruby 1.9 source, this may take a minute..."
`curl -o #{source_file} #{ruby_url}`
end
`tar -zxf #{source_file} -C #{@tmp_dir}`
end
puts "Compiling Ruby 1.9..."
timer = execute_and_time("cd #{source_dir}; " + source_dir + "/configure")
timer += execute_and_time("cd #{source_dir}; " + "make")
return timer
end
# Launch some common Mac applications and time how long
# the process takes,
def application_launch_test
puts "Running application launch test..."
applications = [
"Garageband",
"Terminal",
"Activity Monitor",
"Safari",
"iTunes",
"Xcode",
"Coda",
"Adobe Photoshop CS",
"Google Chrome"
]
start = Time.now
applications.each do |application|
execute_and_capture("open -a \"#{application}\"")
`osascript -e 'tell application "#{application}" to quit'` unless(application == "Coda" || application == "Terminal")
end
return (Time.now - start).to_f
end
# Execute a command in a sub-shell and time
# how long the command takes to execute
def execute_and_time(command)
start = Time.now
execute_and_capture(command)
return (Time.now - start).to_f
end
# Execute a command in a sub-shell and capture
# the entire output of the command to a string.
# This will also capture messages to STDERR
def execute_and_capture(command)
result = `#{command} 2>1`
raise result unless($?.exitstatus == 0)
end
end
DeveloperBenchmark.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment