| 1 |
require 'benchmark' |
| 2 |
$: << File.expand_path('../../lib', __FILE__) |
| 3 |
require 'coderay' |
| 4 |
|
| 5 |
if ARGV.include? '-h' |
| 6 |
puts DATA.read |
| 7 |
exit |
| 8 |
end |
| 9 |
|
| 10 |
lang = ARGV.fetch(0, 'ruby') |
| 11 |
data = nil |
| 12 |
File.open(File.expand_path("../example.#{lang}", __FILE__), 'rb') { |f| data = f.read } |
| 13 |
raise 'Example file is empty.' if data.empty? |
| 14 |
|
| 15 |
format = ARGV.fetch(1, 'html').downcase |
| 16 |
encoder = CodeRay.encoder(format) |
| 17 |
|
| 18 |
size = ARGV.fetch(2, 3000).to_i * 1000 |
| 19 |
unless size.zero? |
| 20 |
data += data until data.size >= size |
| 21 |
data = data[0, size] |
| 22 |
end |
| 23 |
size = data.size |
| 24 |
puts "encoding %d kB of #{lang} code to #{format}..." % [(size / 1000.0).round] |
| 25 |
|
| 26 |
n = ARGV.fetch(3, 10).to_s[/\d+/].to_i |
| 27 |
require 'profile' if ARGV.include? '-p' |
| 28 |
times = [] |
| 29 |
n.times do |i| |
| 30 |
time = Benchmark.realtime { encoder.encode(data, lang) } |
| 31 |
puts "run %d: %5.2f s, %4.0f kB/s" % [i + 1, time, size / time / 1000.0] |
| 32 |
times << time |
| 33 |
end |
| 34 |
|
| 35 |
times_sum = times.inject(0) { |time, sum| sum + time } |
| 36 |
puts 'Average time: %5.2f s, %4.0f kB/s' % [times_sum / times.size, (size * n) / times_sum / 1000.0] |
| 37 |
puts 'Best time: %5.2f s, %4.0f kB/s' % [times.min, size / times.min / 1000.0] |
| 38 |
|
| 39 |
__END__ |
| 40 |
Usage: |
| 41 |
ruby bench.rb [lang] [format] [size in kB] [number of runs] |
| 42 |
|
| 43 |
- lang defaults to ruby. |
| 44 |
- format defaults to html. |
| 45 |
- size defaults to 1000 kB (= 1,000,000 bytes). 0 uses the whole example input. |
| 46 |
- number of runs defaults to 5. |
| 47 |
|
| 48 |
-h prints this help |
| 49 |
-p generates a profile (slow, use with SIZE = 1) |
| 50 |
-w waits after the benchmark (for debugging memory usw) |