# File lib/railsbench/railsbenchmark.rb, line 31
  def initialize(options={})
    unless @gc_frequency = options[:gc_frequency]
      @gc_frequency = 0
      ARGV.each{|arg| @gc_frequency = $1.to_i if arg =~ /-gc(\d+)/ }
    end

    @iterations = (options[:iterations] || 100).to_i

    @remote_addr = options[:remote_addr] || '127.0.0.1'
    @http_host =  options[:http_host] || '127.0.0.1'
    @server_port = options[:server_port] || '80'

    @session_data = options[:session_data] || {}
    @session_key = options[:session_key] || '_session_id'

    ENV['RAILS_ENV'] = 'benchmarking'

    begin
      require ENV['RAILS_ROOT'] + "/config/environment"
      require 'dispatcher' # make edge rails happy

      if Rails::VERSION::STRING >= "2.3"
        @rack_middleware = true
        require 'cgi/session'
        CGI.class_eval "def env_table\n@env_table ||= ENV.to_hash\nend\n"
      else
         @rack_middleware = false
      end

    rescue => e
      $stderr.puts "failed to load application environment"
      e.backtrace.each{|line| $stderr.puts line}
      $stderr.puts "benchmarking aborted"
      exit(-1)
    end

    # we don't want local error template output, which crashes anyway, when run under railsbench
    ActionController::Rescue.class_eval "def local_request?; false; end"

    # print backtrace and exit if action execution raises an exception
    ActionController::Rescue.class_eval "def rescue_action_in_public(exception)\n$stderr.puts \"benchmarking aborted due to application error: \" + exception.message\nexception.backtrace.each{|line| $stderr.puts line}\n$stderr.print \"clearing database connections ...\"\nActiveRecord::Base.send :clear_all_cached_connections! if ActiveRecord::Base.respond_to?(:clear_all_cached_connections)\nActiveRecord::Base.clear_all_connections! if ActiveRecord::Base.respond_to?(:clear_all_connections)\n$stderr.puts\nexit!(-1)\nend\n"

    # override rails ActiveRecord::Base#inspect to make profiles more readable
    ActiveRecord::Base.class_eval "def self.inspect\nsuper\nend\n"

    # make sure Rails doesn't try to read post data from stdin
    CGI::QueryExtension.module_eval "def read_body(content_length)\nENV['RAW_POST_DATA']\nend\n"

    if ARGV.include?('-path')
      $:.each{|f| STDERR.puts f}
      exit
    end

    logger_module = Logger
    if defined?(Log4r) && RAILS_DEFAULT_LOGGER.is_a?(Log4r::Logger)
      logger_module = Logger
    end
    default_log_level = logger_module.const_get("ERROR")
    log_level = options[:log] || default_log_level
    ARGV.each do |arg|
        case arg
        when '-log'
          log_level = default_log_level
        when '-log=(nil|none)'
          log_level = nil
        when /-log=([a-zA-Z]*)/
          log_level = logger_module.const_get($1.upcase) rescue default_log_level
        end
    end

    if log_level
      RAILS_DEFAULT_LOGGER.level = log_level
      ActiveRecord::Base.logger.level = log_level
      ActionController::Base.logger.level = log_level
      ActionMailer::Base.logger = level = log_level if defined?(ActionMailer)
    else
      RAILS_DEFAULT_LOGGER.level = logger_module.const_get "FATAL"
      ActiveRecord::Base.logger = nil
      ActionController::Base.logger = nil
      ActionMailer::Base.logger = nil if defined?(ActionMailer)
    end

    if options.has_key?(:perform_caching)
      ActionController::Base.perform_caching = options[:perform_caching]
    else
      ActionController::Base.perform_caching = false if ARGV.include?('-nocache')
      ActionController::Base.perform_caching = true if ARGV.include?('-cache')
    end

    if ActionView::Base.respond_to?(:cache_template_loading)
      if options.has_key?(:cache_template_loading)
        ActionView::Base.cache_template_loading = options[:cache_template_loading]
      else
        ActionView::Base.cache_template_loading = true
      end
    end

    self.relative_url_root = options[:relative_url_root] || ''

    @patched_gc = GC.collections.is_a?(Numeric) rescue false

    if ARGV.include? '-headers_only'
      require File.dirname(__FILE__) + '/write_headers_only'
    end

  end