Created
June 26, 2014 01:20
-
-
Save morhekil/26c6ed39da1bc54c67fc to your computer and use it in GitHub Desktop.
Multi-process stress load tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler/setup' | |
require 'paperclip' | |
require 'openstack_bridge' | |
require 'dotenv' | |
Dotenv.load | |
$:.unshift File.dirname(__FILE__) | |
$:.unshift File.join(File.dirname(__FILE__), '..') | |
require 'app/services/swift_file' | |
class Swifter < Struct.new(:workers) | |
attr_accessor :pool | |
def run | |
@pool = [] | |
while true | |
fork_new_worker if @pool.size < workers | |
wait_for_worker if @pool.size >= workers | |
end | |
end | |
def fork_new_worker | |
pid = fork do | |
sf = SwiftFile.new | |
sf.send(:os_container).exists?("file#{rand(1e3)}") | |
end | |
puts "#{pid}\tforked" | |
@pool << pid | |
end | |
def wait_for_worker | |
pid = Process.wait | |
@pool.delete pid | |
puts "#{pid}\tdone" | |
end | |
end | |
workers = ARGV.last.to_i > 0 ? ARGV.last.to_i : 10 | |
puts "Starting with #{workers} workers" | |
Swifter.new(workers).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment