Class: Baracus::Httperf

Inherits:
Object
  • Object
show all
Defined in:
lib/httperf.rb

Class Method Summary collapse

Class Method Details

.create_read_wsesslog(date) ⇒ Object



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
# File 'lib/httperf.rb', line 57

def self.create_read_wsesslog(date)
  # parse _all_docs to get all the doc ids for reads
  if Baracus::Config.user && Baracus::Config.password
    all_docs_json = RestClient.get("http://#{Baracus::Config.user}:#{Baracus::Config.password}@#{Baracus::Config.host}:#{Baracus::Config.port}/#{Baracus::Config.db}/_all_docs")
  else
    all_docs_json = RestClient.get("http://#{Baracus::Config.host}:#{Baracus::Config.port}/#{Baracus::Config.db}/_all_docs")
  end

  all_docs = JSON.parse(all_docs_json)
  all_docs_rows = all_docs["rows"]

  # create the httperf wsesslog file
  filename = "httperf_read_wsesslog_#{date.to_i}"
  read_wsesslog = File.new(filename, "w")
  (1..Baracus::Config.sessions).each do |session|
    read_wsesslog.puts "# session #{session}"
    (1..Baracus::Config.reads).each do |read|
      # pick a random doc
      doc_index = rand(all_docs_rows.length)
      read_wsesslog.puts "/#{Baracus::Config.db}/#{all_docs_rows[doc_index]["id"]} method=GET"
    end
    read_wsesslog.puts ""
  end
  read_wsesslog.close
  filename
end

.create_write_wsesslog(date) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/httperf.rb', line 31

def self.create_write_wsesslog(date)
  # create some random data for the doc writes
  chars = ('a'..'z').to_a + ('A'..'Z').to_a
  doc = (0...Baracus::Config.doc_size).collect { chars[rand(chars.length)] }.join

  # use batch=ok for writes?
  if Baracus::Config.batchok
    batchok = "?batch=ok"
  else
    batchok = ""
  end

  # create the httperf wsesslog file
  filename = "httperf_write_wsesslog_#{date.to_i}"
  write_wsesslog = File.new(filename, "w")
  (1..Baracus::Config.sessions).each do |session|
    write_wsesslog.puts "# session #{session}"
    (1..Baracus::Config.writes).each do |write|
      write_wsesslog.puts "/#{Baracus::Config.db}/#{batchok} method=POST contents=\'{\"field\": \"#{doc}\"}\'"
    end
    write_wsesslog.puts ""
  end
  write_wsesslog.close
  filename
end

.run_httperf(wsesslog) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/httperf.rb', line 85

def self.run_httperf(wsesslog)
  # for basic auth
  if Baracus::Config.user && Baracus::Config.password
    auth = Base64.encode64("#{Baracus::Config.user}:#{Baracus::Config.password}")
    auth_switch = " --add-header 'Authorization: Basic #{auth}'"
  else
    auth_switch = ""
  end
  
  unless File.exist?(Baracus::Config.httperf)
    abort("#{Baracus::Config.httperf} does not exist, please adjust config")
  end

  # run httperf with the wsesslog and write results to file
  httperf_cmd = <<-EOH
    #{Baracus::Config.httperf} \
      --hog \
      --rate=#{Baracus::Config.rate} \
      --timeout=#{Baracus::Config.timeout} \
      --send-buffer=4096 \
      --recv-buffer=16384 \
      --max-connections=4 \
      --max-piped-calls=32 \
      --server=#{Baracus::Config.host} \
      --port=#{Baracus::Config.port} \
      --wsesslog=#{Baracus::Config.sessions},0,#{wsesslog} #{auth_switch}
  EOH

  results = ""
  status = popen4(httperf_cmd) do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      results << line
    end
  end

  results
end