|
1 |
| -# The most ugly test script I've ever written! |
2 |
| -# Shame on me! |
3 |
| - |
4 |
| -require 'pathname' |
5 |
| -require 'profile' if ARGV.include? '-p' |
6 |
| - |
7 |
| -MYDIR = File.dirname(__FILE__) |
8 |
| -LIBDIR = Pathname.new(MYDIR).join('..', 'lib').cleanpath.to_s |
9 |
| -$:.unshift MYDIR, LIBDIR |
| 1 | +require 'benchmark' |
| 2 | +$: << File.expand_path('../../lib', __FILE__) |
10 | 3 | require 'coderay'
|
11 | 4 |
|
12 |
| -@size = ARGV.fetch(2, 100).to_i * 1000 |
13 |
| - |
14 |
| -lang = ARGV.fetch(0) do |
15 |
| - puts <<-HELP |
16 |
| -Usage: |
17 |
| - ruby bench.rb (c|ruby|dump) (null|text|tokens|count|statistic|yaml|html) [size in kB] [stream] |
18 |
| -
|
19 |
| - SIZE defaults to 100 kB (= 100,000 bytes). |
20 |
| - SIZE = 0 means the whole input. |
21 |
| - SIZE is ignored when dump is input. |
22 |
| -
|
23 |
| --p generates a profile (slow! use with SIZE = 1) |
24 |
| --o shows the output |
25 |
| -stream enabled streaming mode |
26 |
| -
|
27 |
| -Sorry for the strange interface. I will improve it in the next release. |
28 |
| - HELP |
| 5 | +if ARGV.include? '-h' |
| 6 | + puts DATA.read |
29 | 7 | exit
|
30 | 8 | end
|
31 | 9 |
|
32 |
| -format = ARGV.fetch(1, 'html').downcase |
33 |
| - |
34 |
| -$stream = ARGV.include? 'stream' |
35 |
| -$optimize = ARGV.include? 'opt' |
36 |
| -$style = ARGV.include? 'style' |
37 |
| - |
38 |
| -require 'benchmark' |
39 |
| -require 'fileutils' |
| 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? |
40 | 14 |
|
41 |
| -if format == 'comp' |
42 |
| - format = 'page' |
43 |
| - begin |
44 |
| - require 'syntax' |
45 |
| - require 'syntax/convertors/html.rb' |
46 |
| - rescue LoadError |
47 |
| - puts 'Syntax no found!! (Try % gem install syntax)' |
48 |
| - end |
49 |
| -end |
50 |
| - |
51 |
| -$dump_input = lang == 'dump' |
52 |
| -$dump_output = format == 'dump' |
53 |
| -require 'coderay/helpers/gzip_simple.rb' if $dump_input |
| 15 | +format = ARGV.fetch(1, 'html').downcase |
| 16 | +encoder = CodeRay.encoder(format) |
54 | 17 |
|
55 |
| -def here fn = nil |
56 |
| - return MYDIR unless fn |
57 |
| - File.join here, fn |
| 18 | +size = ARGV.fetch(2, 1000).to_i * 1000 |
| 19 | +unless size.zero? |
| 20 | + data += data until data.size >= size |
| 21 | + data = data[0, size] |
58 | 22 | end
|
| 23 | +size = data.size |
| 24 | +puts "encoding %d kB of #{lang} code to #{format}..." % [(size / 1000.0).round] |
59 | 25 |
|
60 |
| -n = ARGV.find { |a| a[/^N/] } |
61 |
| -N = if n then n[/\d+/].to_i else 1 end |
62 |
| -$filename = ARGV.include?('strange') ? 'strange' : 'example' |
63 |
| - |
64 |
| -Benchmark.bm(20) do |bm| |
65 |
| -N.times do |
66 |
| - |
67 |
| - data = nil |
68 |
| - File.open(here("#$filename." + lang), 'rb') { |f| data = f.read } |
69 |
| - if $dump_input |
70 |
| - @size = CodeRay::Tokens.load(data).text.size |
71 |
| - else |
72 |
| - raise 'Example file is empty.' if data.empty? |
73 |
| - unless @size.zero? |
74 |
| - data += data until data.size >= @size |
75 |
| - data = data[0, @size] |
76 |
| - end |
77 |
| - @size = data.size |
78 |
| - end |
79 |
| - |
80 |
| - options = { |
81 |
| - :tab_width => 2, |
82 |
| - # :line_numbers => :inline, |
83 |
| - :css => $style ? :style : :class, |
84 |
| - } |
85 |
| - $hl = CodeRay.encoder(format, options) unless $dump_output |
86 |
| - time = bm.report('CodeRay') do |
87 |
| - if $stream || true |
88 |
| - if $dump_input |
89 |
| - raise 'Can\'t stream dump.' |
90 |
| - elsif $dump_output |
91 |
| - raise 'Can\'t dump stream.' |
92 |
| - end |
93 |
| - $o = $hl.encode(data, lang, options) |
94 |
| - else |
95 |
| - if $dump_input |
96 |
| - tokens = CodeRay::Tokens.load data |
97 |
| - else |
98 |
| - tokens = CodeRay.scan(data, lang) |
99 |
| - end |
100 |
| - tokens.optimize! if $optimize |
101 |
| - if $dump_output |
102 |
| - $o = tokens.optimize.dump |
103 |
| - else |
104 |
| - $o = tokens.encode($hl) |
105 |
| - end |
106 |
| - end |
107 |
| - end |
108 |
| - $file_created = here('test.' + |
109 |
| - ($dump_output ? 'dump' : $hl.file_extension)) |
110 |
| - File.open($file_created, 'wb') do |f| |
111 |
| - # f.write $o |
112 |
| - end |
113 |
| - Dir.chdir(here) do |
114 |
| - FileUtils.copy 'test.dump', 'example.dump' if $dump_output |
115 |
| - end |
116 |
| - |
117 |
| - time_real = time.real |
118 |
| - |
119 |
| - puts "\t%7.2f KB/s (%d.%d KB)" % [((@size / 1000.0) / time_real), @size / 1000, @size % 1000] |
120 |
| - puts $o if ARGV.include? '-o' |
121 |
| - |
122 |
| -end |
| 26 | +n = ARGV.fetch(3, 5).to_s[/\d+/].to_i |
| 27 | +require 'profile' if ARGV.include? '-p' |
| 28 | +n.times do |i| |
| 29 | + time = Benchmark.realtime { encoder.encode(data, lang) } |
| 30 | + puts "run %d: %5.2f s, %4.0f kB/s" % [i + 1, time, size / time / 1000.0] |
123 | 31 | end
|
124 |
| -puts "Files created: #$file_created" |
125 | 32 |
|
126 |
| -STDIN.gets if ARGV.include? 'wait' |
| 33 | +STDIN.gets if ARGV.include? '-w' |
127 | 34 |
|
128 | 35 | __END__
|
129 |
| -.ruby .normal {} |
130 |
| -.ruby .comment { color: #005; font-style: italic; } |
131 |
| -.ruby .keyword { color: #A00; font-weight: bold; } |
132 |
| -.ruby .method { color: #077; } |
133 |
| -.ruby .class { color: #074; } |
134 |
| -.ruby .module { color: #050; } |
135 |
| -.ruby .punct { color: #447; font-weight: bold; } |
136 |
| -.ruby .symbol { color: #099; } |
137 |
| -.ruby .string { color: #944; background: #FFE; } |
138 |
| -.ruby .char { color: #F07; } |
139 |
| -.ruby .ident { color: #004; } |
140 |
| -.ruby .constant { color: #07F; } |
141 |
| -.ruby .regex { color: #B66; background: #FEF; } |
142 |
| -.ruby .number { color: #F99; } |
143 |
| -.ruby .attribute { color: #7BB; } |
144 |
| -.ruby .global { color: #7FB; } |
145 |
| -.ruby .expr { color: #227; } |
146 |
| -.ruby .escape { color: #277; } |
| 36 | +Usage: |
| 37 | + ruby bench.rb [lang] [format] [size in kB] [number of runs] |
147 | 38 |
|
148 |
| -.xml .normal {} |
149 |
| -.xml .namespace { color: #B66; font-weight: bold; } |
150 |
| -.xml .tag { color: #F88; } |
151 |
| -.xml .comment { color: #005; font-style: italic; } |
152 |
| -.xml .punct { color: #447; font-weight: bold; } |
153 |
| -.xml .string { color: #944; } |
154 |
| -.xml .number { color: #F99; } |
155 |
| -.xml .attribute { color: #BB7; } |
| 39 | + - lang defaults to ruby. |
| 40 | + - format defaults to html. |
| 41 | + - size defaults to 1000 kB (= 1,000,000 bytes). 0 uses the whole example input. |
| 42 | + - number of runs defaults to 5. |
156 | 43 |
|
157 |
| -.yaml .normal {} |
158 |
| -.yaml .document { font-weight: bold; color: #07F; } |
159 |
| -.yaml .type { font-weight: bold; color: #05C; } |
160 |
| -.yaml .key { color: #F88; } |
161 |
| -.yaml .comment { color: #005; font-style: italic; } |
162 |
| -.yaml .punct { color: #447; font-weight: bold; } |
163 |
| -.yaml .string { color: #944; } |
164 |
| -.yaml .number { color: #F99; } |
165 |
| -.yaml .time { color: #F99; } |
166 |
| -.yaml .date { color: #F99; } |
167 |
| -.yaml .ref { color: #944; } |
168 |
| -.yaml .anchor { color: #944; } |
| 44 | +-h prints this help |
| 45 | +-p generates a profile (slow, use with SIZE = 1) |
| 46 | +-w waits after the benchmark (for debugging memory usw) |
0 commit comments