Skip to content

Instantly share code, notes, and snippets.

@fgrehm
Forked from sam/gist:2002262
Created March 9, 2012 00:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgrehm/2004391 to your computer and use it in GitHub Desktop.
Save fgrehm/2004391 to your computer and use it in GitHub Desktop.
Router Benchmark
require "benchmark"
require 'set'
require_relative 'lib/harbor/router'
MAX_VALUE = 1_000
values = (1..MAX_VALUE).map do |i|
i = "%05d" % i
end
tree_router = nil
regexp_router = nil
Benchmark::bmbm do |x|
tree_router = Harbor::Router.new
balanced_router = Harbor::Router.new
regexp_router = Set.new
x.report("INSERTION STATIC : Tree") do
values.each do |i|
tree_router.register("GET", "/users/#{i}/edit", i)
end
# Considers build time as part of insertion time
tree_router.verbs['GET'].send(:build!)
end
x.report("INSERTION STATIC : Regexp") do
values.each do |i|
regexp_router << [ /\/users\/(#{i})\/edit/, i ]
end
end
x.report("MATCHING STATIC : Tree") do
(MAX_VALUE * 10).times do
tree_router.match "GET", "/users/#{"%05d" % rand(MAX_VALUE+1)}/edit"
end
end
x.report("MATCHING STATIC : Regexp") do
(MAX_VALUE * 10).times do
regexp_router.detect { |pair| "/users/#{"%05d" % rand(MAX_VALUE+1)}/edit" =~ pair[0] }
end
end
end
Benchmark::bmbm do |x|
tree_router = Harbor::Router.new
balanced_router = Harbor::Router.new
regexp_router = Set.new
x.report("INSERTION WILDCARD : Tree") do
values.each do |i|
tree_router.register("GET", "/users/#{i}/:id/edit", i)
end
# Considers build time as part of insertion time
tree_router.verbs['GET'].send(:build!)
end
x.report("INSERTION WILDCARD : Regexp") do
values.each do |i|
regexp_router << [ /\/users\/(#{i})\/(\d+)\/edit/, i ]
end
end
x.report("MATCHING WILDCARD : Tree") do
(MAX_VALUE*10).times do
tree_router.match "GET", "/users/#{"%05d" % rand(MAX_VALUE+1)}/#{rand(MAX_VALUE)}/edit"
end
end
x.report("MATCHING WILDCARD : Regexp") do
(MAX_VALUE*10).times do
regexp_router.detect { |pair| "/users/#{"%05d" % rand(MAX_VALUE+1)}/#{rand(MAX_VALUE)}/edit" =~ pair[0] }
end
end
end
Rehearsal -------------------------------------------------------------
INSERTION STATIC : Tree 0.334000 0.000000 0.334000 ( 0.334000)
INSERTION STATIC : Regexp 0.244000 0.000000 0.244000 ( 0.244000)
MATCHING STATIC : Tree 1.297000 0.000000 1.297000 ( 1.297000)
MATCHING STATIC : Regexp 13.382000 0.000000 13.382000 ( 13.382000)
--------------------------------------------------- total: 15.257000sec
user system total real
INSERTION STATIC : Tree 0.045000 0.000000 0.045000 ( 0.045000)
INSERTION STATIC : Regexp 0.106000 0.000000 0.106000 ( 0.106000)
MATCHING STATIC : Tree 0.287000 0.000000 0.287000 ( 0.287000)
MATCHING STATIC : Regexp 12.619000 0.000000 12.619000 ( 12.619000)
Rehearsal ---------------------------------------------------------------
INSERTION WILDCARD : Tree 0.464000 0.000000 0.464000 ( 0.464000)
INSERTION WILDCARD : Regexp 0.139000 0.000000 0.139000 ( 0.140000)
MATCHING WILDCARD : Tree 0.748000 0.000000 0.748000 ( 0.748000)
MATCHING WILDCARD : Regexp 17.349000 0.000000 17.349000 ( 17.349000)
----------------------------------------------------- total: 18.700000sec
user system total real
INSERTION WILDCARD : Tree 0.130000 0.000000 0.130000 ( 0.130000)
INSERTION WILDCARD : Regexp 0.047000 0.000000 0.047000 ( 0.047000)
MATCHING WILDCARD : Tree 0.188000 0.000000 0.188000 ( 0.188000)
MATCHING WILDCARD : Regexp 16.892000 0.000000 16.892000 ( 16.892000)
Rehearsal -------------------------------------------------------------
INSERTION STATIC : Tree 0.010000 0.000000 0.010000 ( 0.013484)
INSERTION STATIC : Regexp 0.010000 0.000000 0.010000 ( 0.011768)
MATCHING STATIC : Tree 0.130000 0.010000 0.140000 ( 0.141849)
MATCHING STATIC : Regexp 31.460000 0.140000 31.600000 ( 31.667999)
--------------------------------------------------- total: 31.760000sec
user system total real
INSERTION STATIC : Tree 0.010000 0.000000 0.010000 ( 0.011345)
INSERTION STATIC : Regexp 0.000000 0.010000 0.010000 ( 0.011591)
MATCHING STATIC : Tree 0.140000 0.000000 0.140000 ( 0.143509)
MATCHING STATIC : Regexp 31.250000 0.090000 31.340000 ( 31.423139)
Rehearsal ---------------------------------------------------------------
INSERTION WILDCARD : Tree 0.050000 0.000000 0.050000 ( 0.051654)
INSERTION WILDCARD : Regexp 0.020000 0.000000 0.020000 ( 0.016229)
MATCHING WILDCARD : Tree 0.270000 0.000000 0.270000 ( 0.274400)
MATCHING WILDCARD : Regexp 37.990000 0.160000 38.150000 ( 38.323789)
----------------------------------------------------- total: 38.490000sec
user system total real
INSERTION WILDCARD : Tree 0.050000 0.000000 0.050000 ( 0.050455)
INSERTION WILDCARD : Regexp 0.020000 0.000000 0.020000 ( 0.016344)
MATCHING WILDCARD : Tree 0.280000 0.000000 0.280000 ( 0.276594)
MATCHING WILDCARD : Regexp 38.630000 0.190000 38.820000 ( 38.877414)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment