Changeset 1201
- Timestamp:
- 01/08/08 14:23:45 (9 months ago)
- Files:
-
- apps/benchmark/script/benchmark (modified) (2 diffs)
- apps/rails_benchmark/app/controllers/perf_controller.rb (modified) (2 diffs)
- apps/rails_benchmark/script/benchmark (modified) (2 diffs)
- apps/shared_bench (added)
- apps/shared_bench/shared.rb (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
apps/benchmark/script/benchmark
r1199 r1201 1 1 #!/usr/bin/env ruby 2 require 'open3'3 require 'optparse'4 2 5 @options = {} 6 @scenarios = {} 7 @err = STDERR.dup 8 9 opts = OptionParser.new do |opts| 10 11 opts.banner = "Usage: ./script/benchmark [cts] argument" 12 opts.define_head "Benchmark for the Rails web framework" 13 opts.separator '*'*80 14 15 opts.on("-c", "--concurrency NUMBER", "Sets the concurrency number during the test.") do |config| 16 @options[:concurrency] = config.to_i 17 end 18 19 opts.on("-t", "--times NUMBER", "Sets the number of iterations to perform during the test.") do |config| 20 @options[:times] = config.to_i 21 end 22 23 opts.on("-s", "--scenarios LIST,OF,SCENARIOS", "Selects which scenario you want to run. Multiple scenarios can be run by supplying a comma seperated list.") do |config| 24 @options[:scenarios] = config.split(",") 25 end 26 27 opts.on("-l", "--[no-]list", "Lists all available scenarios") do |config| 28 @options[:list] = config 29 end 30 31 opts.on("-v", "--version NUMBER", "Runs the tests on this version of Merb (e.g. 0.4.2)") do |config| 32 @options[:version] = config 33 end 34 end.parse! 35 36 def call_ab(msg, url) 37 print " ** #{msg} " 38 Open3.popen3("ab -n#{TIMES} -c#{CNCRC} http://127.0.0.1:4000/#{url}") do |strin, out, err| 39 while true 40 error, output = err.gets, out.gets 41 next unless (error || output) 42 print "." if error =~ /Completed/ 43 STDOUT.flush 44 output = out.gets 45 if (output =~ /Requests per second/) 46 puts "\n " + output 47 break 48 end 49 sleep 0.1 50 end 51 end 52 end 53 54 def get_rps(str) 55 GC.start 56 sleep 1 57 " " + str.find {|x| x =~ /Requests per second/ } 58 end 59 60 def put_header_message(message) 61 spacer = "-" * (39 - message.length / 2) 62 @err.puts "#{spacer} #{message} #{spacer}" 63 end 64 65 def add_scenario(name, title, &block) 66 @scenarios[name] = [title, block] 67 end 68 69 def run_scenarios 70 71 @err.puts "* Benchmarks (These may take a while)" 72 @err.puts "* Requests per benchmark: #{TIMES}" 73 @err.puts "* Concurrency: #{CNCRC}" 74 75 @err.puts 76 77 list = @options[:scenarios] || ["all"] 78 if list.include?("all") 79 run_list = @scenarios.map{|k,v| v } 80 else 81 run_list = @scenarios.keys.sort.map{|k| @scenarios[k] if list.include?(k)} 82 end 83 84 run_list = run_list.compact 85 @err.puts "Available Scenarios #{[@scenarios.keys, "all"].flatten.sort.join(",")}" and exit if run_list.empty? 86 87 run_list.compact.each do |scn| 88 put_header_message(scn.first) 89 scn.last.call 90 end 91 end 92 93 94 # ========================= ADD SCENARIOS HERE =============================================== 95 add_scenario("static", "Static Files") do 96 call_ab("Serve Static File", "hello.txt") 97 end 98 99 add_scenario("erb", "Templates and Partials - (ERB)") do 100 call_ab("Render a string", "perf/string") 101 102 call_ab("Sessions and Time", "perf/index") 103 104 call_ab("Render a simple template", "perf/simple_template") 105 106 [1,10,100].each do |number| 107 call_ab("Render #{number} simple partial#{'s' if number > 1}", "perf/partials/#{number}") 108 end 109 110 [1,10,100].each do |number| 111 call_ab("Render #{number} nested partial#{'s' if number > 1}", "perf/complex_partials/#{number}") 112 end 113 end 114 115 add_scenario("haml", "Templates and Partials - (HAML)") do 116 call_ab("Render a string", "haml_perf/string") 117 118 call_ab("Render a simple template", "haml_perf/simple_template") 119 120 [1,10,100].each do |number| 121 call_ab("Render #{number} simple partial#{'s' if number > 1}", "haml_perf/partials/#{number}") 122 end 123 124 [1,10,100].each do |number| 125 call_ab("Render #{number} nested partial#{'s' if number > 1}", "haml_perf/complex_partials/#{number}") 126 end 127 end 128 129 add_scenario("url", "URL Generation") do 130 [1,10,100].each do |number| 131 call_ab("Render #{number} simple named url#{'s' if number > 1}", "url_perf/simple_named_url_generation/#{number}") 132 end 133 134 [1,10,100].each do |number| 135 call_ab("Render #{number} top level nested named url#{'s' if number > 1}", "url_perf/top_level_nested_named_url_generation/#{number}") 136 end 137 138 [1,10,100].each do |number| 139 call_ab("Render #{number} second level nested named url#{'s' if number > 1}", "url_perf/second_level_nested_named_url_generation/#{number}") 140 end 141 end 142 143 144 # ========================= FINISH SCENARIOS HERE =============================================== 145 146 if @options[:list] 147 out = <<-EOD 148 149 Available Scenarios: #{@scenarios.keys.sort.join(", ")} 150 151 EOD 152 @err.puts out 153 exit 154 end 3 require "#{File.dirname(__FILE__)}/../../shared_bench/shared.rb" 155 4 156 5 begin … … 170 19 sleep 1 171 20 172 TIMES= @options[:times] || 1000173 CNCRC= @options[:concurrency] || 521 @times = @options[:times] || 1000 22 @cncrc = @options[:concurrency] || 5 174 23 175 24 run_scenarios apps/rails_benchmark/app/controllers/perf_controller.rb
r992 r1201 1 1 class PerfController < ApplicationController 2 3 def index 4 5 end 2 6 3 7 def string … … 16 20 end 17 21 22 def index 23 this_time = Time::now 24 last_time = session['last_time'] 25 session['last_time'] = this_time 26 27 content = <<-html 28 <hr> 29 <b> START_TIME </b> @ <i>#{ start_time }</i> 30 <hr> 31 <b> THIS_TIME </b> @ <i>#{ this_time }</i> 32 <hr> 33 <b> LAST_TIME </b> @ <i>#{ last_time }</i> 34 <hr> 35 html 36 end 37 38 private 39 def start_time 40 @@start_time ||= Time::now 41 end 42 18 43 end apps/rails_benchmark/script/benchmark
r1199 r1201 1 1 #!/usr/bin/env ruby 2 require 'open3'3 require 'optparse'4 $stdout.sync = true5 @options = {}6 @scenarios = {}7 @err = STDERR.dup8 2 9 opts = OptionParser.new do |opts| 10 11 opts.banner = "Usage: ./script/benchmark [cts] argument" 12 opts.define_head "Benchmark for the Rails web framework" 13 opts.separator '*'*80 14 15 opts.on("-c", "--concurrency NUMBER", "This flag is for setting the concurrency during the test.") do |config| 16 @options[:concurrency] = config.to_i 17 end 18 19 opts.on("-t", "--times NUMBER", "This flag sets the number of iterations to perform during the test.") do |config| 20 @options[:times] = config.to_i 21 end 22 23 opts.on("-s", "--scenarios LIST,OF,SENARIOS", "This flag allows you to select which scenario you want to run. Multiple scenarios can be run by supplying a comma seperated list.") do |config| 24 @options[:scenarios] = config.split(",") 25 end 26 27 opts.on("-l", "--[no-]list", "Lists all available scenarios") do |config| 28 @options[:list] = config 29 end 30 end.parse! 31 32 def call_ab(msg, url) 33 print " ** #{msg} " 34 Open3.popen3("ab -n#{TIMES} -c#{CNCRC} http://127.0.0.1:4000/#{url}") do |strin, out, err| 35 while true 36 error, output = err.gets, out.gets 37 next unless (error || output) 38 print "." if error =~ /Completed/ 39 STDOUT.flush 40 output = out.gets 41 if (output =~ /Requests per second/) 42 puts "\n " + output 43 break 44 end 45 sleep 0.05 46 end 47 end 48 end 49 50 def get_rps(str) 51 GC.start 52 sleep 1 53 " " + str.find {|x| x =~ /Requests per second/ } 54 end 55 56 def put_header_message(message) 57 spacer = "-" * (39 - message.length / 2) 58 @err.puts "#{spacer} #{message} #{spacer}" 59 end 60 61 def add_scenario(name, title, &block) 62 @scenarios[name] = [title, block] 63 end 64 65 def run_scenarios 66 67 @err.puts "* Benchmarks (These may take a while)" 68 @err.puts "* Requests per benchmark: #{TIMES}" 69 @err.puts "* Concurrency: #{CNCRC}" 70 71 @err.puts 72 73 list = @options[:scenarios] || ["all"] 74 if list.include?("all") 75 run_list = @scenarios.map{|k,v| v } 76 else 77 run_list = @scenarios.keys.sort.map{|k| @scenarios[k] if list.include?(k)} 78 end 79 80 run_list = run_list.compact 81 @err.puts "Available Scenarios #{[@scenarios.keys, "all"].flatten.sort.join(",")}" and exit if run_list.empty? 82 83 run_list.each do |scn| 84 put_header_message(scn.first) 85 scn.last.call 86 end 87 88 end 89 90 91 # ========================= ADD SCENARIOS HERE =============================================== 92 add_scenario("static", "Static Files") do 93 call_ab("Serve Static File", "hello.txt") 94 end 95 96 add_scenario("erb", "Templates and Partials - (ERB)") do 97 call_ab("Render a string", "perf/string") 98 call_ab("Render a simple template", "perf/simple_template") 99 100 [1,10,100].each do |number| 101 call_ab("Render #{number} simple partial#{'s' if number > 1}", "perf/partials/#{number}") 102 end 103 104 [1,10,100].each do |number| 105 call_ab("Render #{number} nested partial#{'s' if number > 1}", "perf/complex_partials/#{number}") 106 end 107 end 108 109 add_scenario("haml", "Templates and Partials - (HAML)") do 110 call_ab("Render a string", "haml_perf/string") 111 call_ab("Render a simple template", "haml_perf/simple_template") 112 113 [1,10,100].each do |number| 114 call_ab("Render #{number} simple partial#{'s' if number > 1}", "haml_perf/partials/#{number}") 115 end 116 117 [1,10,100].each do |number| 118 call_ab("Render #{number} nested partial#{'s' if number > 1}", "haml_perf/complex_partials/#{number}") 119 end 120 end 121 122 add_scenario("url", "URL Generation") do 123 [1,10,100].each do |number| 124 call_ab("Render #{number} simple named url#{'s' if number > 1}", "url_perf/simple_named_url_generation/#{number}") 125 end 126 127 [1,10,100].each do |number| 128 call_ab("Render #{number} top level nested named url#{'s' if number > 1}", "url_perf/top_level_nested_named_url_generation/#{number}") 129 end 130 131 [1,10,100].each do |number| 132 call_ab("Render #{number} second level nested named url#{'s' if number > 1}", "url_perf/second_level_nested_named_url_generation/#{number}") 133 end 134 end 135 136 137 # ========================= FINISH SCENARIOS HERE =============================================== 138 139 140 if @options[:list] 141 out = <<-EOD 142 143 Available Scenarios: #{@scenarios.keys.sort.join(", ")} 144 145 EOD 146 @err.puts out 147 exit 148 end 3 require "#{File.dirname(__FILE__)}/../../shared_bench/shared.rb" 149 4 150 5 begin … … 158 13 ENV["EVENT"] = "1" 159 14 `mongrel_rails start -d -p 4000 -e production` 160 sleep 5 15 161 16 puts Dir[File.join(File.expand_path(__FILE__), "..", "log", "*.pid")] 162 17 puts Dir["#{File.dirname(__FILE__)}/../log/*.pid"].inspect 163 pid = File.read("#{File.dirname(__FILE__)}/../log/mongrel.pid")18 sleep 0.05 until pid = File.read("#{File.dirname(__FILE__)}/../log/mongrel.pid") rescue nil 164 19 165 ps = `ps aux #{pid}` 20 sleep 0.05 until ps = `ps aux #{pid}` 21 166 22 memsize = (ps.split("\n")[1].split[5].to_f / 10.24).round / 100.0 167 23 168 24 @err.puts "* Memory size before benchmarking: #{memsize}MB" 169 25 170 TIMES= @options[:times] || 1000171 CNCRC= @options[:concurrency]|| 526 @times = @options[:times] || 1000 27 @cncrc = @options[:concurrency]|| 5 172 28 173 29 run_scenarios
