Browse Source

Merge pull request #746 from bbrowning/sinatra-validation

Fix Sinatra validation errors
Mike Smith 11 years ago
parent
commit
d45ae4de6e
2 changed files with 13 additions and 3 deletions
  1. 2 2
      sinatra/benchmark_config
  2. 11 1
      sinatra/hello_world.rb

+ 2 - 2
sinatra/benchmark_config

@@ -5,7 +5,7 @@
       "setup_file": "setup_ruby",
       "setup_file": "setup_ruby",
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/queries?queries=",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
@@ -26,7 +26,7 @@
       "setup_file": "setup_jruby",
       "setup_file": "setup_jruby",
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/queries?queries=",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",

+ 11 - 1
sinatra/hello_world.rb

@@ -9,6 +9,8 @@ set :static, false
 # Specify the encoder - otherwise, sinatra/json inefficiently
 # Specify the encoder - otherwise, sinatra/json inefficiently
 # attempts to load one of several on each request
 # attempts to load one of several on each request
 set :json_encoder => :to_json
 set :json_encoder => :to_json
+# Don't prefix JSON results with { "world": {...} }
+ActiveRecord::Base.include_root_in_json = false
 
 
 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 }
@@ -40,13 +42,21 @@ get '/plaintext' do
 end
 end
 
 
 get '/db' do
 get '/db' do
+  ActiveRecord::Base.connection_pool.with_connection do
+    json(World.find(Random.rand(10000) + 1))
+  end
+end
+
+get '/queries' do
   queries = (params[:queries] || 1).to_i
   queries = (params[:queries] || 1).to_i
+  queries = 1 if queries < 1
+  queries = 500 if queries > 500
 
 
   ActiveRecord::Base.connection_pool.with_connection do
   ActiveRecord::Base.connection_pool.with_connection do
     results = (1..queries).map do
     results = (1..queries).map do
       World.find(Random.rand(10000) + 1)
       World.find(Random.rand(10000) + 1)
     end
     end
 
 
-    json results
+    json(results)
   end
   end
 end
 end