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.


Download the gem or source from


None save for Ruby 1.8.x.


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'.


require 'rubygems' require_gem 'threadsafe_benchmark' include ThreadsafeBenchmark

threads = [] max_num = 5000

5.to_i.times { |i|

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


threads.each { |t| t.join }


This code is released under the MIT license.


The RubyForge page for all RHG-related plugins is

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