diff options
author | murphy <murphy@rubychan.de> | 2006-07-10 00:32:57 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2006-07-10 00:32:57 +0000 |
commit | 9f4c7ab7553f9be7c9d14da0ba7462ad746c2f5d (patch) | |
tree | 686ee95cc9010314278846936476d1cf0bf04353 /bench | |
parent | 42436798565c36a7d9582fcf587946d273083ab1 (diff) | |
download | coderay-9f4c7ab7553f9be7c9d14da0ba7462ad746c2f5d.tar.gz |
Big re-indenting - no more tabs!
Diffstat (limited to 'bench')
-rw-r--r-- | bench/bench.rb | 232 |
1 files changed, 116 insertions, 116 deletions
diff --git a/bench/bench.rb b/bench/bench.rb index 7e3cbb8..a60d5eb 100644 --- a/bench/bench.rb +++ b/bench/bench.rb @@ -7,21 +7,21 @@ require 'coderay' @size = ARGV.fetch(2, 100).to_i * 2**10 # 2**10 = 1 Ki lang = ARGV.fetch(0) do - puts <<-HELP + puts <<-HELP Usage: ruby bench.rb (c|ruby|dump) (null|text|tokens|count|statistic|yaml|html) [SIZE in KB] [stream] - - SIZE defaults to 100. - SIZE = 0 means the whole input. - SIZE is ignored when dump is input. - + + SIZE defaults to 100. + SIZE = 0 means the whole input. + SIZE is ignored when dump is input. + -p generates a profile (slow! use with SIZE = 1) -o shows the output stream enabled streaming mode Sorry for the strange interface. I will improve it in the next release. - HELP - exit + HELP + exit end format = ARGV.fetch(1, 'html').downcase @@ -34,14 +34,14 @@ require 'benchmark' require 'fileutils' if format == 'comp' - format = 'html' - compare = true - begin - require 'syntax' - require 'syntax/convertors/html.rb' - rescue LoadError - raise 'This requires Syntax! (Try % gem install syntax)' - end + format = 'html' + compare = true + begin + require 'syntax' + require 'syntax/convertors/html.rb' + rescue LoadError + raise 'This requires Syntax! (Try % gem install syntax)' + end end $dump_input = lang == 'dump' @@ -50,8 +50,8 @@ require 'coderay/helpers/gzip_simple.rb' if $dump_input MYDIR = File.dirname __FILE__ def here fn = nil - return MYDIR unless fn - File.join here, fn + return MYDIR unless fn + File.join here, fn end n = ARGV.find { |a| a[/^N/] } @@ -64,104 +64,104 @@ $filename = ARGV.include?('strange') ? 'strange' : 'example' Benchmark.bm(20) do |bm| - data = nil - File.open(here("#$filename." + lang), 'rb') { |f| data = f.read } - if $dump_input - @size = CodeRay::Tokens.load(data).text_size - else - raise 'Example file is empty.' if data.empty? - unless @size.zero? - data += data until data.size >= @size - data = data[0, @size] - end - @size = data.size - end - - time = bm.report('CodeRay') do - options = { - :tab_width => 2, - :line_numbers => :inline, - :line_numbers_offset => Offset, - :bold_every => BoldEvery, - :wrap => :page, - :css => $style ? :style : :class, - } - options[:debug] = $DEBUG - $hl = CodeRay.encoder(format, options) unless $dump_output - N.times do - if $stream - if $dump_input - raise 'Can\'t stream dump.' - elsif $dump_output - raise 'Can\'t dump stream.' - end - $o = $hl.encode_stream(data, lang, options) - @token_count = $hl.token_stream.size - else - if $dump_input - tokens = CodeRay::Tokens.load data - else - tokens = CodeRay.scan(data, lang) - @token_count = tokens.size - end - @token_count = tokens.size - tokens.optimize! if $optimize - if $dump_output - $o = tokens.optimize.dump - else - $o = tokens.encode($hl) - end - end - end - $file_created = 'test.' + format - file = here($file_created) - File.open(file, 'wb') do |f| - f.write $o - end - end - Dir.chdir(here) do - FileUtils.copy 'test.dump', 'example.dump' if $dump_output - end - - time_real = time.real / N - - puts "\t%7.2f KB/sec (%d.%d KB)\t%0.2f KTok/sec" % [((@size / 1024.0) / time_real), @size / 1024, @size % 1024, ((@token_count / 1000.0) / time_real)] - puts $o if ARGV.include? '-o' - - if compare - time = bm.report('Syntax') do - c = Syntax::Convertors::HTML.for_syntax lang - puts 'No Syntax syntax found!' if c.tokenizer.is_a? Syntax::Default - begin - v = $VERBOSE - $VERBOSE = nil - N.times do - output = c.convert(data) - end - $VERBOSE = v - rescue => boom - output = boom.inspect - end - Dir.chdir(here) do - File.open('test.syntax.' + format, 'wb') do |f| - f.write '<html><head><style>%s</style></head><body><div class="ruby">%s</div></body></html>' % [DATA.read, output] - end - end - $file_created << ", test.syntax.#{format}" - end - puts "\t%7.2f KB/sec" % ((@size / 1024.0) / time.real) - - time = bm.report('SilverCity') do - Dir.chdir(here) do - File.open('input-data', 'w') { |f| f.write data } - N.times do - `c:/Python/Scripts/source2html.pyo --generator=#{lang} input-data > test.silvercity.html` - end - end - $file_created << ", test.silvercity.#{format}" - end - puts "\t%7.2f KB/sec" % ((@size / 1024.0) / time.real) - end + data = nil + File.open(here("#$filename." + lang), 'rb') { |f| data = f.read } + if $dump_input + @size = CodeRay::Tokens.load(data).text_size + else + raise 'Example file is empty.' if data.empty? + unless @size.zero? + data += data until data.size >= @size + data = data[0, @size] + end + @size = data.size + end + + time = bm.report('CodeRay') do + options = { + :tab_width => 2, + :line_numbers => :inline, + :line_numbers_offset => Offset, + :bold_every => BoldEvery, + :wrap => :page, + :css => $style ? :style : :class, + } + options[:debug] = $DEBUG + $hl = CodeRay.encoder(format, options) unless $dump_output + N.times do + if $stream + if $dump_input + raise 'Can\'t stream dump.' + elsif $dump_output + raise 'Can\'t dump stream.' + end + $o = $hl.encode_stream(data, lang, options) + @token_count = $hl.token_stream.size + else + if $dump_input + tokens = CodeRay::Tokens.load data + else + tokens = CodeRay.scan(data, lang) + @token_count = tokens.size + end + @token_count = tokens.size + tokens.optimize! if $optimize + if $dump_output + $o = tokens.optimize.dump + else + $o = tokens.encode($hl) + end + end + end + $file_created = 'test.' + format + file = here($file_created) + File.open(file, 'wb') do |f| + f.write $o + end + end + Dir.chdir(here) do + FileUtils.copy 'test.dump', 'example.dump' if $dump_output + end + + time_real = time.real / N + + puts "\t%7.2f KB/sec (%d.%d KB)\t%0.2f KTok/sec" % [((@size / 1024.0) / time_real), @size / 1024, @size % 1024, ((@token_count / 1000.0) / time_real)] + puts $o if ARGV.include? '-o' + + if compare + time = bm.report('Syntax') do + c = Syntax::Convertors::HTML.for_syntax lang + puts 'No Syntax syntax found!' if c.tokenizer.is_a? Syntax::Default + begin + v = $VERBOSE + $VERBOSE = nil + N.times do + output = c.convert(data) + end + $VERBOSE = v + rescue => boom + output = boom.inspect + end + Dir.chdir(here) do + File.open('test.syntax.' + format, 'wb') do |f| + f.write '<html><head><style>%s</style></head><body><div class="ruby">%s</div></body></html>' % [DATA.read, output] + end + end + $file_created << ", test.syntax.#{format}" + end + puts "\t%7.2f KB/sec" % ((@size / 1024.0) / time.real) + + time = bm.report('SilverCity') do + Dir.chdir(here) do + File.open('input-data', 'w') { |f| f.write data } + N.times do + `c:/Python/Scripts/source2html.pyo --generator=#{lang} input-data > test.silvercity.html` + end + end + $file_created << ", test.silvercity.#{format}" + end + puts "\t%7.2f KB/sec" % ((@size / 1024.0) / time.real) + end end puts "Files created: #$file_created" |