diff --git a/benchmark/benchmark_helper.rb b/benchmark/benchmark_helper.rb new file mode 100644 index 0000000..4704afc --- /dev/null +++ b/benchmark/benchmark_helper.rb @@ -0,0 +1,22 @@ +require 'benchmark' +require 'benchmark/ips' + +require_relative '../lib/oga' + +## +# Reads a big XML file and returns it as a String. +# +# @return [String] +# +def read_big_xml + return File.read(File.expand_path('../fixtures/big.xml', __FILE__)) +end + +## +# Reads a normal sized HTML fixture. +# +# @return [String] +# +def read_html + return File.read(File.expand_path('../fixtures/gist.html', __FILE__)) +end diff --git a/benchmark/lexer/big_xml_average.rb b/benchmark/lexer/big_xml_average.rb index 4b1360b..c9edbbe 100644 --- a/benchmark/lexer/big_xml_average.rb +++ b/benchmark/lexer/big_xml_average.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark' +require_relative '../benchmark_helper' -xml = File.read(File.expand_path('../../fixtures/big.xml', __FILE__)) +xml = read_big_xml amount = 10 timings = [] diff --git a/benchmark/lexer/big_xml_time.rb b/benchmark/lexer/big_xml_time.rb index 18c3e97..3a20745 100644 --- a/benchmark/lexer/big_xml_time.rb +++ b/benchmark/lexer/big_xml_time.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark' +require_relative '../benchmark_helper' -xml = File.read(File.expand_path('../../fixtures/big.xml', __FILE__)) +xml = read_big_xml Benchmark.bmbm(10) do |bench| bench.report '10MB of XML' do diff --git a/benchmark/lexer/cdata.rb b/benchmark/lexer/cdata.rb index 6235a08..e1bed6a 100644 --- a/benchmark/lexer/cdata.rb +++ b/benchmark/lexer/cdata.rb @@ -1,5 +1,4 @@ -require_relative '../../lib/oga' -require 'benchmark/ips' +require_relative '../benchmark_helper' string = 'Hello, how are you doing today?' small = "" diff --git a/benchmark/lexer/element.rb b/benchmark/lexer/element.rb index 32007a7..379544c 100644 --- a/benchmark/lexer/element.rb +++ b/benchmark/lexer/element.rb @@ -1,5 +1,4 @@ -require_relative '../../lib/oga' -require 'benchmark/ips' +require_relative '../benchmark_helper' simple = '
Hello world
' attributes = 'Hello world
' diff --git a/benchmark/lexer/html.rb b/benchmark/lexer/html.rb index 0d428c2..ba921f8 100644 --- a/benchmark/lexer/html.rb +++ b/benchmark/lexer/html.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark/ips' +require_relative '../benchmark_helper' -html = File.read(File.expand_path('../../fixtures/gist.html', __FILE__)) +html = read_html Benchmark.ips do |bench| bench.report 'lex HTML' do diff --git a/benchmark/lexer/html_time.rb b/benchmark/lexer/html_time.rb index 32516c5..fed6df9 100644 --- a/benchmark/lexer/html_time.rb +++ b/benchmark/lexer/html_time.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark' +require_relative '../benchmark_helper' -html = File.read(File.expand_path('../../fixtures/gist.html', __FILE__)) +html = read_html Benchmark.bmbm(20) do |bench| bench.report 'lex HTML' do diff --git a/benchmark/lexer/line_rate.rb b/benchmark/lexer/line_rate.rb index 6dcf4e2..fe35eeb 100644 --- a/benchmark/lexer/line_rate.rb +++ b/benchmark/lexer/line_rate.rb @@ -1,14 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark' +require_relative '../benchmark_helper' -fixture = File.expand_path('../../fixtures/big.xml', __FILE__) - -unless File.file?(fixture) - system('rake fixtures') - puts -end - -xml = File.read(fixture) +xml = read_big_xml lines = xml.lines.count lexer = Oga::XML::Lexer.new(xml) timings = Benchmark.bm(20) do |bench| diff --git a/benchmark/parser/big_xml_time.rb b/benchmark/parser/big_xml_time.rb index 1c19b37..0a68008 100644 --- a/benchmark/parser/big_xml_time.rb +++ b/benchmark/parser/big_xml_time.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark' +require_relative '../benchmark_helper' -xml = File.read(File.expand_path('../../fixtures/big.xml', __FILE__)) +xml = read_big_xml parser = Oga::XML::Parser.new(xml) Benchmark.bmbm(10) do |bench| diff --git a/benchmark/parser/element.rb b/benchmark/parser/element.rb index 44ecddd..9bb452e 100644 --- a/benchmark/parser/element.rb +++ b/benchmark/parser/element.rb @@ -1,5 +1,4 @@ -require_relative '../../lib/oga' -require 'benchmark/ips' +require_relative '../benchmark_helper' simple = 'Hello world
' attributes = 'Hello world
' diff --git a/benchmark/parser/html.rb b/benchmark/parser/html.rb index 634c479..e68a8de 100644 --- a/benchmark/parser/html.rb +++ b/benchmark/parser/html.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark/ips' +require_relative '../benchmark_helper' -html = File.read(File.expand_path('../../fixtures/gist.html', __FILE__)) +html = read_html Benchmark.ips do |bench| bench.report 'parse HTML' do diff --git a/benchmark/pull_parser/big_xml_time.rb b/benchmark/pull_parser/big_xml_time.rb index 7d79424..bbbeff2 100644 --- a/benchmark/pull_parser/big_xml_time.rb +++ b/benchmark/pull_parser/big_xml_time.rb @@ -1,7 +1,6 @@ -require_relative '../../lib/oga' -require 'benchmark' +require_relative '../benchmark_helper' -xml = File.read(File.expand_path('../../fixtures/big.xml', __FILE__)) +xml = read_big_xml parser = Oga::XML::PullParser.new(xml) Benchmark.bmbm(10) do |bench|