Class: Drnbench::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/drnbench/client/http.rb

Direct Known Subclasses

HttpDroongaClient

Constant Summary collapse

SUPPORTED_HTTP_METHODS =
["GET", "POST"]
@@count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ HttpClient

Returns a new instance of HttpClient.


29
30
31
32
33
34
35
36
# File 'lib/drnbench/client/http.rb', line 29

def initialize(params)
  @runner   = params[:runner]
  @config   = params[:config]
  @count    = 0
  @id       = @@count
  @@count += 1
  @thread = nil
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results


23
24
25
# File 'lib/drnbench/client/http.rb', line 23

def results
  @results
end

#runnerObject (readonly)

Returns the value of attribute runner


23
24
25
# File 'lib/drnbench/client/http.rb', line 23

def runner
  @runner
end

#waitObject (readonly)

Returns the value of attribute wait


23
24
25
# File 'lib/drnbench/client/http.rb', line 23

def wait
  @wait
end

Instance Method Details

#runObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/drnbench/client/http.rb', line 38

def run
  @thread = Thread.new do
    clients = {}
    start_time = Time.now
    loop do
      if @runner.empty?
        puts "WORNING: requests queue becomes empty! (#{Time.now - start_time} sec)"
        stop
        break
      end

      request = @runner.pop_request
      request = fixup_request(request)

      client_params = {
        :protocol => :http,
        :host     => request["host"],
        :port     => request["port"],
        :timeout  => request["timeout"],
      }
      client = clients[client_params.to_s] ||= Droonga::Client.new(client_params)

      request["headers"] ||= {}
      request["headers"]["user-agent"] = "Ruby/#{RUBY_VERSION} Droonga::Benchmark::Runner::HttpClient"
      start_time = Time.now
      begin
      response = client.request(request)
        @runner.push_result(
          :request => request,
          :status => response.code,
          :elapsed_time => Time.now - start_time,
          :client => @id,
          :index => @count,
        )
      rescue Timeout::Error
        @runner.push_result(
          :request => request,
          :status => "0",
          :elapsed_time => Time.now - start_time,
          :client => @id,
          :index => @count,
        )
      end
      @count += 1
      sleep @config.wait
    end
  end
  self
end

#running?Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/drnbench/client/http.rb', line 95

def running?
  not @thread.nil?
end

#stopObject


88
89
90
91
92
93
# File 'lib/drnbench/client/http.rb', line 88

def stop
  return unless @thread

  @thread.exit
  @thread = nil
end