Sha256: 83716c5ce68d1dde7050251aa4c957044d72d8273678640a83ce06dcdf399c17

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

module Bench
  class Session
    include Logging
    include Timer
    attr_accessor :cookies, :last_result, :results, :thread_id, :iteration, :client_id
    
    def initialize(thread_id,iteration)
      @cookies = {}
      @results = {}
      @thread_id,@iteration = thread_id,iteration
    end
      
    def post(marker,url,headers={})
      @body = yield
      _request(marker,:_post,url,headers)
    end
    
    def get(marker,url,headers={})
      params = yield if block_given?
      url_params = url.clone
      url_params << "?" + _url_params(params) if params
      _request(marker,:_get,url_params,headers)          
    end
    
    protected
    def _request(marker,verb,url,headers)
      result = Result.new(marker,verb,url,@thread_id,@iteration)
      @results[result.marker] ||= []
      @results[result.marker] << result
      begin
        result.time = time do
          headers.merge!(:cookies => @cookies)
          result.last_response = send(verb,url,headers)
          @last_result = result
        end
        bench_log "#{log_prefix} #{verb.to_s.upcase.gsub(/_/,'')} #{url} #{@last_result.code} #{result.time}"      
      rescue RestClient::Exception => e
        result.error = e
        bench_log "#{log_prefix} #{verb.to_s.upcase.gsub(/_/,'')} #{url}"      
        bench_log "#{log_prefix} #{e.http_code.to_s} #{e.message}\n"
        raise e
      end
      @last_result.cookies['rhoconnect_session'] = 
        CGI.escape(@last_result.cookies['rhoconnect_session']) if @last_result.cookies['rhoconnect_session']
      @cookies = @cookies.merge(@last_result.cookies)
      @last_result
    end
    
    def _get(url,headers)
      #bench_log "GET #{url}"
      RestClient.get(url, headers)
    end
    
    def _post(url,headers)
      #bench_log "POST #{url}"
      RestClient.post(url, @body, headers)
    end
    
    def _url_params(params)
      elements = []
      params.each do |key,value|
        elements << "#{key}=#{value}"
      end
      elements.join('&')
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rhoconnect-3.0.5 bench/lib/bench/session.rb
rhoconnect-3.0.4 bench/lib/bench/session.rb
rhoconnect-3.0.3 bench/lib/bench/session.rb
rhoconnect-3.0.2 bench/lib/bench/session.rb
rhoconnect-3.0.1 bench/lib/bench/session.rb
rhoconnect-3.0.0 bench/lib/bench/session.rb
rhoconnect-3.0.0.rc1 bench/lib/bench/session.rb
rhoconnect-3.0.0.beta3 bench/lib/bench/session.rb
rhoconnect-3.0.0.beta1 bench/lib/bench/session.rb