Thursday, December 13, 2007

ThreadsafeBenchmark

When testing products such as services which need to be stress-tested prior to release, it's necessary to use multi-threading to get as close to real world usage as possible. This gem, though not intended to be a replacement for full-fledged testing suites such as LoadRunner, can provide instantaneous results to facilitate TDD programming. To reduce the duplication of code, the gem utilizes Ruby's built-in Benchmark module for the base functionality while preventing the output from clobbering through the use of thread-specific IO buffers.

require '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 }

Using the standard Benchmark, the results would be printed haphazardly making it difficult to read and interpret. But ThreadsafeBenchmark cleans everything up giving us nicely laid out columns.

usersystemtotalreal
for0.000000.000000.000000( 0.002889)
times0.000000.000000.000000( 0.002477)
upto0.000000.000000.000000( 0.002479)
for0.000000.000000.000000( 0.002401)
times0.010000.000000.010000( 0.002586)
upto0.000000.000000.000000( 0.002413)
for0.000000.000000.000000( 0.002205)
times0.010000.000000.010000( 0.002245)
upto0.000000.000000.000000( 0.002272)
for0.000000.000000.000000( 0.001822)
times0.010000.000000.010000( 0.001958)
upto0.000000.000000.000000( 0.001943)
for0.010000.000000.010000( 0.010090)
times0.010000.000000.010000( 0.009225)
upto0.010000.000000.010000( 0.007986)

The gem and source files are available at Rubyforge.

No comments: