/
benchmark.rb
65 lines (53 loc) · 1.82 KB
/
benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
$LOAD_PATH.push File.expand_path('../../lib', __FILE__)
require 'saxerator'
require 'benchmark'
file = ARGV.shift
unless File.exist?(file)
puts "Cannot find file #{file}"
exit 1
end
file = File.new(file)
ADAPTERS = %i[nokogiri ox oga rexml].freeze
class SaxeratorBenchmark
def initialize(file)
@file = file
end
def with_adapter(adapter) # rubocop:disable Metrics/MethodLength
puts '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
puts
puts "Benchmark with :#{adapter} parser"
puts
puts '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
puts
count = count2 = count3 = count4 = 0
Benchmark.bm do |x|
x.report('for_tag') do
Saxerator.parser(@file) { |confing| confing.adapter = adapter }
.for_tag(:artist).each { count += 1 }
end
x.report('at_depth') do
Saxerator.parser(@file) { |confing| confing.adapter = adapter }
.at_depth(2).each { count2 += 1 }
end
x.report('within') do
Saxerator.parser(@file) { |confing| confing.adapter = adapter }
.within(:artists).each { count3 += 1 }
end
x.report('composite') do
Saxerator.parser(@file) { |confing| confing.adapter = adapter }
.for_tag(:name)
.within(:artist).at_depth(3).each { count4 += 1 }
end
end
puts
puts '##########################################################'
puts
puts "for_tag: #{count} artist elements parsed"
puts "at_depth: #{count2} elements parsed"
puts "within: #{count3} artists children parsed"
puts "composite: #{count4} names within artist nested 3 tags deep parsed"
puts
end
end
saxerator_benchmark = SaxeratorBenchmark.new(file)
ADAPTERS.each { |adapter| saxerator_benchmark.with_adapter(adapter) }