Skip to content

Instantly share code, notes, and snippets.

@soveran
Created April 23, 2015 14:39
Show Gist options
  • Save soveran/a331acc8726dcba4ad43 to your computer and use it in GitHub Desktop.
Save soveran/a331acc8726dcba4ad43 to your computer and use it in GitHub Desktop.
Initial benchmarks of Rack, Syro and Cuba.
require "benchmark"
require "cuba"
require "syro"
class HelloWorld
def call(env)
if env['REQUEST_METHOD'] == 'GET' && env['PATH_INFO'] == '/foo/bar'
[200, {"Content-Type" => "text/html"}, ["hello world"]]
elsif env['REQUEST_METHOD'] == 'GET' && env['PATH_INFO'] == '/admin'
[200, {"Content-Type" => "text/html"}, ["for admins"]]
else
[404, {"Content-Type" => "text/html"}, [""]]
end
end
end
RackApp = HelloWorld.new
class Admin < Cuba
define do
on root, get do
res.write "for admins"
end
end
end
Cuba.define do
on "foo/bar" do
on root, get do
res.write "hello world"
end
end
on "admin" do
run Admin
end
end
admin = Syro.new {
get {
res.write("for admins")
}
}
app = Syro.new {
on("foo/bar") {
get {
res.write("hello world")
}
}
on("admin") {
run(admin)
}
}
env1 = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/foo/bar",
"SCRIPT_NAME" => "/" }
env2 = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/admin",
"SCRIPT_NAME" => "/" }
N = 10000
Benchmark.bmbm { |x|
x.report("Rack") {
N.times {
RackApp.call(env1)
}
}
x.report("Cuba") {
N.times {
Cuba.call(env1)
}
}
x.report("Syro") {
N.times {
app.call(env1)
}
}
x.report("Rack/Admin") {
N.times {
RackApp.call(env2)
}
}
x.report("Cuba/Admin") {
N.times {
Cuba.call(env2)
}
}
x.report("Syro/Admin") {
N.times {
app.call(env2)
}
}
}
:!ruby benchmarks.rb
Rehearsal ----------------------------------------------
Rack 0.020000 0.000000 0.020000 ( 0.020291)
Cuba 0.380000 0.010000 0.390000 ( 0.376348)
Syro 0.070000 0.000000 0.070000 ( 0.068477)
Rack/Admin 0.020000 0.000000 0.020000 ( 0.024030)
Cuba/Admin 0.620000 0.000000 0.620000 ( 0.622762)
Syro/Admin 0.050000 0.000000 0.050000 ( 0.050333)
------------------------------------- total: 1.170000sec
user system total real
Rack 0.010000 0.000000 0.010000 ( 0.017027)
Cuba 0.390000 0.000000 0.390000 ( 0.391857)
Syro 0.060000 0.000000 0.060000 ( 0.065231)
Rack/Admin 0.020000 0.000000 0.020000 ( 0.022116)
Cuba/Admin 0.380000 0.000000 0.380000 ( 0.383459)
Syro/Admin 0.040000 0.000000 0.040000 ( 0.036940)
@luislavena
Copy link

Calculating -------------------------------------
                Rack    33.295k i/100ms
                Cuba     2.067k i/100ms
                Syro    10.046k i/100ms
          Rack/Admin    28.234k i/100ms
          Cuba/Admin     1.326k i/100ms
          Syro/Admin    17.260k i/100ms
-------------------------------------------------
                Rack    558.293k (± 5.7%) i/s -      2.797M
                Cuba     22.669k (± 4.1%) i/s -    113.685k
                Syro    117.537k (± 4.9%) i/s -    592.714k
          Rack/Admin    422.478k (± 5.0%) i/s -      2.118M
          Cuba/Admin     20.848k (± 5.3%) i/s -    104.754k
          Syro/Admin    229.125k (± 4.7%) i/s -      1.156M

Comparison:
                Rack:   558293.5 i/s
          Rack/Admin:   422478.2 i/s - 1.32x slower
          Syro/Admin:   229124.8 i/s - 2.44x slower
                Syro:   117537.4 i/s - 4.75x slower
                Cuba:    22668.9 i/s - 24.63x slower
          Cuba/Admin:    20848.1 i/s - 26.78x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment