Changeset 1201

Show
Ignore:
Timestamp:
01/08/08 14:23:45 (9 months ago)
Author:
wyca..@gmail.com
Message:

benchmark changes so Rails and merb use the same code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • apps/benchmark/script/benchmark

    r1199 r1201  
    11#!/usr/bin/env ruby 
    2 require 'open3' 
    3 require 'optparse' 
    42 
    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 
     3require "#{File.dirname(__FILE__)}/../../shared_bench/shared.rb" 
    1554 
    1565begin 
     
    17019  sleep 1 
    17120 
    172   TIMES = @options[:times] || 1000 
    173   CNCRC = @options[:concurrency] || 5 
     21  @times = @options[:times] || 1000 
     22  @cncrc = @options[:concurrency] || 5 
    17423 
    17524  run_scenarios 
  • apps/rails_benchmark/app/controllers/perf_controller.rb

    r992 r1201  
    11class PerfController < ApplicationController 
     2 
     3  def index 
     4     
     5  end 
    26   
    37  def string 
     
    1620  end 
    1721   
     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   
    1843end 
  • apps/rails_benchmark/script/benchmark

    r1199 r1201  
    11#!/usr/bin/env ruby 
    2 require 'open3' 
    3 require 'optparse' 
    4 $stdout.sync = true 
    5 @options = {} 
    6 @scenarios = {} 
    7 @err = STDERR.dup 
    82 
    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 
     3require "#{File.dirname(__FILE__)}/../../shared_bench/shared.rb" 
    1494 
    1505begin 
     
    15813  ENV["EVENT"] = "1" 
    15914  `mongrel_rails start -d -p 4000 -e production` 
    160   sleep 5 
     15 
    16116  puts Dir[File.join(File.expand_path(__FILE__), "..", "log", "*.pid")] 
    16217  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 
    16419 
    165   ps = `ps aux #{pid}` 
     20  sleep 0.05 until ps = `ps aux #{pid}` 
     21   
    16622  memsize = (ps.split("\n")[1].split[5].to_f / 10.24).round / 100.0 
    16723 
    16824  @err.puts "* Memory size before benchmarking: #{memsize}MB" 
    16925 
    170   TIMES = @options[:times] || 1000 
    171   CNCRC = @options[:concurrency]|| 5  
     26  @times = @options[:times] || 1000 
     27  @cncrc = @options[:concurrency]|| 5  
    17228 
    17329  run_scenarios