Class: CelluloidBenchmark::BenchmarkRun

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/celluloid_benchmark/benchmark_run.rb

Overview

A test run of a scenario. Holds response times, codes, and requests. Reports results as an array of Benchmarks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBenchmarkRun



17
18
19
20
21
22
23
24
25
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 17

def initialize
  if !Dir.exists?("log")
    FileUtils.mkdir "log"
  end

  # Could replace with Celluloid.logger
  @logger = ::Logger.new("log/benchmark.log")
  @thresholds = Hash.new
end

Instance Attribute Details

#ended_atObject

Returns the value of attribute ended_at.



11
12
13
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 11

def ended_at
  @ended_at
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 12

def logger
  @logger
end

#started_atObject

Returns the value of attribute started_at.



13
14
15
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 13

def started_at
  @started_at
end

#thresholdsObject (readonly)

Returns the value of attribute thresholds.



15
16
17
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 15

def thresholds
  @thresholds
end

#visitorsObject

Returns the value of attribute visitors.



14
15
16
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 14

def visitors
  @visitors
end

Instance Method Details

#benchmarksObject



71
72
73
74
75
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 71

def benchmarks
  response_times.map do |label, response_times|
    CelluloidBenchmark::Benchmark.new label, thresholds[label], response_times, response_codes[label]
  end
end

#elapsed_timeObject



85
86
87
88
89
90
91
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 85

def elapsed_time
  if started_at && ended_at
    (ended_at - started_at).to_f
  else
    0
  end
end

#inspectObject



97
98
99
100
101
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 97

def inspect
  response_times.map do |label, response_times|
    "#{label} #{response_times.reduce(:+) / response_times.size} #{response_times.min} #{response_times.max} #{response_times.size}"
  end
end

#log(http_status_code, start_time, end_time, server_response_time, label, threshold) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 27

def log(http_status_code, start_time, end_time, server_response_time, label, threshold)
  response_codes[label] << http_status_code.to_i

  network_and_server_time = network_and_server_time(start_time, end_time)

  if server_response_time
    response_times[label] << server_response_time
    network_times[label] << network_and_server_time - server_response_time
  else
    response_times[label] << network_and_server_time
    network_times[label] << network_and_server_time
  end

  if threshold
    thresholds[label] = threshold
  end

  logger.info "#{http_status_code} #{network_and_server_time} #{label}"
end

#mark_endObject



81
82
83
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 81

def mark_end
  @ended_at = Time.now
end

#mark_startObject



77
78
79
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 77

def mark_start
  @started_at = Time.now
end

#network_timeObject



63
64
65
66
67
68
69
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 63

def network_time
  requests = network_times.values.flatten

  if requests.size > 0
    requests.reduce(:+) / requests.size
  end
end

#network_timesObject



59
60
61
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 59

def network_times
  @network_times ||= Hash.new { |hash, value| hash[value] = [] }
end

#ok?Boolean



93
94
95
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 93

def ok?
  benchmarks.all?(&:ok?)
end

#requestsObject



55
56
57
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 55

def requests
  response_times.values.compact.map(&:size).reduce(0, &:+)
end

#response_codesObject



51
52
53
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 51

def response_codes
  @response_codes ||= Hash.new { |hash, value| hash[value] = [] }
end

#response_timesObject



47
48
49
# File 'lib/celluloid_benchmark/benchmark_run.rb', line 47

def response_times
  @response_times ||= Hash.new { |hash, value| hash[value] = [] }
end