Browse Source

Refactor redis tests to allow more dbs

Zane Kansil 10 years ago
parent
commit
c80a582ebc

+ 4 - 4
frameworks/Crystal/moonshine/benchmark_config.json

@@ -22,10 +22,10 @@
     },
     "redis": {
       "setup_file": "setup",
-      "db_url": "/db",
-      "query_url": "/queries?queries=",
-      "fortune_url": "/fortunes",
-      "update_url": "/updates?queries=",
+      "db_url": "/redis/db",
+      "query_url": "/redis/queries?queries=",
+      "fortune_url": "/redis/fortunes",
+      "update_url": "/redis/updates?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Fullstack",

+ 81 - 77
frameworks/Crystal/moonshine/server.cr

@@ -54,101 +54,105 @@ private def sanitizedQueryCount(request)
   queries
 end
 
-app.define do
+#
+# Basic Tests
+#
+
+# Test 1: JSON Serialization
+app.get "/json", do |request|
+  res = ok({ :message => "Hello, World!" }.to_json)
+  res.headers["Content-type"] = CONTENT::JSON
+  res
+end
 
-  # Test 1: JSON Serialization
-  get "/json", do |request|
-    res = ok({ :message => "Hello, World!" }.to_json)
-    res.headers["Content-type"] = CONTENT::JSON
-    res
-  end
+# Test 6: Plaintext
+app.get "/plaintext", do |request|
+  res = ok("Hello, World!")
+  res.headers["Content-type"] = CONTENT::PLAIN
+  res
+end
 
-  # Test 2: Single database query
-  route "/db", do |request|
-    res = ok(randomWorld.to_json)
-    res.headers["Content-type"] = CONTENT::JSON
-    res
-  end
+#
+# Redis DatabaseTests
+#
 
-  # Test 3: Multiple database query
-  route "/queries", do |request|
-    results = (1..sanitizedQueryCount(request)).map do
-      randomWorld
-    end
+# Redis Test 2: Single database query
+app.get "/redis/db", do |request|
+  res = ok(randomWorld.to_json)
+  res.headers["Content-type"] = CONTENT::JSON
+  res
+end
 
-    res = ok(results.to_json)
-    res.headers["Content-type"] = CONTENT::JSON
-    res
+# Redis Test 3: Multiple database query
+app.get "/redis/queries", do |request|
+  results = (1..sanitizedQueryCount(request)).map do
+    randomWorld
   end
 
-  # Test 4: Fortunes
-  route "/fortunes", do |request|
-    data = [] of  Hash(Symbol, (String | Int32))
-
-    REDIS.lrange("fortunes", 0, -1).each_with_index do |e, i|
-      data.push({ :id => i + 1, :message => e.to_s })
-    end
-    
-    additional_fortune = {
-      :id => 0
-      :message => "Additional fortune added at request time."
-    }
-    data.push(additional_fortune)
+  res = ok(results.to_json)
+  res.headers["Content-type"] = CONTENT::JSON
+  res
+end
 
-    data.sort! { |a, b|
-      a[:message].to_s <=> b[:message].to_s
-    }
+# Redis Test 4: Fortunes
+app.get "/redis/fortunes", do |request|
+  data = [] of  Hash(Symbol, (String | Int32))
 
-    # New builder for each request!
-    html = HTML::Builder.new.build do
-      html {
-        head {
-          title { text "Fortunes" }
-        }
-        body {
-          table {
+  REDIS.lrange("fortunes", 0, -1).each_with_index do |e, i|
+    data.push({ :id => i + 1, :message => e.to_s })
+  end
+  
+  additional_fortune = {
+    :id => 0
+    :message => "Additional fortune added at request time."
+  }
+  data.push(additional_fortune)
+
+  data.sort! { |a, b|
+    a[:message].to_s <=> b[:message].to_s
+  }
+
+  # New builder for each request!
+  html = HTML::Builder.new.build do
+    html {
+      head {
+        title { text "Fortunes" }
+      }
+      body {
+        table {
+          tr {
+            thead { text "id" }
+            thead { text "message" }
+          }
+          data.each { |e|
             tr {
-              thead { text "id" }
-              thead { text "message" }
-            }
-            data.each { |e|
-              tr {
-                td { text e[:id].to_s }
-                td { text e[:message].to_s }
-              }
+              td { text e[:id].to_s }
+              td { text e[:message].to_s }
             }
           }
         }
       }
-    end
-
-    # Doctype not available in builder
-    # builder only supports `thead`, tests need to see `th`
-    res = ok("<!doctype html>" + html.gsub("thead", "th"))
-    res.headers["Content-type"] = CONTENT::HTML
-    res
+    }
   end
 
-  # Test 5: Database Updates
-  route "/updates", do |request|
-    updated = (1..sanitizedQueryCount(request)).map do
-      world = randomWorld
-      world[:randomNumber] = rand(1..ID_MAXIMUM)
-      setWorld(world)
-    end
-
-    res = ok(updated.to_json)
-    res.headers["Content-type"] = CONTENT::JSON
-    res
-  end
+  # Doctype not available in builder
+  # builder only supports `thead`, tests need to see `th`
+  res = ok("<!doctype html>" + html.gsub("thead", "th"))
+  res.headers["Content-type"] = CONTENT::HTML
+  res
+end
 
-  # Test 6: Plaintext
-  route "/plaintext", do |request|
-    res = ok("Hello, World!")
-    res.headers["Content-type"] = CONTENT::PLAIN
-    res
+# Redis Test 5: Database Updates
+app.get "/redis/updates", do |request|
+  updated = (1..sanitizedQueryCount(request)).map do
+    world = randomWorld
+    world[:randomNumber] = rand(1..ID_MAXIMUM)
+    setWorld(world)
   end
 
+  res = ok(updated.to_json)
+  res.headers["Content-type"] = CONTENT::JSON
+  res
 end
 
 app.run(8080)