Skip to content

Instantly share code, notes, and snippets.

@flavorjones
Created June 7, 2016 15:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavorjones/9234697161b2cb0c99dcc1863d796fed to your computer and use it in GitHub Desktop.
Save flavorjones/9234697161b2cb0c99dcc1863d796fed to your computer and use it in GitHub Desktop.
benchmark nokogiri vs rexml
# parsing a document 6731 bytes long
Warming up --------------------------------------
nokogiri 488.000 i/100ms
rexml 21.000 i/100ms
Calculating -------------------------------------
nokogiri 6.150k (±23.4%) i/s - 28.792k in 5.067345s
rexml 208.195 (± 5.3%) i/s - 1.050k in 5.056578s
Comparison:
nokogiri: 6150.3 i/s
rexml: 208.2 i/s - 29.54x slower
# xpath search
Warming up --------------------------------------
nokogiri 2.388k i/100ms
rexml 42.000 i/100ms
Calculating -------------------------------------
nokogiri 26.081k (± 3.8%) i/s - 131.340k in 5.043496s
rexml 431.709 (± 5.8%) i/s - 2.184k in 5.078139s
Comparison:
nokogiri: 26081.5 i/s
rexml: 431.7 i/s - 60.41x slower
#! /usr/bin/env ruby
require 'nokogiri'
require 'rexml/document'
require 'benchmark/ips'
require 'test/unit'
include Test::Unit::Assertions
xml = File.read "vcenter.xml"
nokogiri_doc = Nokogiri::XML xml
rexml_doc = REXML::Document.new xml
puts "# parsing a document #{xml.length} bytes long\n"
Benchmark.ips do |b|
b.report "nokogiri" do
Nokogiri::XML xml
end
b.report "rexml" do
REXML::Document.new xml
end
b.compare!
end
puts "# xpath search\n"
Benchmark.ips do |b|
b.report "nokogiri" do
nodes = nokogiri_doc.xpath("//vmext:VmObjectRefsList//vmext:MoRef", vmext: "http://www.vmware.com/vcloud/extension/v1.5")
assert_equal ["vm-41", "vm-43", "vm-44"], nodes.map(&:inner_text)
end
b.report "rexml" do
nodes = REXML::XPath.match(rexml_doc, "//vmext:VmObjectRefsList//vmext:MoRef")
assert_equal ["vm-41", "vm-43", "vm-44"], nodes.map(&:children).map(&:first).map(&:to_s)
end
b.compare!
end
<?xml version="1.0" encoding="UTF-8"?>
<fakeroot>
<vmext:VmObjectRefsList
xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
page="1"
numberOfPages="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<vmext:VmObjectRef
name="RH5u3_32bit">
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc2-v41u1"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>vm-41</vmext:MoRef>
<vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>
</vmext:VmObjectRef>
<vmext:VmObjectRef
name="W2K3 64 R2">
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc2-v41u1"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>vm-43</vmext:MoRef>
<vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>
</vmext:VmObjectRef>
<vmext:VmObjectRef
name="Ubuntu91_32_vt4">
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc2-v41u1"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>vm-44</vmext:MoRef>
<vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>
</vmext:VmObjectRef>
</vmext:VmObjectRefsList>
<vmext:ResourcePoolList
xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
type="application/vnd.vmware.admin.resourcePoolList+xml">
<vcloud:Link
rel="up"
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:ResourcePool
name="cluster2">
<vmext:MoRef>resgroup-195</vmext:MoRef>
<vmext:VimObjectType>RESOURCE_POOL</vmext:VimObjectType>
<vmext:DataStoreRefs>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc9-ds1"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>datastore-172</vmext:MoRef>
<vmext:VimObjectType>DATASTORE</vmext:VimObjectType>
</vmext:VimObjectRef>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc9-ds2"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>datastore-173</vmext:MoRef>
<vmext:VimObjectType>DATASTORE</vmext:VimObjectType>
</vmext:VimObjectRef>
</vmext:DataStoreRefs>
</vmext:ResourcePool>
<vmext:ResourcePool
name="cluster3">
<vmext:MoRef>resgroup-230</vmext:MoRef>
<vmext:VimObjectType>RESOURCE_POOL</vmext:VimObjectType>
<vmext:DataStoreRefs>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc9-ds10"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>datastore-174</vmext:MoRef>
<vmext:VimObjectType>DATASTORE</vmext:VimObjectType>
</vmext:VimObjectRef>
</vmext:DataStoreRefs>
</vmext:ResourcePool>
</vmext:ResourcePoolList>
<vmext:VimObjectRefList
xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<vcloud:Link
rel="up"
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:VimObjectRefs>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name="vc9"
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>dvportgroup-32</vmext:MoRef>
<vmext:VimObjectType>DV_PORTGROUP</vmext:VimObjectType>
</vmext:VimObjectRef>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name=""
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>network-175</vmext:MoRef>
<vmext:VimObjectType>NETWORK</vmext:VimObjectType>
</vmext:VimObjectRef>
</vmext:VimObjectRefs>
</vmext:VimObjectRefList>
<vmext:VMWExternalNetworkReferences
xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5">
<vmext:ExternalNetworkReference
type="application/vnd.vmware.admin.extension.network+xml"
name="VC0"
href="https://vcloud.example.com/api/admin/extension/externalnet/85" />
<vmext:ExternalNetworkReference
type="application/vnd.vmware.admin.extension.network+xml"
name="VC1"
href="https://vcloud.example.com/api/admin/extension/externalnet/302" />
</vmext:VMWExternalNetworkReferences>
<vmext:VMWStorageProfiles
xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
>
<vmext:VMWStorageProfile
name="Gold">
<vmext:MoRef>storageProfile-CFAA6D92-36FC-4C16-9B30-FAC79B902371</vmext:MoRef>
<vmext:VimObjectType>STORAGE_PROFILE</vmext:VimObjectType>
<vmext:FreeStorageMb>169203.0</vmext:FreeStorageMb>
<vmext:TotalStorageMb>325120.0</vmext:TotalStorageMb>
<vmext:DataStoreRefs>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name=""
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>datastore-44</vmext:MoRef>
<vmext:VimObjectType>DATASTORE</vmext:VimObjectType>
</vmext:VimObjectRef>
<vmext:VimObjectRef>
<vmext:VimServerRef
type="application/vnd.vmware.admin.vmwvirtualcenter+xml"
name=""
href="https://vcloud.example.com/api/admin/extension/vimServer/9" />
<vmext:MoRef>datastore-45</vmext:MoRef>
<vmext:VimObjectType>DATASTORE</vmext:VimObjectType>
</vmext:VimObjectRef>
</vmext:DataStoreRefs>
</vmext:VMWStorageProfile>
<vmext:VMWStorageProfile
name="Silver">
</vmext:VMWStorageProfile>
<vmext:VMWStorageProfile
name="Bronze">
</vmext:VMWStorageProfile>
</vmext:VMWStorageProfiles>
</fakeroot>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment