| 1 |
require 'rubygems' |
| 2 |
|
| 3 |
iterations = 100 |
| 4 |
test_file = "#{File.dirname(__FILE__)}/benchmark.txt" |
| 5 |
implementations = %w[BlueCloth RDiscount Maruku PEGMarkdown] |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
implementations.reject! do |class_name| |
| 10 |
begin |
| 11 |
module_path = |
| 12 |
if class_name == 'PEGMarkdown' |
| 13 |
'peg_markdown' |
| 14 |
else |
| 15 |
class_name.downcase |
| 16 |
end |
| 17 |
require module_path |
| 18 |
false |
| 19 |
rescue LoadError => boom |
| 20 |
module_path.tr! '_', '-' |
| 21 |
puts "#{class_name} excluded. Try: gem install #{module_path}" |
| 22 |
true |
| 23 |
end |
| 24 |
end |
| 25 |
|
| 26 |
|
| 27 |
implementations.map! { |class_name| Object.const_get(class_name) } |
| 28 |
|
| 29 |
|
| 30 |
def benchmark(implementation, text, iterations) |
| 31 |
start = Time.now |
| 32 |
iterations.times do |i| |
| 33 |
implementation.new(text).to_html |
| 34 |
end |
| 35 |
Time.now - start |
| 36 |
end |
| 37 |
|
| 38 |
|
| 39 |
test_data = File.read(test_file) |
| 40 |
|
| 41 |
|
| 42 |
puts "Spinning up ..." |
| 43 |
implementations.each { |impl| benchmark(impl, test_data, 1) } |
| 44 |
|
| 45 |
|
| 46 |
puts "Running benchmarks ..." |
| 47 |
results = |
| 48 |
implementations.inject([]) do |r,impl| |
| 49 |
GC.start |
| 50 |
r << [ impl, benchmark(impl, test_data, iterations) ] |
| 51 |
end |
| 52 |
|
| 53 |
puts "Results for #{iterations} iterations:" |
| 54 |
results.each do |impl,time| |
| 55 |
printf " %10s %09.06fs total time, %09.06fs average\n", "#{impl}:", time, time / iterations |
| 56 |
end |