| 1 | # encoding: utf-8 |
|---|
| 2 | [ './ext', '../ext', './lib', '../lib' ].each { |l| $: << l } |
|---|
| 3 | require 'rubygems' |
|---|
| 4 | require 'fastxml' |
|---|
| 5 | require 'hpricot' |
|---|
| 6 | require 'open-uri' |
|---|
| 7 | require 'benchmark' |
|---|
| 8 | require 'rexml/document' |
|---|
| 9 | require 'xml/libxml' |
|---|
| 10 | |
|---|
| 11 | test_path = './test_data/hasno_feed.xml' |
|---|
| 12 | #fraw = open('http://hasno.info/feed/atom.xml') { |f| f.readlines } |
|---|
| 13 | fraw = open( test_path ) { |f| f.readlines } |
|---|
| 14 | ds = fraw.join('') |
|---|
| 15 | xpath_xpr = "/feed/entry" |
|---|
| 16 | |
|---|
| 17 | doc = hpd = rxd = lxd = nil |
|---|
| 18 | fxn = hpn = rxn = lxn = nil |
|---|
| 19 | fxs = hps = rxs = lxs = nil |
|---|
| 20 | |
|---|
| 21 | Benchmark.bm(15) do |x| |
|---|
| 22 | x.report("fastxml.new") { doc = FastXml::Doc.new( ds ) } |
|---|
| 23 | x.report("fastxml.to_s") { fxs = doc.to_s } |
|---|
| 24 | x.report("fastxml.search") { fxn = doc.search( xpath_xpr ) } |
|---|
| 25 | puts "" |
|---|
| 26 | x.report("hpricot.new") { hpd = Hpricot( ds ) } |
|---|
| 27 | x.report("hpricot.to_s") { hps = hpd.to_s } |
|---|
| 28 | x.report("hpricot.search") { hpn = hpd.search( xpath_xpr ) } |
|---|
| 29 | puts "" |
|---|
| 30 | x.report("libxml.new") { lxd = XML::Document.file( test_path ) } |
|---|
| 31 | x.report("libxml.to_s") { lxs = lxd.to_s } |
|---|
| 32 | x.report("libxml.search") { lxn = lxd.find( xpath_xpr )} |
|---|
| 33 | puts "" |
|---|
| 34 | x.report("REXML.new") { rxd = REXML::Document.new( ds ) } |
|---|
| 35 | x.report("REXML.to_s") { rxs = rxd.to_s } |
|---|
| 36 | x.report("REXML.xpath") { rxn = REXML::XPath.match( rxd, xpath_xpr ) } |
|---|
| 37 | end |
|---|
| 38 | |
|---|
| 39 | puts "\noutput matching\n" |
|---|
| 40 | puts "fastxml vs orig: %s\n" % (fxs && ds && (fxs == ds)) |
|---|
| 41 | puts "hpricot vs orig: %s\n" % (hps && ds && (hps == ds)) |
|---|
| 42 | puts " REXML vs orig: %s\n" % (rxs && ds && (rxs == ds)) |
|---|
| 43 | puts " fastxml vs libxml: %s\n" % (fxs && lxs && (fxs == lxs)) |
|---|
| 44 | puts "fastxml vs hpricot: %s\n" % (fxs && hps && (fxs == hps)) |
|---|
| 45 | puts " fastxml vs REXML: %s\n" % (fxs && rxs && (fxs == rxs)) |
|---|
| 46 | |
|---|
| 47 | puts "\nxpath expression: #{xpath_xpr}\n" |
|---|
| 48 | puts "fastxml nodes: %d" % fxn.length |
|---|
| 49 | puts " libxml nodes: %d" % lxn.length |
|---|
| 50 | puts "hpricot nodes: %d" % hpn.length |
|---|
| 51 | puts " REXML nodes: %d" % rxn.length |
|---|