Browse Source

optimize spider-gazelle benchmark (#4331)

* optimize spider-gazelle benchmark

* spider-gazelle fix maximum db id
Stephen von Takach 6 years ago
parent
commit
626cd33653
1 changed files with 12 additions and 11 deletions
  1. 12 11
      frameworks/Crystal/spider-gazelle/src/controllers/benchmark.cr

+ 12 - 11
frameworks/Crystal/spider-gazelle/src/controllers/benchmark.cr

@@ -2,14 +2,12 @@ class Benchmark < Application
   ID_MAXIMUM = 10_000
 
   base "/"
-  before_action :get_query_count, only: [:queries, :updates]
 
   def get_query_count
     queries = query_params["queries"]
     queries = queries.to_i? || 1
-    @queries = queries.clamp(1..500)
+    queries.clamp(1..500)
   end
-  @queries : Int32 = 0
 
   # Test 1: JSON Serialization
   get "/json", :json do
@@ -24,7 +22,7 @@ class Benchmark < Application
   # Postgres Test 2: Single database query
   get "/db", :db do
     results = {} of Symbol => Int32
-    if world = World.find rand(1..ID_MAXIMUM)
+    if world = World.find(Random.rand(ID_MAXIMUM).succ)
       results = {id: world.id, randomNumber: world.randomnumber}
     end
 
@@ -33,8 +31,8 @@ class Benchmark < Application
 
   # Postgres Test 3: Multiple database query
   get "/queries", :queries do
-    results = (1..@queries).map do
-      if world = World.find rand(1..ID_MAXIMUM)
+    results = (1..get_query_count).map do
+      if world = World.find(Random.rand(ID_MAXIMUM).succ)
         {id: world.id, randomNumber: world.randomnumber}
       end
     end
@@ -44,9 +42,9 @@ class Benchmark < Application
 
   # Postgres Test 5: Database Updates
   get "/updates", :updates do
-    results = (1..@queries).map do
-      if world = World.find rand(1..ID_MAXIMUM)
-        world.randomnumber = rand(1..ID_MAXIMUM)
+    results = (1..get_query_count).map do
+      if world = World.find(Random.rand(ID_MAXIMUM).succ)
+        world.randomnumber = Random.rand(ID_MAXIMUM).succ
         world.save
         {id: world.id, randomNumber: world.randomnumber}
       end
@@ -56,17 +54,20 @@ class Benchmark < Application
   end
 
   # Postgres Test 4: Fortunes
+  FORTUNE_MESSAGE = "Additional fortune added at request time."
+  FORTUNE_CTYPE = "text/html; charset=UTF-8"
+
   get "/fortunes", :fortunes do
     fortune = Fortune.new
     fortune.id = 0
-    fortune.message = "Additional fortune added at request time."
+    fortune.message = FORTUNE_MESSAGE
 
     fortunes = Fortune.all
     fortunes << fortune
     fortunes.sort_by! { |fortune| fortune.message || "" }
 
     # by default this would have been returned as text/html
-    response.content_type = "text/html; charset=UTF-8"
+    response.content_type = FORTUNE_CTYPE
     render html: Kilt.render("src/views/fortunes.ecr")
   end
 end