diff --git a/.gitignore b/.gitignore index 7ce2579..3359fde 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ Gemfile.lock lib/oga/xml/lexer.rb lib/oga/xml/parser.rb + +benchmark/fixtures/big.xml diff --git a/Rakefile b/Rakefile index 4024903..925b87b 100644 --- a/Rakefile +++ b/Rakefile @@ -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) diff --git a/benchmark/lexer/bench_line_rate.rb b/benchmark/lexer/bench_line_rate.rb new file mode 100644 index 0000000..6dcf4e2 --- /dev/null +++ b/benchmark/lexer/bench_line_rate.rb @@ -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)}" diff --git a/task/fixtures.rake b/task/fixtures.rake new file mode 100644 index 0000000..06ccf43 --- /dev/null +++ b/task/fixtures.rake @@ -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