File: benchmark.rb

package info (click to toggle)
ruby-rdiscount 2.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 412 kB
  • ctags: 578
  • sloc: ansic: 4,122; ruby: 447; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'rubygems'

iterations = 100
test_file = "#{File.dirname(__FILE__)}/benchmark.txt"
implementations = %w[BlueCloth RDiscount Maruku PEGMarkdown]

# Attempt to require each implementation and remove any that are not
# installed.
implementations.reject! do |class_name|
  begin
    module_path =
      if class_name == 'PEGMarkdown'
        'peg_markdown'
      else
        class_name.downcase
      end
    require module_path
    false
  rescue LoadError => boom
    module_path.tr! '_', '-'
    puts "#{class_name} excluded. Try: gem install #{module_path}"
    true
  end
end

# Grab actual class objects.
implementations.map! { |class_name| Object.const_get(class_name) }

# The actual benchmark.
def benchmark(implementation, text, iterations)
  start = Time.now
  iterations.times do |i|
    implementation.new(text).to_html
  end
  Time.now - start
end

# Read test file
test_data = File.read(test_file)

# Prime the pump
puts "Spinning up ..."
implementations.each { |impl| benchmark(impl, test_data, 1) }

# Run benchmarks; gather results.
puts "Running benchmarks ..."
results =
  implementations.inject([]) do |r,impl|
    GC.start
    r << [ impl, benchmark(impl, test_data, iterations) ]
  end

puts "Results for #{iterations} iterations:"
results.each do |impl,time|
  printf "  %10s %09.06fs total time, %09.06fs average\n", "#{impl}:", time, time / iterations
end