File: comparison.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 (536 lines) | stat: -rw-r--r-- 15,207 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/ruby -I.

require "rexml/document"
require "rexml/streamlistener"
require "cps"

###########################################################################
# Check the system to determine which software is installed               #
###########################################################################
puts "Checking system..."
begin
	print "Have nqxml? " ; $stdout.flush
	require "nqxml/treeparser"
	require "nqxml/writer"
	require 'nqxml/streamingparser'
	require 'nqxml/info'
	have_nqxml = true
rescue Exception
	have_nqxml = false
end
puts have_nqxml
begin
	print "Have java? " ; $stdout.flush
	`java -version 2>&1`
	raise "no java" unless $?==0
	have_java = true
rescue Exception
	have_java = false
end
puts have_java
have_exml = have_java
if have_java
	begin
		print "Have EXML and flatbench test? " ; $stdout.flush
		`java -mx128m -cp EXML.jar:. flatbench verify`
		raise "no flatbench or EXML.jar" unless $? == 0
		have_exml = true
	rescue Exception
		have_exml = false
	end
  puts have_exml
end
begin
	print "Have XMLParser? " ; $stdout.flush
	require "xmlparser"
	require "xmltree"
	require "xmltreebuilder"
	have_xmlparser = true
rescue Exception
	have_xmlparser = false
end
puts have_xmlparser

###########################################################################
# Define some helper methods                                              #
###########################################################################
def ms a, b
	mod = "%0.2f"%(b/a)
	"#{b} (#{mod})"
end

def best element
	items = {}
	color_index = 0
	element.elements.each( "TD" ) do |elem|
		items[elem.text.split[0].to_f] = elem if elem.text =~ /^\d/
	end
	items.keys.sort.reverse.each do |key|
		items[key].attributes["BGCOLOR"] = "##{COLORS[color_index]}"
		color_index+=1
	end
end

DOCUMENTATION = "../documentation.xml"
TUTORIAL = "../docs/tutorial.xml"
COLORS = [ "99FF99", "BBFFBB", "FFFFFF"  ]
NACOLOR = "#FFAAAA"

###########################################################################
# Set up the HTML output document                                         #
###########################################################################
b = CPS.new 5
htmlout = REXML::Document.new
htmlout << REXML::Element.new("HTML")
body = htmlout.root.add_element "BODY"
body.add_element("H1").text = "REXML Comparisons"

body.add_element("H2").text = "Timing Comparison"
body.add_element("P").text = <<EOF
This page contains information comparing REXML to other XML parsing
technologies.  Included are timing comparisons.  Each benchmark was
run for 5 seconds, which probably isn't enough to flatten out startup
bumps and so on, but EXML in particular barfed at a 10 second benchmark
when it ran out of memory.
EOF
body.add_element("P").text = "The numbers under the column headers are the versions of the library being used.  The version under the EXML column is the version of Java that the test was run under; I'm not aware of a way to get the EXML library version.  The version of Ruby is #{`ruby -v`}."
body.add_element("P").text = "The test for hand-parsing and XPath are looking for the same elements in a large document (tutorial.xml).  Therefore, if the XPath value is half of the hand-parsing value, then on average, XPath is half as fast (but much easier!) as hand parsing."
body.add_element("P").text = "These values are in calls-per-second; higher is better. The numbers in parenthesis are multiples of the REXML score; more than one is faster than REXML, less than one is slower.   N/A means 'Not Applicable', meaning the parser doesn't support that function.  The scores are color coded; Java scores are not included in this, since it wouldn't be meaningful.  In these tests, Java is (nearly) always faster than Ruby.  The color coding is as follows:"
colors = body.add_element "TABLE", {"BORDER"=>"0"}
colors = colors.add_element "TR"
colors.add_element("TD", {"BGCOLOR"=>"##{COLORS[2]}"}).text = "slowest"
colors.add_element("TD", {"BGCOLOR"=>"##{COLORS[1]}"}).text = "middle"
colors.add_element("TD", {"BGCOLOR"=>"##{COLORS[0]}"}).text = "fastest"
colors.add_element("TD", {"BGCOLOR"=>NACOLOR}).text = "not available"

