Summary

When testing products such as services which need to be stress tested as much as possible, it’s necessary to use multi-threading to get as close to real world usage as possible. To reduce the duplication of code, this plugin utilizes Ruby’s built-in Benchmark for the base functionality while preventing the output from clobbering through the use of thread-specific IO buffers.

Install

Download the gem or source from rubyforge.org/projects/revhealth/

Dependencies

None save for Ruby 1.8.x.

Usage

Anywhere you need to do multi-threaded testing, just use ‘threadsafe_bm’ instead of the standard ‘bm’ call. The method has also been aliased as ‘ts_bm’.

Sample

require ‘rubygems’ require_gem ‘threadsafe_benchmark’ include ThreadsafeBenchmark

threads = [] max_num = 5000

5.to_i.times { |i|

threads << Thread.new(max_num) { |n| 
  threadsafe_bm(6) { |x|
    x.report("for:")   { for i in 1..n; a = "1"; end }
    x.report("times:") { n.times do   ; a = "1"; end }
    x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  }
}

}

threads.each { |t| t.join }

License

This code is released under the MIT license.

Support

The RubyForge page for all RHG-related plugins is rubyforge.org/projects/revhealth

You can email the RHG Rails team at [email protected]