2014-05-25 22:31:03 +00:00
|
|
|
require 'stringio'
|
2014-05-01 11:08:44 +00:00
|
|
|
require 'benchmark'
|
|
|
|
require 'benchmark/ips'
|
|
|
|
|
|
|
|
require_relative '../lib/oga'
|
|
|
|
|
2014-05-25 22:31:03 +00:00
|
|
|
# @return [File]
|
|
|
|
def big_xml_file
|
2015-08-18 20:32:57 +00:00
|
|
|
File.open(File.expand_path('../fixtures/big.xml', __FILE__), 'r')
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [File]
|
|
|
|
def big_kaf_file
|
|
|
|
File.open(File.expand_path('../fixtures/kaf.xml', __FILE__), 'r')
|
2014-05-25 22:31:03 +00:00
|
|
|
end
|
|
|
|
|
2014-05-01 11:08:44 +00:00
|
|
|
# @return [String]
|
|
|
|
def read_big_xml
|
2015-08-18 20:32:57 +00:00
|
|
|
big_xml_file.read
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [String]
|
|
|
|
def read_big_kaf
|
|
|
|
big_kaf_file.read
|
2014-05-01 11:08:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [String]
|
|
|
|
def read_html
|
2015-03-04 22:14:43 +00:00
|
|
|
return File.read(File.expand_path('../fixtures/with_entities.html', __FILE__))
|
2014-05-01 11:08:44 +00:00
|
|
|
end
|
2014-05-01 11:43:33 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Benchmarks the average runtime of the given block.
|
|
|
|
#
|
|
|
|
# @param [Fixnum] amount The amount of times to call the block.
|
|
|
|
# @param [Fixnum] precision
|
|
|
|
#
|
|
|
|
def measure_average(amount = 10, precision = 3)
|
|
|
|
timings = []
|
|
|
|
|
|
|
|
amount.times do |iter|
|
|
|
|
timing = Benchmark.measure { yield }.real
|
|
|
|
|
|
|
|
timings << timing.real
|
|
|
|
|
|
|
|
puts "Iteration: #{iter + 1}: #{timing.real.round(precision)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
average = timings.inject(:+) / timings.length
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Iterations: #{amount}"
|
|
|
|
puts "Average: #{average.round(precision)} sec"
|
|
|
|
end
|