#!/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 (no JIT)" ; th.add_element "BR" ; th.add_text java_version
th = table_header.add_element("TH", {"HALIGN"=>"CENTER"})
th.add_text "EXML (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
