class Puma::Benchmark::Core

Public Class Methods

new(params) click to toggle source
# File lib/puma/benchmark/core.rb, line 8
def initialize(params)
  @base_url    = params[:base_url] || 'http://localhost:3000'
  @api         = params[:api]
  @max_threads = (params[:threads] || 4).to_i 
  @max_workers = (params[:workers]|| 4).to_i
  @duration    = params[:duration] || 30
  @environment = params[:environment] || 'production'
  @result      = []
end

Public Instance Methods

current_dir() click to toggle source
# File lib/puma/benchmark/core.rb, line 18
def current_dir
  File.expand_path(File.dirname('.'))
end
pid_file() click to toggle source
# File lib/puma/benchmark/core.rb, line 22
def pid_file
  "#{current_dir}/tmp/pids/server.pid"
end
print_timer() click to toggle source
process(worker_count, thread_count) click to toggle source
# File lib/puma/benchmark/core.rb, line 35
def process(worker_count, thread_count)
  if !File.exist?(pid_file)
    fork {
      system("RAILS_ENV=#{@environment} puma -w #{worker_count} -t #{thread_count}")
    }
    sleep(5)
  end

  print_timer
  stdout, stderr, status = Open3.capture3("wrk -t 2 -c 100 -d #{@duration}s  #{@base_url}/#{@api}")
  pp stdout

  #p "Requests / sec: #{stdout.split(' ')[-3]}"
  req_per_sec = stdout.split(' ')[-3]
  @result << {workers: worker_count.to_s, threads: thread_count.to_s, req_per_sec: req_per_sec}
  system("kill #{File.read(pid_file)}")
  sleep(5)
end
run() click to toggle source
# File lib/puma/benchmark/core.rb, line 26
def run
  @max_workers.times do |worker_count|
    @max_threads.times do |thread_count|
      process(worker_count + 1, thread_count + 1)
    end
  end
  Puma::Benchmark::PrintResult.new.call(@result)
end