Class: Puma::Benchmark::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/benchmark/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Core

Returns a new instance of Core.



8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#current_dirObject



18
19
20
# File 'lib/puma/benchmark/core.rb', line 18

def current_dir
  File.expand_path(File.dirname('.'))
end

#pid_fileObject



22
23
24
# File 'lib/puma/benchmark/core.rb', line 22

def pid_file
  "#{current_dir}/tmp/pids/server.pid"
end


54
55
56
57
58
59
60
61
62
63
# File 'lib/puma/benchmark/core.rb', line 54

def print_timer
  fork{
    p 'Sending request for 30 secs'
    30.times do |n|
      print "    #{30 - n} sec left \r"
      $stdout.flush
      sleep(1)
    end
  }
end

#process(worker_count, thread_count) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

#runObject



26
27
28
29
30
31
32
33
# 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