From c0d947aa98391b9bfbf148f70d5e1b1c86e284b8 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 16 Feb 2014 16:44:51 +1100 Subject: [PATCH] allow bench to run with unicorn optionally memstats can output yaml now --- config/initializers/99-unicorn.rb | 2 +- config/unicorn.conf.rb | 4 ++-- script/bench.rb | 34 +++++++++++++++++++++++++++---- script/memstats.rb | 20 ++++++++++++------ 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/config/initializers/99-unicorn.rb b/config/initializers/99-unicorn.rb index 273a70e76..9014f5ab7 100644 --- a/config/initializers/99-unicorn.rb +++ b/config/initializers/99-unicorn.rb @@ -1,4 +1,4 @@ -if ENV['UNICORN_ENABLE_OOBGC'] +if ENV['UNICORN_ENABLE_OOBGC'] == '1' require 'middleware/unicorn_oobgc' Middleware::UnicornOobgc.init end diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb index 31c67713b..5367c46e8 100644 --- a/config/unicorn.conf.rb +++ b/config/unicorn.conf.rb @@ -1,7 +1,7 @@ # See http://unicorn.bogomips.org/Unicorn/Configurator.html # enable out of band gc out of the box, it is low risk and improves perf a lot -ENV['UNICORN_ENABLE_OOBGC'] = "1" +ENV['UNICORN_ENABLE_OOBGC'] ||= "1" discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../") @@ -11,7 +11,7 @@ worker_processes (ENV["UNICORN_WORKERS"] || 3).to_i working_directory discourse_path # listen "#{discourse_path}/tmp/sockets/unicorn.sock" -listen 3000 +listen (ENV["UNICORN_PORT"] || 3000).to_i # nuke workers after 30 seconds instead of 60 seconds (the default) timeout 30 diff --git a/script/bench.rb b/script/bench.rb index 59f13f976..94680367a 100644 --- a/script/bench.rb +++ b/script/bench.rb @@ -8,6 +8,7 @@ require "optparse" @iterations = 500 @best_of = 1 @mem_stats = false +@unicorn = false opts = OptionParser.new do |o| o.banner = "Usage: ruby bench.rb [options]" @@ -27,6 +28,9 @@ opts = OptionParser.new do |o| o.on("-m", "--memory_stats") do @mem_stats = true end + o.on("-u", "--unicorn", "Use unicorn to serve pages as opposed to thin") do + @unicorn = true + end end opts.parse! @@ -140,7 +144,9 @@ api_key = `bundle exec rake api_key:get`.split("\n")[-1] def bench(path) puts "Running apache bench warmup" - `ab -n 10 "http://127.0.0.1:#{@port}#{path}"` + add = "" + add = "-c 3 " if @unicorn + `ab #{add} -n 10 "http://127.0.0.1:#{@port}#{path}"` puts "Benchmarking #{path}" `ab -n #{@iterations} -e tmp/ab.csv "http://127.0.0.1:#{@port}#{path}"` @@ -157,7 +163,12 @@ begin puts "precompiling assets" run("bundle exec rake assets:precompile") - pid = spawn("bundle exec thin start -p #{@port}") + pid = if @unicorn + ENV['UNICORN_PORT'] = @port.to_s + spawn("bundle exec unicorn -c config/unicorn.conf.rb") + else + spawn("bundle exec thin start -p #{@port}") + end while port_available? @port sleep 1 @@ -208,14 +219,29 @@ begin run("RAILS_ENV=profile bundle exec rake assets:clean") - rss = `ps -o rss -p #{pid}`.chomp.split("\n").last.to_i + def get_mem(pid) + YAML.load `ruby script/memstats.rb #{pid} --yaml` + end + + + mem = get_mem(pid) results = results.merge({ "timings" => @timings, "ruby-version" => "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}", - "rss_kb" => rss + "rss_kb" => mem["rss_kb"], + "pss_kb" => mem["pss_kb"] }).merge(facts) + if @unicorn + child_pids = `ps --ppid #{pid} | awk '{ print $1; }' | grep -v PID`.split("\n") + child_pids.each do |child| + mem = get_mem(child) + results["rss_kb_#{child}"] = mem["rss_kb"] + results["pss_kb_#{child}"] = mem["pss_kb"] + end + end + puts results.to_yaml if @mem_stats diff --git a/script/memstats.rb b/script/memstats.rb index 70cc3ce50..261f14a3a 100644 --- a/script/memstats.rb +++ b/script/memstats.rb @@ -127,10 +127,18 @@ def get_commandline( pid ) return commandline.join(' ') end - -puts "#{"Process:".ljust(20)} #{pid}" -puts "#{"Command Line:".ljust(20)} #{get_commandline(pid)}" -puts "Memory Summary:" -totals.keys.sort.each do |k| - puts " #{k.ljust(20)} #{format_number( totals[k] ).rjust(12)} kB" +if ARGV.include? '--yaml' + require 'yaml' + puts Hash[*totals.map do |k,v| + [k + '_kb', v] + end.flatten].to_yaml +else + puts "#{"Process:".ljust(20)} #{pid}" + puts "#{"Command Line:".ljust(20)} #{get_commandline(pid)}" + puts "Memory Summary:" + totals.keys.sort.each do |k| + puts " #{k.ljust(20)} #{format_number( totals[k] ).rjust(12)} kB" + end end + +