Benchmark Oga XPath evaluation without the parser.

This gives better insight in the performance of the evaluator itself.
This commit is contained in:
Yorick Peterse 2014-09-02 20:55:42 +02:00
parent 84d6ba96c2
commit d92133ef43
2 changed files with 22 additions and 2 deletions

View File

@ -14,6 +14,9 @@ rex_doc = REXML::Document.new(xml)
ox_exp = 'number/^Text' ox_exp = 'number/^Text'
xpath_exp = 'root/number/text()' xpath_exp = 'root/number/text()'
oga_ast = Oga::XPath::Parser.new(xpath_exp).parse
evaluator = Oga::XPath::Evaluator.new(oga_doc)
Benchmark.ips do |bench| Benchmark.ips do |bench|
# Technically not XPath but it's the closest thing Ox provides. # Technically not XPath but it's the closest thing Ox provides.
bench.report 'Ox' do bench.report 'Ox' do
@ -28,6 +31,12 @@ Benchmark.ips do |bench|
oga_doc.xpath(xpath_exp) oga_doc.xpath(xpath_exp)
end end
# This is measured to see what the performance of the evaluator is _without_
# the overhead of the lexer/parser.
bench.report 'Oga cached' do
evaluator.evaluate_ast(oga_ast)
end
bench.report 'REXML' do bench.report 'REXML' do
REXML::XPath.match(rex_doc, xpath_exp) REXML::XPath.match(rex_doc, xpath_exp)
end end

View File

@ -79,10 +79,21 @@ module Oga
# evaluator.evaluate('//a') # evaluator.evaluate('//a')
# #
# @param [String] string An XPath expression as a String. # @param [String] string An XPath expression as a String.
# @return [Oga::XML::Node] # @return [Mixed]
# #
def evaluate(string) def evaluate(string)
ast = Parser.new(string).parse ast = Parser.new(string).parse
return evaluate_ast(ast)
end
##
# Evaluates a pre-parsed XPath expression.
#
# @param [Oga::XPath::Node] ast_node
# @return [Mixed]
#
def evaluate_ast(ast)
context = XML::NodeSet.new([@document]) context = XML::NodeSet.new([@document])
return process(ast, context) return process(ast, context)