Skip to content

Instantly share code, notes, and snippets.

@pwgustafson
Last active August 29, 2015 14:22
Show Gist options
  • Save pwgustafson/b2abd6a1f21528e54961 to your computer and use it in GitHub Desktop.
Save pwgustafson/b2abd6a1f21528e54961 to your computer and use it in GitHub Desktop.
Beanstalk Benchmark
require 'beaneater'
require 'JSON'
class SimpleBenchmarker
def run(&block)
start_time = Time.now
block.call
end_time = Time.now
elapsed = end_time - start_time
p "Elapsed Time: #{elapsed} seconds"
end
end
# Connect to pool
@beanstalk = Beaneater.new('localhost:12345')
# Enqueue jobs to tube
@tube = @beanstalk.tubes["my-tube"]
@tube.clear
@jobs = 10_000
b1 = SimpleBenchmarker.new
p "Adding #{@jobs} jobs"
b1.run do
@jobs.times do |i|
time = Time.now
@tube.put "{ 'job_created' : '#{time}' }"
end
end
# # Process jobs from tube
b2 = SimpleBenchmarker.new
p "processising #{@jobs} jobs"
processed = 0
b2.run do
while @tube.peek(:ready)
job = @tube.reserve
print "." if @tube.stats.current_jobs_ready if processed % 1000 == 0
job.delete
processed += 1
end
end
# Disconnect the pool
@beanstalk.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment