Class: RocketJob::Performance
- Inherits:
-
Object
- Object
- RocketJob::Performance
- Defined in:
- lib/rocket_job/performance.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#mongo_config ⇒ Object
Returns the value of attribute mongo_config.
-
#ruby ⇒ Object
Returns the value of attribute ruby.
-
#servers ⇒ Object
Returns the value of attribute servers.
-
#version ⇒ Object
Returns the value of attribute version.
-
#workers ⇒ Object
Returns the value of attribute workers.
Instance Method Summary collapse
- #count_running_workers ⇒ Object
-
#export_results(results) ⇒ Object
Export the Results hash to a CSV file.
-
#initialize ⇒ Performance
constructor
A new instance of Performance.
-
#parse(argv) ⇒ Object
Parse command line options.
-
#run_test_case(count = self.count) ⇒ Object
Loads the queue with jobs to be processed once the queue is loaded.
Constructor Details
#initialize ⇒ Performance
Returns a new instance of Performance.
8 9 10 11 12 13 14 15 16 |
# File 'lib/rocket_job/performance.rb', line 8 def initialize @version = RocketJob::VERSION @ruby = defined?(JRuby) ? "jruby_#{JRUBY_VERSION}" : "ruby_#{RUBY_VERSION}" @count = 100_000 @servers = 0 @workers = 0 @environment = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" @mongo_config = "config/mongoid.yml" end |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def count @count end |
#environment ⇒ Object
Returns the value of attribute environment.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def environment @environment end |
#mongo_config ⇒ Object
Returns the value of attribute mongo_config.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def mongo_config @mongo_config end |
#ruby ⇒ Object
Returns the value of attribute ruby.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def ruby @ruby end |
#servers ⇒ Object
Returns the value of attribute servers.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def servers @servers end |
#version ⇒ Object
Returns the value of attribute version.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def version @version end |
#workers ⇒ Object
Returns the value of attribute workers.
6 7 8 |
# File 'lib/rocket_job/performance.rb', line 6 def workers @workers end |
Instance Method Details
#count_running_workers ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rocket_job/performance.rb', line 95 def count_running_workers self.servers = 0 self.workers = 0 RocketJob::Server.running.each do |server| next if server.zombie? self.servers += 1 self.workers += server.heartbeat.workers end puts "Running: #{workers} workers, distributed across #{servers} servers" end |
#export_results(results) ⇒ Object
Export the Results hash to a CSV file
61 62 63 64 65 66 |
# File 'lib/rocket_job/performance.rb', line 61 def export_results(results) CSV.open("job_results_#{ruby}_#{servers}s_#{workers}w_v#{version}.csv", "wb") do |csv| csv << results.first.keys results.each { |result| csv << result.values } end end |
#parse(argv) ⇒ Object
Parse command line options
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rocket_job/performance.rb', line 69 def parse(argv) parser = OptionParser.new do |o| o.on("-c", "--count COUNT", "Count of jobs to enqueue") do |arg| self.count = arg.to_i end o.on("-m", "--mongo MONGO_CONFIG_FILE_NAME", "Path and filename of config file. Default: config/mongoid.yml") do |arg| self.mongo_config = arg end o.on("-e", "--environment ENVIRONMENT", "The environment to run the app on (Default: RAILS_ENV || RACK_ENV || development)") do |arg| self.environment = arg end end parser. = "rocketjob_perf <options>" parser.on_tail "-h", "--help", "Show help" do puts parser exit 1 end parser.parse! argv end |
#run_test_case(count = self.count) ⇒ Object
Loads the queue with jobs to be processed once the queue is loaded. Retain the first and last job for timings, all others are destroyed on completion.
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 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rocket_job/performance.rb', line 20 def run_test_case(count = self.count) if RocketJob::Server.where(:state.in => %w[running paused]).count.zero? raise "Please start servers before starting the performance test" end count_running_workers puts "Waiting for workers to pause" RocketJob::Server.pause_all RocketJob::Jobs::SimpleJob.delete_all # Wait for paused workers to stop loop do running = 0 RocketJob::Server.paused.each do |server| running += server.heartbeat.workers unless server.zombie? end puts "Waiting for #{running} workers" break if running.zero? sleep 1 end puts "Enqueuing jobs" first = RocketJob::Jobs::SimpleJob.create!(priority: 1, destroy_on_complete: false) (count - 2).times { RocketJob::Jobs::SimpleJob.create! } last = RocketJob::Jobs::SimpleJob.create!(priority: 100, destroy_on_complete: false) puts "Resuming workers" RocketJob::Server.resume_all sleep 3 until last.reload.completed? duration = last.reload.completed_at - first.reload.started_at first.destroy last.destroy {count: count, duration: duration, jobs_per_second: (count.to_f / duration).to_i} end |