Benchmark for lexer lines/second.

This benchmark uses a fixture file that is automatically downloaded.
This commit is contained in:
Yorick Peterse 2014-04-17 20:06:24 +02:00
parent 54e6650338
commit 6f1ce17b31
4 changed files with 43 additions and 1 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@ Gemfile.lock
lib/oga/xml/lexer.rb
lib/oga/xml/parser.rb
benchmark/fixtures/big.xml

View File

@ -8,7 +8,13 @@ GEMSPEC = Gem::Specification.load('oga.gemspec')
LEXER_OUTPUT = 'lib/oga/xml/lexer.rb'
PARSER_OUTPUT = 'lib/oga/xml/parser.rb'
GENERATED_FILES = ['coverage', 'yardoc', LEXER_OUTPUT, PARSER_OUTPUT]
GENERATED_FILES = [
'coverage',
'yardoc',
LEXER_OUTPUT,
PARSER_OUTPUT,
'benchmark/fixtures/big.xml'
]
GENERATED_FILES.each do |file|
CLEAN << file if File.exist?(file)

View File

@ -0,0 +1,25 @@
require_relative '../../lib/oga'
require 'benchmark'
fixture = File.expand_path('../../fixtures/big.xml', __FILE__)
unless File.file?(fixture)
system('rake fixtures')
puts
end
xml = File.read(fixture)
lines = xml.lines.count
lexer = Oga::XML::Lexer.new(xml)
timings = Benchmark.bm(20) do |bench|
bench.report("#{lines} lines") do
lexer.advance { |_| }
lexer.reset
end
end
time = timings[0].real
rate = lines / time
puts
puts "Lines/second: #{rate.round(3)}"

9
task/fixtures.rake Normal file
View File

@ -0,0 +1,9 @@
desc 'Generates large XML fixtures'
task :fixtures do
dest = File.expand_path('../../benchmark/fixtures/big.xml.gz', __FILE__)
unless File.file?(dest)
sh "wget http://downloads.yorickpeterse.com/files/big_xml_file.xml.gz -O #{dest}"
sh "gunzip #{dest}"
end
end