# OLD:
# ser@petra rexml/devel% ruby -I. rexml_prof.rb 1
# [2656, 1268]
# REXML: 9.987124
# Totals: 60224, 60272
# Each: 602.24, 602.24

# for x in 1 2 3 4 5; do ; echo TEST $x
# ruby -I src rexml_prof.rb $x
# done
# TEST 1
# Time: 131%
# Mem: 94%
# [2648, 1264]
# REXML: 13.119141
# Totals: 56764, 56808
# Each: 567.64, 567.64
#
# TEST 2
# Time: 74%
# Mem: 34%
# [2648, 1264]
# Light: 7.467977
# Totals: 20492, 20512
# Each: 204.92, 204.92
#
# TEST 3
# [2648, 1264]
# 
# Light: 0.033621
# Totals: 644, 664
# Each: 6.44, 6.44
# -rw-rw-r--    1 ser      users       30478 Apr 19 21:37 docs/tutorial.xml
# TEST 4
# [2660, 1268]
# 
# Light: 0.001491
# Totals: 396, 412
# Each: 3.96, 3.96
# -rw-rw-r--    1 ser      users         299 Apr 19 21:37 test/lang.xml
# TEST 5
# [2660, 1268]
# Light: 0.009074
# Totals: 76, 120
# Each: 0.76, 0.76

def mem
        m = `ps awwux`.split("\n").grep(/\d ruby /)[0].split
        [m[4].to_i, m[5].to_i]
end
p mem
def memdiff
        a,b = mem
        rv = [a-$lasta, b-$lastb]
        $lasta = a
        $lastb = b
        rv
end
if ARGV[0] == '1'
	require 'rexml/document'
	$lasta,$lastb = mem
	f = File.new( "../docs/documentation.xml" )
	memdiff
	r = REXML::Document.new
	x = memdiff
	memdiff
	t=Time.now; b=[]; 80.times{
		b << REXML::Document.new( f )
		print "." ; $stdout.flush
		f.rewind
	} 
	puts 
	puts "REXML: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
elsif ARGV[0] == '1.5'
	$lasta,$lastb = mem
	memdiff
	x = memdiff
	memdiff
	c=[]
	t=Time.now; 100000.times{ 
		a = [1,2,3,4,5]
		b = []
		b << a
		c << b
	}
	puts 
	puts "Light: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
elsif ARGV[0] == '1.6'
	$lasta,$lastb = mem
	memdiff
	x = memdiff
	memdiff
	c=[]
	t=Time.now; 100000.times{ 
		a = [1,2,3,4,5]
		b = []
		b.replace( a )
		c << b
	}
	puts 
	puts "Light: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
elsif ARGV[0] == '2'
	require 'rexml/parsers/lightparser'
	$lasta,$lastb = mem
	f = File.new( "../docs/documentation.xml" )
	memdiff
	parser = REXML::Parsers::LightParser.new( f );
	x = memdiff
	memdiff
	t=Time.now; c=[]; 80.times{ 
		c << parser.parse
		print "." ; $stdout.flush
    parser.rewind
	}
	puts 
	puts "Light: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
elsif ARGV[0] == '3'
	require 'rexml/parsers/lightparser'
	$lasta,$lastb = mem
	f = File.new( "../docs/tutorial.xml" )
	memdiff
	parser = REXML::Parsers::LightParser.new( f );
	x = memdiff
	memdiff
  t = Time.now
  b = parser.parse
	puts 
	puts "Light: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
	puts `ls -l ../docs/tutorial.xml`
	puts b.inspect
elsif ARGV[0] == '4'
	require 'rexml/parsers/lightparser'
	$lasta,$lastb = mem
	f = File.new( "test/lang.xml" )
	memdiff
	parser = REXML::Parsers::LightParser.new( f );
	x = memdiff
	memdiff
  t = Time.now
  parser.parse
	puts 
	puts "Light: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
	puts `ls -l test/lang.xml`
elsif ARGV[0] == '5'
	$lasta,$lastb = mem
	f = File.new( "test/lang.xml" )
	memdiff
	x = memdiff
	t=Time.now; c=[]; 100.times{ 
		c << f.readlines
		print "." ; $stdout.flush
		f.rewind
	}
	puts 
	puts "Light: #{Time.now-t}"
	GC.start
	y = memdiff
	y[0] -= x[0]
	y[1] -= x[1]
	puts "Totals: #{y[0]}, #{y[1]}"
	puts "Each: #{y[0]/100.0}, #{y[0]/100.0}"
elsif ARGV[0] == '6'
	$lasta,$lastb = mem
	f = File.new( "../docs/documentation.xml" )
	t=Time.now
	memdiff
	lines = f.readlines
	GC.start
	p memdiff
	o = lines.collect { |l| { l => l.split(" ") } }
	puts Time.now-t
	GC.start
	p memdiff
elsif ARGV[0] == '7'
	$lasta,$lastb = mem
	memdiff
	t=Time.now
	a = []
	100000.times { |x| a << [] }
	p Time.now-t
	GC.start
	p memdiff
elsif ARGV[0] == '8'
	$lasta,$lastb = mem
	memdiff
	t=Time.now
	a = {}
	10000.times { |x| a[x] = {} }
	p Time.now-t
	GC.start
	p memdiff
end