table = body.add_element "TABLE", {"BORDER"=>"1", "WIDTH"=>"100%"}
table_header = table.add_element "TR"
table_header.add_element("TH", {"HALIGN"=>"CENTER"}).text="function"
th = table_header.add_element("TH", {"HALIGN"=>"CENTER"})
th.add_text "REXML" ; th.add_element "BR" ; th.add_text REXML::Version
th = table_header.add_element("TH", {"HALIGN"=>"CENTER"})
nqxml_version = "N/A"
nqxml_version = NQXML::Version if have_nqxml
th.add_text "NQXML" ; th.add_element "BR" 
th.add_text nqxml_version
th = table_header.add_element("TH", {"HALIGN"=>"CENTER"})
th.add_text "XMLParser"
java_version = "N/A"
java_version = %x{java -version 2>&1}.split("\n")[0].split[2].tr('"', '') if have_java
th = table_header.add_element("TH", {"HALIGN"=>"CENTER"})
th.add_text "EXML (JIT)" ; th.add_element "BR" ; th.add_text java_version
th = table_header.add_element("TH", {"HALIGN"=>"CENTER"})
th.add_text "EXML (No JIT)" ; th.add_element "BR" ; th.add_text java_version

parsing = table.add_element "TR"
parsing.add_element("TD").text = "Parsing small document"
el_new = table.add_element "TR"
el_new.add_element("TD").text = "Creating and adding new Element"
tree = table.add_element "TR"
tree.add_element("TD").text = "Creating a new document tree"
writing = table.add_element "TR"
writing.add_element("TD").text = "Writing document out to a string"
detecting = table.add_element "TR"
detecting.add_element("TD").text = "Hand-parsing children"
xpath = table.add_element "TR"
xpath.add_element("TD").text = "XPath"
parsebig = table.add_element "TR"
parsebig.add_element("TD").text = "Parsing large document"
stream = table.add_element "TR"
stream.add_element("TD").text = "Stream parsing"

PARSING = "Parsing small document"
ADDING = "Adding new elemnt"
DOCUMENT = "Document creation"
WRITING = "Writing tree"
DETECT = "By-hand search"
XPATH = "XPath search"
BIG = "Parse large document"
STREAM = "Stream parsing"



