public
Created

  • Download Gist
Gemfile
Ruby
1 2 3 4
source "https://rubygems.org"
 
gem "hamster", "0.4.3"
gem "nokogiri", "1.5.9"
details.rb
Ruby
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
# Scenario:
#
# Building an XML sitemap for a website, given an Array of URLs.
# In the actual example it's an Array of Strings, but to simplify
# things I chose to use an Array of Integers which I then interpolate.
# This is similar to the actual use-case as well.
#
# I know I could easily create the output using simple String
# interpolation, but I'm providing this Benchmark in the hopes that it
# can better the community and how jruby uses Nokogiri.
#
# System Information:
#
# Macbook Pro 13 inch.
# 2.9GHz Intel Core i7
# 8GB 1600MHz DDR3
#
# jruby was installed with rbenv 0.4.0
#
# $ jruby --version
# jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM)
# 64-Bit Server VM 1.6.0_43-b01-447-11M4203 [darwin-x86_64]
#
# Invoked using the following command on the terminal:
# $ jruby -J-Xms2048m -J-Xmx2048m -S nokogiri_benchmark.rb
#
# Output (jruby):
# user system total real
# 100 urls 2.490000 0.130000 2.620000 ( 1.038000)
# 5000 urls 36.050000 0.230000 36.280000 ( 32.682000)
# 10000 urls 142.100000 0.680000 142.780000 (138.808000)
# 30000 urls 1329.230000 5.760000 1334.990000 (1271.284000)
# 50000 urls 3857.420000 15.890000 3873.310000 (3570.221000)
#
# When run on 1.9.3-p392 the following output occurs when invoking the
# script with: ruby nokogiri_benchmark.rb
#
# Output (ruby):
# user system total real
# 100 urls 0.010000 0.000000 0.010000 ( 0.010593)
# 5000 urls 0.380000 0.000000 0.380000 ( 0.379011)
# 10000 urls 0.740000 0.010000 0.750000 ( 0.742528)
# 30000 urls 2.290000 0.010000 2.300000 ( 2.308314)
# 50000 urls 4.060000 0.030000 4.090000 ( 4.095105)
nokogiri_benchmark.rb
Ruby
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
require 'rubygems'
 
require 'benchmark'
require 'hamster/list'
require 'nokogiri'
 
def build_sitemap(urls)
base_url = "www.example.com"
 
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
xml.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9") do
xml.url {
xml.loc base_url + "/"
}
 
urls.each do |url|
full_url = base_url + "#{url}"
xml.url {
xml.loc full_url
}
end
end
end
builder.to_xml
end
 
def integers
Hamster.iterate(1, &:next)
end
 
Benchmark.bm(15) do |x|
[100, 5_000, 10_000, 30_000, 50_000].each do |amount|
x.report("#{amount} urls") { build_sitemap(integers.take(amount)) }
end
end

You should try with builder as well.

user system total real

#100 urls 0.113000 0.000000 0.113000 ( 0.113000)
#5000 urls 0.466000 0.000000 0.466000 ( 0.466000)
#10000 urls 0.702000 0.000000 0.702000 ( 0.702000)
#30000 urls 2.200000 0.000000 2.200000 ( 2.200000)
#50000 urls 3.629000 0.000000 3.629000 ( 3.629000)

require 'rubygems'

require 'benchmark'
require 'hamster/list'
require 'builder'

def build_sitemap(urls)
base_url = "www.example.com"

xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!

xml.urlset(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") do
xml.url {
xml.loc base_url + "/"
}

urls.each do |url|
  full_url = base_url + "#{url}"
  xml.url {
    xml.loc full_url
  }
end

end
xml.target!
end

def integers
Hamster.iterate(1, &:next)
end

Benchmark.bm(15) do |x|
[100, 5_000, 10_000, 30_000, 50_000].each do |amount|
x.report("#{amount} urls") { build_sitemap(integers.take(amount)) }
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.