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
|
#!/usr/bin/env ruby
$: << '.'
$: << '..'
$: << '../lib'
$: << '../ext'
if __FILE__ == $0
while (i = ARGV.index('-I'))
x = ARGV.slice!(i, 2)
$: << x[1]
end
end
require 'optparse'
require 'ox'
require 'perf'
require 'files'
begin
require 'nokogiri'
rescue Exception => e
end
begin
require 'libxml'
rescue Exception => e
end
$verbose = 0
$ox_only = false
$all_cbs = false
$filename = nil # nil indicates new file names perf.xml will be created and used
$filesize = 1000 # KBytes
$iter = 1000
$strio = false
$pos = false
$smart = false
opts = OptionParser.new
opts.on('-v', 'increase verbosity') { $verbose += 1 }
opts.on('-x', 'ox only') { $ox_only = true }
opts.on('-a', 'all callbacks') { $all_cbs = true }
opts.on('-b', 'html smart') { $smart = true }
opts.on('-p', 'update position') { $pos = true; $all_cbs = true }
opts.on('-z', 'use StringIO instead of file') { $strio = true }
opts.on('-f', '--file [String]', String, 'filename') { |f| $filename = f }
opts.on('-i', '--iterations [Int]', Integer, 'iterations') { |it| $iter = it }
opts.on('-s', '--size [Int]', Integer, 'file size in KBytes') { |s| $filesize = s }
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
opts.parse(ARGV)
$xml_str = nil
# size is in Kbytes
def create_file(filename, size)
head = %{<?xml version="1.0"?>
<?ox version="1.0" mode="object" circular="no" xsd_date="no"?>
<!DOCTYPE table PUBLIC "-//ox//DTD TABLE 1.0//EN" "http://www.ohler.com/DTDs/TestTable-1.0.dtd">
<table>
}
tail = %{</table>
}
row = %{ <!-- row %08d element -->
<row id="%08d">
<cell id="A" type="Fixnum">1234</cell>
<cell id="B" type="String">A string.</cell>
<cell id="C" type="String">This is a longer string that stretches over a larger number of characters.</cell>
<cell id="D" type="Float">-12.345</cell>
<cell id="E" type="Date">2011-09-18 23:07:26 +0900</cell>
<cell id="F" type="Image"><![CDATA[xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00xx00]]></cell>
</row>
}
cnt = ((size * 1000) - head.size - tail.size) / row.size
File.open(filename, 'w') do |f|
f.write(head)
cnt.times do |i|
f.write(format(row, i, i))
end
f.write(tail)
end
end
class OxSax < Ox::Sax
def error(message, line, column);
puts message;
end
end
class OxAllSax < OxSax
def start_element(name); end
def attr(name, str); end
def attr_value(name, value); end
def end_element(name); end
def text(str); end
def value(value); end
def instruct(target); end
def doctype(value); end
def comment(value); end
def cdata(value); end
end
class OxPosAllSax < OxAllSax
def initialize
@line = nil
@column = nil
end
end
unless defined?(Nokogiri).nil?
class NoSax < Nokogiri::XML::SAX::Document
def error(message);
puts message;
end
def warning(message);
puts message;
end
end
class NoAllSax < NoSax
def start_element(name, attrs = []); end
def characters(text); end
def cdata_block(string); end
def comment(string); end
def end_document(); end
def end_element(name); end
def start_document(); end
def xmldecl(version, encoding, standalone); end
end
end
unless defined?(LibXML).nil?
class LxSax
include LibXML::XML::SaxParser::Callbacks
end
class LxAllSax < LxSax
def on_start_element(element, attributes); end
def on_cdata_block(cdata); end
def on_characters(chars); end
def on_comment(msg); end
def on_end_document(); end
def on_end_element(element); end
def on_end_element_ns(name, prefix, uri); end
def on_error(msg); end
def on_external_subset(name, external_id, system_id); end
def on_has_external_subset(); end
def on_has_internal_subset(); end
def on_internal_subset(name, external_id, system_id); end
def on_is_standalone(); end
def on_processing_instruction(target, data); end
def on_reference(name); end
def on_start_document(); end
def on_start_element_ns(name, attributes, prefix, uri, namespaces); end
end
end
if $filename.nil?
create_file('perf.xml', $filesize)
$filename = 'perf.xml'
end
$xml_str = File.read($filename)
puts "A #{$filesize} KByte XML file was parsed #{$iter} times for this test."
$handler = nil
perf = Perf.new
perf.add('Ox::Sax', 'sax_parse') do
input = $strio ? StringIO.new($xml_str) : IO.open(IO.sysopen($filename))
Ox.sax_parse($handler, input, smart: $smart)
input.close
end
perf.before('Ox::Sax') do
$handler = if $all_cbs
$pos ? OxPosAllSax.new : OxAllSax.new
else
OxSax.new
end
end
unless $ox_only
unless defined?(Nokogiri).nil?
perf.add('Nokogiri::XML::Sax', 'parse') do
input = $strio ? StringIO.new($xml_str) : IO.open(IO.sysopen($filename))
$handler.parse(input)
input.close
end
perf.before('Nokogiri::XML::Sax') { $handler = Nokogiri::XML::SAX::Parser.new($all_cbs ? NoAllSax.new : NoSax.new) }
end
unless defined?(LibXML).nil?
perf.add('LibXML::XML::Sax', 'parse') do
input = $strio ? StringIO.new($xml_str) : IO.open(IO.sysopen($filename))
parser = LibXML::XML::SaxParser.io(input)
parser.callbacks = $handler
parser.parse
input.close
end
perf.before('LibXML::XML::Sax') { $handler = $all_cbs ? LxAllSax.new : LxSax.new }
end
end
perf.run($iter)
|