File: parse_bench.rb

package info (click to toggle)
librexml-ruby 1.2.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 792 kB
  • ctags: 655
  • sloc: ruby: 3,778; xml: 1,609; java: 109; makefile: 43
file content (147 lines) | stat: -rw-r--r-- 2,912 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/ruby -I. -w

require "xmlparser"
require "xmlscan/parser"
require "nqxml/streamingparser"
require "rexml/document"
require "rexml/streamlistener"


require "benchmark"
include Benchmark

## for xmlparser
class SampleXMLParser < XMLParser
  def startElement(*args)
    $count += 1
  end
end

## for nqxml

## for xmlscan
class LooseNSVisitor
  include XMLScan::XMLNamespaceVisitor
  def on_start_element_ns(*args)
    $count += 1
  end
  def on_start_element(*args)
    $count += 1
  end
  def parse_error(path, lineno, msg);end
  def wellformed_error(path, lineno, msg);end
  def ns_error(path, lineno, msg);end
end

## for rexml
class REXMLListener
  include REXML::StreamListener
  def tag_start(name, attrs)
    $count += 1
  end
end
rexmllistener = REXMLListener.new()

#n=400
n=10
bm(25) do |test|
  File.open("w3cindex.html") do |source|
#  File.open("project.xml") do |source|

    print "-"*70,"\n"
    print "Parse File object:\n"
    test.report("XMLParser"){
      n.times {
	$count = 0
	SampleXMLParser.new.parse(source)
	p $count if $DEBUG
	source.rewind
      }
    }
    test.report("XMLScan::XMLParser"){
      n.times {
	$count = 0
	XMLScan::XMLParser.new(LooseNSVisitor.new).parse(source)
	p $count if $DEBUG
	source.rewind
      }
    }
    test.report("XMLScan::XMLNSParser"){
      n.times {
	$count = 0
	XMLScan::XMLNamespaceParser.new(LooseNSVisitor.new).parse(source)
	p $count if $DEBUG
	source.rewind
      }
    }
    test.report("NQXML::StrmParser"){
      n.times {
	$count = 0
	p = NQXML::StreamingParser.new(source)
	p.each{|elem|
	  if elem.type == NQXML::Tag && !elem.isTagEnd
	    $count += 1
	  end
	}
	p $count if $DEBUG
	source.rewind
      }
    }
    test.report("REXML::StreamListener"){
      n.times {
	$count = 0
	s = REXML::SourceFactory.create_from(source)
	REXML::Document.parse_stream(s, rexmllistener)
	p $count if $DEBUG
	source.rewind
      }
    }

    print "-"*70,"\n"
    print "Parse String object:\n"
    source = source.read()

    test.report("XMLParser"){
      n.times{
	$count = 0
	SampleXMLParser.new.parse(source)
	p $count if $DEBUG
      }
    }
    test.report("XMLScan::XMLParser"){
      n.times {
	$count = 0
	XMLScan::XMLParser.new(LooseNSVisitor.new).parse(source)
	p $count  if $DEBUG
      }
    }
    test.report("XMLScan::XMLNSParser"){
      n.times {
	$count = 0
	XMLScan::XMLNamespaceParser.new(LooseNSVisitor.new).parse(source)
	p $count if $DEBUG
      }
    }
    test.report("NQXML::StrmParser"){
      n.times {
	$count = 0
	p = NQXML::StreamingParser.new(source)
	p.each{|elem|
	  if elem.type == NQXML::Tag && !elem.isTagEnd
	    $count += 1
	  end
	}
	p $count if $DEBUG
      }
    }
    test.report("REXML::StreamListener"){
      n.times {
	$count = 0
	s = REXML::SourceFactory.create_from(source)
	REXML::Document.parse_stream(s, rexmllistener)
	p $count if $DEBUG
      }
    }
    print "-"*70,"\n"
  end
end