2013-08-15 15:19:23 +10:00
require " socket "
require " csv "
2013-08-15 16:35:57 +10:00
require " yaml "
2013-08-15 15:32:07 +10:00
def run ( command )
system ( command , out : $stdout , err : :out )
end
2013-08-29 21:23:00 +10:00
begin
require 'facter'
rescue LoadError
run " gem install facter "
puts " just installed the facter gem, please re-run script "
exit
end
@timings = { }
2013-08-15 16:35:57 +10:00
def measure ( name )
start = Time . now
yield
@timings [ name ] = ( ( Time . now - start ) * 1000 ) . to_i
end
2013-08-15 15:37:33 +10:00
def prereqs
puts " Be sure to following packages are installed:
sudo tasksel install postgresql - server
sudo apt - get - y install build - essential libssl - dev libyaml - dev git libtool libxslt - dev libxml2 - dev libpq - dev gawk curl pngcrush python - software - properties
sudo apt - add - repository - y ppa : rwky / redis
sudo apt - get update
sudo apt - get install redis - server
"
end
2013-08-15 15:19:23 +10:00
puts " Running bundle "
2013-08-15 15:32:07 +10:00
if ! run ( " bundle " )
puts " Quitting, some of the gems did not install "
2013-08-15 15:37:33 +10:00
prereqs
2013-08-15 15:32:07 +10:00
exit
end
2013-08-15 15:19:23 +10:00
puts " Ensuring config is setup "
unless %x{ which ab > /dev/null 2>&1 }
abort " Apache Bench is not installed. Try: apt-get install apache2-utils or brew install ab "
end
unless File . exists? ( " config/database.yml " )
puts " Copying database.yml.development.sample to database.yml "
` cp config/database.yml.development-sample config/database.yml `
end
unless File . exists? ( " config/redis.yml " )
puts " Copying redis.yml.sample to redis.yml "
` cp config/redis.yml.sample config/redis.yml `
end
2013-08-15 16:35:57 +10:00
# Github settings
2013-08-15 15:19:23 +10:00
ENV [ " RAILS_ENV " ] = " profile "
2013-08-15 16:35:57 +10:00
ENV [ " RUBY_GC_MALLOC_LIMIT " ] = " 1000000000 "
ENV [ " RUBY_HEAP_SLOTS_GROWTH_FACTOR " ] = " 1.25 "
ENV [ " RUBY_HEAP_MIN_SLOTS " ] = " 800000 "
ENV [ " RUBY_FREE_MIN " ] = " 600000 "
2013-08-15 15:19:23 +10:00
def port_available? port
2013-08-17 11:36:41 +02:00
server = TCPServer . open ( " 0.0.0.0 " , port )
2013-08-15 15:19:23 +10:00
server . close
true
rescue Errno :: EADDRINUSE
false
end
2013-08-15 17:13:05 +10:00
@port = 60079
2013-08-15 15:19:23 +10:00
2013-08-15 17:13:05 +10:00
while ! port_available? @port
@port += 1
2013-08-15 15:19:23 +10:00
end
puts " Ensuring profiling DB exists and is migrated "
puts ` bundle exec rake db:create `
` bundle exec rake db:migrate `
2013-08-15 16:59:38 +10:00
puts " Timing loading Rails "
2013-08-15 16:35:57 +10:00
measure ( " load_rails " ) do
` bundle exec rake middleware `
end
2013-08-15 15:19:23 +10:00
2013-08-15 16:59:38 +10:00
puts " Populating Profile DB "
run ( " bundle exec ruby script/profile_db_generator.rb " )
2013-08-15 15:19:23 +10:00
2013-09-10 16:03:11 +10:00
puts " Getting api key "
2013-09-10 16:22:58 +10:00
api_key = ` bundle exec rake api_key:get ` . split ( " \n " ) [ - 1 ]
2013-09-10 16:03:11 +10:00
2013-08-15 17:13:05 +10:00
def bench ( path )
2013-08-15 15:19:23 +10:00
puts " Running apache bench warmup "
2013-09-10 16:22:58 +10:00
` ab -n 100 "http://127.0.0.1: #{ @port } #{ path } " `
2013-08-15 17:13:05 +10:00
puts " Benchmarking #{ path } "
2013-09-10 16:22:58 +10:00
` ab -n 100 -e tmp/ab.csv "http://127.0.0.1: #{ @port } #{ path } " `
2013-08-15 15:19:23 +10:00
percentiles = Hash [ * [ 50 , 75 , 90 , 99 ] . zip ( [ ] ) . flatten ]
CSV . foreach ( " tmp/ab.csv " ) do | percent , time |
percentiles [ percent . to_i ] = time . to_i if percentiles . key? percent . to_i
end
2013-08-15 17:13:05 +10:00
percentiles
end
begin
2013-09-10 16:03:11 +10:00
# critical cause cache may be incompatible
puts " precompiling assets "
run ( " bundle exec rake assets:precompile " )
2013-08-15 17:13:05 +10:00
pid = spawn ( " bundle exec thin start -p #{ @port } " )
while port_available? @port
sleep 1
end
2013-08-17 11:36:41 +02:00
puts " Starting benchmark... "
2013-09-03 18:58:56 +10:00
# asset precompilation is a dog, wget to force it
run " wget http://127.0.0.1: #{ @port } / -o tmp/test.html "
2013-08-15 17:13:05 +10:00
home_page = bench ( " / " )
topic_page = bench ( " /t/oh-how-i-wish-i-could-shut-up-like-a-tunnel-for-so/69 " )
2013-09-10 16:03:11 +10:00
append = " ?api_key= #{ api_key } &api_username=admin1 "
2013-09-10 16:22:58 +10:00
2013-09-10 16:03:11 +10:00
home_page_admin = bench ( " / #{ append } " )
topic_page_admin = bench ( " /t/oh-how-i-wish-i-could-shut-up-like-a-tunnel-for-so/69 #{ append } " )
2013-08-15 17:48:11 +10:00
puts " Your Results: (note for timings- percentile is first, duration is second in millisecs) "
2013-08-15 15:19:23 +10:00
2013-08-29 21:34:32 +10:00
facts = Facter . to_hash
facts . delete_if { | k , v |
! [ " operatingsystem " , " architecture " , " kernelversion " ,
" memorysize " , " physicalprocessorcount " , " processor0 " ,
" virtual " ] . include? ( k )
}
2013-09-10 16:03:11 +10:00
run ( " RAILS_ENV=profile bundle exec rake assets:clean " )
2013-08-15 15:19:23 +10:00
puts ( {
2013-08-15 17:13:05 +10:00
" home_page " = > home_page ,
" topic_page " = > topic_page ,
2013-09-10 16:03:11 +10:00
" home_page_admin " = > home_page_admin ,
" topic_page_admin " = > topic_page_admin ,
2013-08-15 17:48:11 +10:00
" timings " = > @timings ,
" ruby-version " = > " #{ RUBY_VERSION } -p #{ RUBY_PATCHLEVEL } " ,
" rails4? " = > ENV [ " RAILS4 " ] == " 1 "
2013-08-29 21:34:32 +10:00
} . merge ( facts ) . to_yaml )
2013-08-15 15:19:23 +10:00
2013-08-29 21:23:00 +10:00
2013-09-10 16:03:11 +10:00
# TODO include Facter.to_hash ... for all facts
2013-08-15 15:19:23 +10:00
ensure
Process . kill " KILL " , pid
end