|
| 1 | +require "socket" |
| 2 | +require "csv" |
| 3 | + |
| 4 | +puts "Running bundle" |
| 5 | +`bundle` |
| 6 | + |
| 7 | +puts "Ensuring config is setup" |
| 8 | + |
| 9 | +unless %x{which ab > /dev/null 2>&1} |
| 10 | + abort "Apache Bench is not installed. Try: apt-get install apache2-utils or brew install ab" |
| 11 | +end |
| 12 | + |
| 13 | + |
| 14 | +unless File.exists?("config/database.yml") |
| 15 | + puts "Copying database.yml.development.sample to database.yml" |
| 16 | + `cp config/database.yml.development-sample config/database.yml` |
| 17 | +end |
| 18 | + |
| 19 | +unless File.exists?("config/redis.yml") |
| 20 | + puts "Copying redis.yml.sample to redis.yml" |
| 21 | + `cp config/redis.yml.sample config/redis.yml` |
| 22 | +end |
| 23 | + |
| 24 | +ENV["RAILS_ENV"] = "profile" |
| 25 | + |
| 26 | +def port_available? port |
| 27 | + server = TCPServer.open port |
| 28 | + server.close |
| 29 | + true |
| 30 | +rescue Errno::EADDRINUSE |
| 31 | + false |
| 32 | +end |
| 33 | + |
| 34 | +port = 60079 |
| 35 | + |
| 36 | +while !port_available? port |
| 37 | + port += 1 |
| 38 | +end |
| 39 | + |
| 40 | +puts "Ensuring profiling DB exists and is migrated" |
| 41 | +puts `bundle exec rake db:create` |
| 42 | +`bundle exec rake db:migrate` |
| 43 | + |
| 44 | +puts "Loading Rails" |
| 45 | +require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
| 46 | + |
| 47 | + |
| 48 | +begin |
| 49 | + unless pid = fork |
| 50 | + require "rack" |
| 51 | + Rack::Server.start(:config => "config.ru", |
| 52 | + :AccessLog => [], |
| 53 | + :Port => port) |
| 54 | + exit |
| 55 | + end |
| 56 | + |
| 57 | + while port_available? port |
| 58 | + sleep 1 |
| 59 | + end |
| 60 | + |
| 61 | + puts "Running apache bench warmup" |
| 62 | + `ab -n 100 http://localhost:#{port}/` |
| 63 | + puts "Benchmarking front page" |
| 64 | + `ab -n 100 -e tmp/ab.csv http://localhost:#{port}/` |
| 65 | + |
| 66 | + percentiles = Hash[*[50, 75, 90, 99].zip([]).flatten] |
| 67 | + CSV.foreach("tmp/ab.csv") do |percent, time| |
| 68 | + percentiles[percent.to_i] = time.to_i if percentiles.key? percent.to_i |
| 69 | + end |
| 70 | + |
| 71 | + puts "Your Results:" |
| 72 | + |
| 73 | + puts({ |
| 74 | + "home_page" => percentiles |
| 75 | + }.to_yaml) |
| 76 | + |
| 77 | +ensure |
| 78 | + Process.kill "KILL", pid |
| 79 | +end |
0 commit comments