Browse Source

Merge pull request #633 from bbrowning/sinatra_perf

Fix the Sinatra tests to more closely resemble production settings
Mike Smith 11 years ago
parent
commit
edf8bfd2a9
1 changed files with 20 additions and 4 deletions
  1. 20 4
      sinatra/hello_world.rb

+ 20 - 4
sinatra/hello_world.rb

@@ -4,6 +4,11 @@ require "sinatra/activerecord"
 
 
 set :logging, false
 set :logging, false
 set :activerecord_logger, nil
 set :activerecord_logger, nil
+set :static, false
+
+# Specify the encoder - otherwise, sinatra/json inefficiently
+# attempts to load one of several on each request
+set :json_encoder => :to_json
 
 
 if RUBY_PLATFORM == 'java'
 if RUBY_PLATFORM == 'java'
   set :database, { :adapter => 'jdbcmysql', :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :host => 'localhost', :pool => 256, :timeout => 5000 }
   set :database, { :adapter => 'jdbcmysql', :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :host => 'localhost', :pool => 256, :timeout => 5000 }
@@ -11,6 +16,15 @@ else
   set :database, { :adapter => 'mysql2', :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :host => 'localhost', :pool => 256, :timeout => 5000 }
   set :database, { :adapter => 'mysql2', :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :host => 'localhost', :pool => 256, :timeout => 5000 }
 end
 end
 
 
+# The sinatra-activerecord gem registers before and after filters that
+# call expensive synchronized ActiveRecord methods on every request to
+# verify every connection in the pool, even for routes that don't use
+# the database. Clear those filters and handle connection management
+# ourselves, which is what applications seeking high throughput with
+# ActiveRecord need to do anyway.
+settings.filters[:before].clear
+settings.filters[:after].clear
+
 class World < ActiveRecord::Base
 class World < ActiveRecord::Base
   self.table_name = "World"
   self.table_name = "World"
   attr_accessible :randomNumber
   attr_accessible :randomNumber
@@ -28,9 +42,11 @@ end
 get '/db' do
 get '/db' do
   queries = (params[:queries] || 1).to_i
   queries = (params[:queries] || 1).to_i
 
 
-  results = (1..queries).map do
-    World.find(Random.rand(10000) + 1)
+  ActiveRecord::Base.connection_pool.with_connection do
+    results = (1..queries).map do
+      World.find(Random.rand(10000) + 1)
+    end
+
+    json results
   end
   end
-  
-  results.to_json
 end
 end