Class: Fropy::Benchmark

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

Instance Method Summary collapse

Constructor Details

#initialize(command, options) ⇒ Benchmark

Returns a new instance of Benchmark.



3
4
5
6
7
8
9
10
11
# File 'lib/fropy/benchmark.rb', line 3

def initialize(command, options)
  @command = command
  @options = {
    poll_interval: 0.05,
    dev_null: false,
    log: true,
    verbose: false
  }.merge(options)
end

Instance Method Details

#measureObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fropy/benchmark.rb', line 13

def measure
  log "Benchmarking #{@command}"
  log "Polling with interval of #{@options[:poll_interval]}s"
  log "Redirecting output to /dev/null" if @options[:dev_null]
  @max_memory = -1

  r_out, w_out = IO.pipe

  cmd = @command
  if @options[:dev_null]
    cmd << " > /dev/null"
  end

  @start_time = Time.now
  pid = Process.spawn cmd, out: w_out
  log "Spawned with pid: #{pid}"

  loop do
    status = Process.getpgid(pid) rescue nil
    break if status == nil
    current_memory = `ps -o rss= -p #{pid}`.to_i #kb
    puts "Using #{mem}kb" if @options[:verbose]
    @max_memory = current_memory if current_memory > @max_memory
    sleep @options[:poll_interval]
  end

  @end_time = Time.now
  @time = @end_time - @start_time

  w_out.close
  output = r_out.read
  r_out.close

  log "      Time: #{@time}s"
  log "Max Memory: #{@max_memory}"
end