Permalink
Browse files

Compare performance of ShamRack vs running a Rack app in a separate p…

…rocess.
  • Loading branch information...
1 parent ce21b3c commit fea4a51bb10cc23112f80e8c43792d90e4d63b05 @mdub committed Jun 27, 2009
Showing with 59 additions and 0 deletions.
  1. +49 −0 benchmark/benchmark.rb
  2. +10 −0 benchmark/hello_app.rb
View
@@ -0,0 +1,49 @@
+require "rubygems"
+
+require "pathname"
+dir = Pathname(__FILE__).parent
+$LOAD_PATH.unshift(dir.parent + "lib")
+
+require dir + "hello_app"
+require "sham_rack"
+require "restclient"
+
+# mount an instance of the app using ShamRack
+ShamRack.mount(HelloApp.new, "hello.sham")
+
+# run another instance in a separate process
+server_pid = fork do
+ puts "Starting HTTP server on port 3333"
+ $stdout = File.new('/dev/null', 'w')
+ HelloApp.run!(:port => 3333)
+end
+
+at_exit do
+ puts "Killing HTTP server"
+ Process.kill("TERM", server_pid)
+ Process.wait(server_pid)
+end
+
+puts "Waiting for server to come up"
+begin
+ puts RestClient.get("http://localhost:3333/hello/stranger")
+rescue SystemCallError => e
+ retry
+end
+
+iterations = (ARGV.shift || 1000).to_i
+
+%w(hello.sham localhost:3333).each do |site|
+
+ start = Time.now
+
+ iterations.times do
+ x = RestClient.get("http://#{site}/hello/stranger").to_s
+ end
+
+ elapsed_time = (Time.now - start)
+ requests_per_second = iterations / elapsed_time.to_f
+
+ printf "%-20s #{iterations} requests in %f; %f per second\n", site, elapsed_time, requests_per_second
+
+end
View
@@ -0,0 +1,10 @@
+require "rubygems"
+require "sinatra/base"
+
+class HelloApp < Sinatra::Base
+
+ get "/hello/:subject" do
+ "Hello, #{params[:subject]}"
+ end
+
+end

0 comments on commit fea4a51

Please sign in to comment.