begin
	######################################################################
	# Rexml benchmarks                                                   #
	######################################################################
	puts "GENERATING REXML"

	# PARSING
	source = File.new "project.xml"
	parsing_rex =  b.time(PARSING){
		REXML::Document.new source
		source.rewind
	}
	source.close
	parsing.add_element("TD").text = parsing_rex.to_s
	source = nil
	GC.start


	# ADDING
	document = REXML::Document.new
	element = document.add_element("x")
	el_new_rex=b.time(ADDING){element.add( REXML::Element.new("x") )}
	el_new.add_element("TD").text = el_new_rex.to_s
	document = element = nil
	GC.start

	
	# DOCUMENT
	tag2 = tag3 = nil
	tree_rex = b.time(DOCUMENT) {
		document = REXML::Document.new()
		document.add(REXML::Element.new("tag1"))
		document.root.attributes["blah"] = "four"

		tag2 = REXML::Element.new( "tag2" )
		tag2.attributes["some"] = "value"
		document.root.add(tag2)

		tag3 = REXML::Element.new( "tag3")
		tag2.add(tag3)
	}
	tree.add_element("TD").text = tree_rex.to_s
	tag2 = tag3 = nil
	GC.start


	# WRITING
	source = File.new TUTORIAL
	document = REXML::Document.new(source)
	source.close
	string = nil
	writing_rex = b.time(WRITING) {
		string = ""
		document.write string, -1
	}
	writing.add_element("TD").text = writing_rex.to_s
	source = string = nil
	GC.start


	# DETECT
	node = nil
	detecting_rex = b.time(DETECT) {
		element = document.root.detect{ |node|
			node.kind_of?(REXML::Element) and node.name == "overview"
		}
		element = element.detect{ |node|
			node.kind_of?(REXML::Element) and node.name == "general"
		}
		element = element.detect{ |node|
			node.kind_of?(REXML::Element) and
			node.name == "subsection" and
			node.attributes['title'] == 'Iterating'
		}
	}
	detecting.add_element("TD").text = detecting_rex.to_s
	node = element = nil
	GC.start


	# XPATH
	xpath_rex = b.time(XPATH) {
		document.root.elements["overview/general/subsection[@title='Iterating']"]
	}
	xpath.add_element("TD").text = xpath_rex.to_s
	GC.start


	# BIG
	source = File.new DOCUMENTATION
	parsebig_rex = b.time(BIG) {
		REXML::Document.new(source)
		source.rewind
	}
	source.close
	parsebig.add_element("TD").text = parsebig_rex.to_s
	document = source = element = nil
	GC.start 


	# STREAM
	class Listener
		include REXML::StreamListener
	end
	source_file = File.new(DOCUMENTATION)
	listener = Listener.new
	source = nil
	stream_rex = b.time(STREAM) {
		source = REXML::SourceFactory.create_from source_file
		REXML::Document.parse_stream source, listener
		source_file.rewind
	}
	stream.add_element("TD").text = stream_rex.to_s
	source_file.close
	source_file = source = nil
	GC.start



	######################################################################
	# NQXML                                                              #
	######################################################################
	if have_nqxml
		puts "\nGENERATING NQXML"

		# PARSING
		source = File.new "project.xml"
		val = b.time(PARSING){
			NQXML::TreeParser.new(source).document
			source.rewind
		}
		source.close
		parsing.add_element("TD").text = ms(parsing_rex, val)
		source = nil
		GC.start


		# ADDING
		document = NQXML::Document.new
		element = document.setRoot(NQXML::Tag.new("x", {}))
		val=b.time(ADDING){element.addChild(NQXML::Tag.new("x", {}))}
		el_new.add_element("TD").text = ms(el_new_rex, val)
		document = element = nil
		GC.start

		
		# DOCUMENT
		tag2 = tag3 = nil
		val = b.time(DOCUMENT) {
			document = NQXML::Document.new()
			document.setRoot(NQXML::Tag.new("tag1", {"blah"=>"four"}))

			tag2 = NQXML::Tag.new( "tag2", {"some"=>"value"})
			node = document.rootNode.addChild( tag2 )

			tag3 = NQXML::Tag.new( "tag3", {})
			node.addChild( tag3 )
		}
		tree.add_element("TD").text = ms(tree_rex, val)
		document = tag2 = tag3 = nil
		GC.start


		# WRITING
		source = File.new TUTORIAL
		document = NQXML::TreeParser.new(source).document
		source.close
		writer = string = nil
		val = b.time(WRITING) {
			string = ""
			writer = NQXML::Writer.new string
			writer.writeDocument( document )
		}
		writing.add_element("TD").text = ms(writing_rex, val)
		source = writer = string = nil
		GC.start


		# DETECT
		node = entity = nil
		val = b.time(DETECT) {
			element = document.rootNode.children.detect{ |node|
				entity = node.entity
				entity.kind_of?(NQXML::Tag) and entity.name == "overview"
			}
			element = element.children.detect{ |node|
				entity = node.entity
				entity.kind_of?(NQXML::Tag) and entity.name == "general"
			}
			element = element.children.detect{ |node|
				entity = node.entity
				entity.kind_of?(NQXML::Tag) and
				entity.name == "subsection" and
				entity.attrs['title'] == 'Iterating'
			}
		}
		detecting.add_element("TD").text = ms(detecting_rex, val)
		element = document = node = entity = nil
		GC.start


		# XPATH
		na = xpath.add_element("TD").text = "N/A"
		na.attributes["BGCOLOR"] = NACOLOR


		# BIG
		source = File.new DOCUMENTATION 
		val = b.time(BIG) {
			NQXML::TreeParser.new(source).document
			source.rewind
		}
		source.close
		parsebig.add_element("TD").text = ms(parsebig_rex, val)
		source = nil
		GC.start


		# STREAM
		source_file = File.new(DOCUMENTATION)
		listener = Listener.new
		parser = entity = nil
		val = b.time(STREAM) {
			parser = NQXML::StreamingParser.new(source_file)
			parser.each { | entity | }
			source_file.rewind
		}
		source_file.close
		source_file = nil
		stream.add_element("TD").text = ms(stream_rex,val)
		parser = entity = nil
		GC.start
	end # have_nqxml



	if have_xmlparser
		puts "\nGENERATING XMLParser"
		# TAKAHASHI Masayoshi <maki@inac.co.jp>

		# PARSING
		class SampleBuilder < XML::SimpleTreeBuilder; end
		source = File.new "project.xml"
		val = b.time(PARSING){
			SampleBuilder.new().parse(source)
			source.rewind
		}
		source.close
		parsing.add_element("TD").text = ms(parsing_rex, val)
		source = nil
		GC.start


		# ADDING
		document = XML::DOM::Document.new
		element = XML::DOM::Element.new("x", [])
		document.appendChild( element )
		val=b.time(ADDING){
			element.appendChild(XML::DOM::Element.new("x", []))
		}
		el_new.add_element("TD").text = ms(el_new_rex, val)
		document = element = nil
		GC.start


		# DOCUMENT
		tag2 = tag3 = node = nil
		val = b.time(DOCUMENT) {
			document = XML::DOM::Document.new()
			document.appendChild(XML::DOM::Element.new("tag1",
							 [Attr.new("blah",
									"four") ]))
			tag2 = XML::DOM::Element.new("tag2", [Attr.new("some",
								  "value") ])
			node = document.appendChild( tag2 )
			tag3 = XML::DOM::Element.new("tag3", [])
			node = document.appendChild( tag3 )
		}
		tree.add_element("TD").text = ms(tree_rex, val)
		document = tag2 = tag3 = nil
		GC.start

		# WRITING
		na = writing.add_element("TD").text = "N/A"
		na.attributes["BGCOLOR"] = NACOLOR


		# DETECT
		entity = document = nil
		File.open(TUTORIAL) do |source|
			document = SampleBuilder.new().parse(source)
		end
		val = b.time(DETECT) {
			element = document.getElementsByTagName("overview")[0]
			nodeList = element.getElementsByTagName("general")[0]
			nodeList = element.getElementsByTagName("subsiction")
			nodeList.each{ |node|
				if node.getAttribute('title') == 'Iterating'
					element = node
					break
				end
			}
		}
		detecting.add_element("TD").text = ms(detecting_rex, val)
		element = document = node = entity = nil
		GC.start


		# XPATH
		na = xpath.add_element("TD").text = "N/A"
		na.attributes["BGCOLOR"] = NACOLOR


		# BIG
		source = File.new DOCUMENTATION 
		val = b.time(BIG) {
			SampleBuilder.new().parse(source)
			source.rewind
		}
		source.close
		parsebig.add_element("TD").text = ms(parsebig_rex, val)
		source = nil
		GC.start


		# STREAM
		source_file = File.new(DOCUMENTATION)
		parser = entity = nil
		val = b.time(STREAM) {
			parser = XMLParser.new.parse(source_file)
			source_file.rewind
		}
		source_file.close
		source_file = nil
		stream.add_element("TD").text = ms(stream_rex,val)
		parser = entity = nil
		GC.start
	end

	for element in [parsing, el_new, tree, writing, detecting, xpath, parsebig, stream]
		best(element)
	end

	if have_exml
		tags = [parsing, el_new, tree, writing, detecting, xpath, parsebig, stream]
		rextags = [parsing_rex, el_new_rex, tree_rex, writing_rex, detecting_rex, xpath_rex, parsebig_rex, stream_rex]
		ind = 0
		block = proc do |line|
			puts line
			lines = line.scan(/\((.*?)\)/m)
			if lines[0].nil?
				na = tags[ind].add_element("TD").text = "N/A"
				na.attributes["BGCOLOR"] = NACOLOR
			else
				val = lines[0][0].chop.chop.to_f
				tags[ind].add_element("TD").text = ms(rextags[ind], val)
			end
			ind += 1
		end

# Electric NOJIT
		puts "\nGENERATING Electric, no JIT"
		process = IO.popen("java -mx64m -Djava.compiler=nojit -cp .:EXML.jar flatbench") 
		ind = 0
		process.each &block

# Electric JIT
		puts "\nGENERATING Electric, JIT"
		process = IO.popen("java -mx64m -cp .:EXML.jar flatbench") 
		ind = 0
		process.each &block
	end

rescue 
	puts "Error:"
	puts $!
	puts "Writing index anyway."
end

body.add_element "P"

fout = File.new("index.html", "w+")
htmlout.write fout
fout.close