Browse Source

[Ruby] Align to clarified benchmark requirements (#2582)

[ci fw-only Ruby/rack-sequel Ruby/roda-sequel Ruby/sinatra-sequel Ruby/sinatra]
Mike Pastore 8 years ago
parent
commit
189b34fc15

+ 6 - 18
frameworks/Ruby/rack-sequel/hello_world.rb

@@ -20,19 +20,12 @@ class HelloWorld
     Random.rand(MAX_PK).succ
     Random.rand(MAX_PK).succ
   end
   end
 
 
-  # Return an array of `n' unique random numbers between 1 and MAX_PK
-  def randn(n)
-    (1..MAX_PK).to_a.shuffle!.take(n)
-  end
-
   def db
   def db
     World.with_pk(rand1).values
     World.with_pk(rand1).values
   end
   end
 
 
   def queries(env)
   def queries(env)
-    # Benchmark requirements explicitly forbid a WHERE..IN here, so be good
-    randn(bounded_queries(env))
-      .map! { |id| World.with_pk(id).values }
+    Array.new(bounded_queries(env)) { World.with_pk(rand1).values }
   end
   end
 
 
   def fortunes
   def fortunes
@@ -76,19 +69,14 @@ class HelloWorld
     HTML
     HTML
   end
   end
 
 
-  WORLD_BY_ID_FOR_UPDATE = World.naked.for_update.where(:id=>:$id).prepare(:first, :world_by_id_for_update)
+  WORLD_BY_ID = World.naked.where(:id=>:$id).prepare(:first, :world_by_id)
   WORLD_UPDATE = World.where(:id=>:$id).prepare(:update, :world_update, :randomnumber=>:$randomnumber)
   WORLD_UPDATE = World.where(:id=>:$id).prepare(:update, :world_update, :randomnumber=>:$randomnumber)
 
 
   def updates(env)
   def updates(env)
-    # Benchmark requirements explicitly forbid a WHERE..IN here, transactions
-    # are optional, batch updates are allowed (but each transaction can only
-    # read and write a single record?), so... be good
-    randn(bounded_queries(env)).map! do |id|
-      DB.transaction do
-        world = WORLD_BY_ID_FOR_UPDATE.(:id=>id)
-        WORLD_UPDATE.(:id=>id, :randomnumber=>(world[:randomnumber] = rand1))
-        world
-      end
+    Array.new(bounded_queries(env)) do
+      world = WORLD_BY_ID.(:id=>rand1)
+      WORLD_UPDATE.(:id=>world[:id], :randomnumber=>(world[:randomnumber] = rand1))
+      world
     end
     end
   end
   end
 
 

+ 2 - 1
frameworks/Ruby/roda-sequel/Gemfile

@@ -5,7 +5,8 @@ gem 'json', '~> 2.0'
 gem 'passenger', '~> 5.1', :platforms=>[:ruby, :mswin], :require=>false
 gem 'passenger', '~> 5.1', :platforms=>[:ruby, :mswin], :require=>false
 gem 'puma', '~> 3.6', :require=>false
 gem 'puma', '~> 3.6', :require=>false
 gem 'sequel', '~> 4.43'
 gem 'sequel', '~> 4.43'
-gem 'roda', '~> 2.23'
+gem 'roda', '~> 2.23',
+  :git=>'https://github.com/jeremyevans/roda.git', :branch=>'master'
 gem 'sysrandom', '~> 1.0', :require=>'sysrandom/securerandom'
 gem 'sysrandom', '~> 1.0', :require=>'sysrandom/securerandom'
 gem 'tilt', '~> 2.0', :require=>'tilt/erb'
 gem 'tilt', '~> 2.0', :require=>'tilt/erb'
 gem 'torquebox-web', '>= 4.0.0.beta3', '< 5', :platforms=>:jruby, :require=>false
 gem 'torquebox-web', '>= 4.0.0.beta3', '< 5', :platforms=>:jruby, :require=>false

+ 7 - 30
frameworks/Ruby/roda-sequel/hello_world.rb

@@ -4,6 +4,7 @@
 class HelloWorld < Roda
 class HelloWorld < Roda
   plugin :default_headers, 'Content-Type'=>'text/html; charset=utf-8'
   plugin :default_headers, 'Content-Type'=>'text/html; charset=utf-8'
   plugin :default_headers, 'Server'=>SERVER_STRING if SERVER_STRING
   plugin :default_headers, 'Server'=>SERVER_STRING if SERVER_STRING
+  plugin :hooks
   plugin :json
   plugin :json
   plugin :render, :escape=>:erubi, :layout_opts=>{ :cache_key=>'default_layout' }
   plugin :render, :escape=>:erubi, :layout_opts=>{ :cache_key=>'default_layout' }
   plugin :static_routing
   plugin :static_routing
@@ -20,42 +21,27 @@ class HelloWorld < Roda
     Random.rand(MAX_PK).succ
     Random.rand(MAX_PK).succ
   end
   end
 
 
-  # Return an array of `n' unique random numbers between 1 and MAX_PK
-  def randn(n)
-    (1..MAX_PK).to_a.shuffle!.take(n)
-  end
-
-  def set_date
+  after do
     response['Date'] = Time.now.httpdate
     response['Date'] = Time.now.httpdate
   end
   end
 
 
   # Test type 1: JSON serialization
   # Test type 1: JSON serialization
   static_get '/json' do
   static_get '/json' do
-    set_date
-
     { :message=>'Hello, World!' }
     { :message=>'Hello, World!' }
   end
   end
 
 
   # Test type 2: Single database query
   # Test type 2: Single database query
   static_get '/db' do
   static_get '/db' do
-    set_date
-
     World.with_pk(rand1).values
     World.with_pk(rand1).values
   end
   end
 
 
   # Test type 3: Multiple database queries
   # Test type 3: Multiple database queries
   static_get '/queries' do
   static_get '/queries' do
-    set_date
-
-    # Benchmark requirements explicitly forbid a WHERE..IN here, so be good
-    randn(bounded_queries)
-      .map! { |id| World.with_pk(id).values }
+    Array.new(bounded_queries) { World.with_pk(rand1).values }
   end
   end
 
 
   # Test type 4: Fortunes
   # Test type 4: Fortunes
   static_get '/fortunes' do
   static_get '/fortunes' do
-    set_date
-
     @fortunes = Fortune.all
     @fortunes = Fortune.all
     @fortunes << Fortune.new(
     @fortunes << Fortune.new(
       :id=>0,
       :id=>0,
@@ -68,24 +54,15 @@ class HelloWorld < Roda
 
 
   # Test type 5: Database updates
   # Test type 5: Database updates
   static_get '/updates' do
   static_get '/updates' do
-    set_date
-
-    # Benchmark requirements explicitly forbid a WHERE..IN here, transactions
-    # are optional, batch updates are allowed (but each transaction can only
-    # read and write a single record?), so... be good
-    randn(bounded_queries).map! do |id|
-      DB.transaction do
-        world = World.for_update.with_pk(id)
-        world.update(:randomnumber=>rand1)
-        world.values
-      end
+    Array.new(bounded_queries) do
+      world = World.with_pk(rand1)
+      world.update(:randomnumber=>rand1)
+      world.values
     end
     end
   end
   end
 
 
   # Test type 6: Plaintext
   # Test type 6: Plaintext
   static_get '/plaintext' do
   static_get '/plaintext' do
-    set_date
-
     response['Content-Type'] = 'text/plain'
     response['Content-Type'] = 'text/plain'
 
 
     'Hello, World!'
     'Hello, World!'

+ 8 - 18
frameworks/Ruby/sinatra-sequel/hello_world.rb

@@ -36,11 +36,6 @@ class HelloWorld < Sinatra::Base
     def rand1
     def rand1
       Random.rand(MAX_PK).succ
       Random.rand(MAX_PK).succ
     end
     end
-
-    # Return an array of `n' unique random numbers between 1 and MAX_PK
-    def randn(n)
-      (1..MAX_PK).to_a.shuffle!.take(n)
-    end
   end
   end
 
 
   after do
   after do
@@ -63,9 +58,7 @@ class HelloWorld < Sinatra::Base
 
 
   # Test type 3: Multiple database queries
   # Test type 3: Multiple database queries
   get '/queries' do
   get '/queries' do
-    # Benchmark requirements explicitly forbid a WHERE..IN here, so be good
-    json randn(bounded_queries)
-      .map! { |id| World.with_pk(id).values }
+    json Array.new(bounded_queries) { World.with_pk(rand1).values }
   end
   end
 
 
   # Test type 4: Fortunes
   # Test type 4: Fortunes
@@ -82,16 +75,13 @@ class HelloWorld < Sinatra::Base
 
 
   # Test type 5: Database updates
   # Test type 5: Database updates
   get '/updates' do
   get '/updates' do
-    # Benchmark requirements explicitly forbid a WHERE..IN here, transactions
-    # are optional, batch updates are allowed (but each transaction can only
-    # read and write a single record?), so... be good
-    json(randn(bounded_queries).map! do |id|
-      DB.transaction do
-        world = World.for_update.with_pk(id)
-        world.update(:randomnumber=>rand1)
-        world.values
-      end
-    end)
+    worlds = Array.new(bounded_queries) do
+      world = World.with_pk(rand1)
+      world.update(:randomnumber=>rand1)
+      world.values
+    end
+
+    json worlds
   end
   end
 
 
   # Test type 6: Plaintext
   # Test type 6: Plaintext

+ 9 - 20
frameworks/Ruby/sinatra/hello_world.rb

@@ -31,11 +31,6 @@ class HelloWorld < Sinatra::Base
     def rand1
     def rand1
       Random.rand(MAX_PK).succ
       Random.rand(MAX_PK).succ
     end
     end
-
-    # Return an array of `n' unique random numbers between 1 and MAX_PK
-    def randn(n)
-      (1..MAX_PK).to_a.shuffle!.take(n)
-    end
   end
   end
 
 
   after do
   after do
@@ -54,20 +49,19 @@ class HelloWorld < Sinatra::Base
   # Test type 2: Single database query
   # Test type 2: Single database query
   get '/db' do
   get '/db' do
     world = ActiveRecord::Base.connection_pool.with_connection do
     world = ActiveRecord::Base.connection_pool.with_connection do
-      World.find(rand1)
+      World.find(rand1).attributes
     end
     end
 
 
-    json world.attributes
+    json world
   end
   end
 
 
   # Test type 3: Multiple database queries
   # Test type 3: Multiple database queries
   get '/queries' do
   get '/queries' do
     worlds = ActiveRecord::Base.connection_pool.with_connection do
     worlds = ActiveRecord::Base.connection_pool.with_connection do
-      # Benchmark requirements explicitly forbid a WHERE..IN here, so be good
-      randn(bounded_queries).map! { |id| World.find(id) }
+      Array.new(bounded_queries) { World.find(rand1).attributes }
     end
     end
 
 
-    json worlds.map!(&:attributes)
+    json worlds
   end
   end
 
 
   # Test type 4: Fortunes
   # Test type 4: Fortunes
@@ -87,19 +81,14 @@ class HelloWorld < Sinatra::Base
   # Test type 5: Database updates
   # Test type 5: Database updates
   get '/updates' do
   get '/updates' do
     worlds = ActiveRecord::Base.connection_pool.with_connection do |conn|
     worlds = ActiveRecord::Base.connection_pool.with_connection do |conn|
-      # Benchmark requirements explicitly forbid a WHERE..IN here, transactions
-      # are optional, batch updates are allowed (but each transaction can only
-      # read and write a single record?), so... be good
-      randn(bounded_queries).map! do |id|
-        conn.transaction do
-          world = World.lock.find(id)
-          world.update(:randomnumber=>rand1)
-          world
-        end
+      Array.new(bounded_queries) do
+        world = World.find(rand1)
+        world.update(:randomnumber=>rand1)
+        world.attributes
       end
       end
     end
     end
 
 
-    json worlds.map!(&:attributes)
+    json worlds
   end
   end
 
 
   # Test type 6: Plaintext
   # Test type 6: Plaintext