Browse Source

Merge pull request #1313 from jmaddux-techempower/fix-play-scala-mongodb

Fix play-scala-mongodb
Mike Smith 10 years ago
parent
commit
28c82dc2a0

+ 6 - 5
frameworks/Scala/play-scala-mongodb/README.md

@@ -10,10 +10,11 @@ This is the Play portion of a [benchmarking test suite](../) comparing a variety
 The tests were run with:
 The tests were run with:
 
 
 * [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
 * [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
-* [Play 2.1.0](http://http://www.playframework.com/)
-* [Reactivemongo 0.9-SNAPSHOT](https://github.com/zenexity/Play-ReactiveMongo)
+* [Play 2.2.0](http://http://www.playframework.com/)
+* [Reactivemongo 0.10.5.0.akka22](https://github.com/zenexity/Play-ReactiveMongo)
 
 
 ## Test URLs
 ## Test URLs
-### Data-Store/Database Mapping Test
-
-http://localhost/db?queries=5
+### Single Database Query test
+http://localhost:9000/db
+### Multiple Database Query test
+http://localhost:9000/queries?queries=5

+ 29 - 4
frameworks/Scala/play-scala-mongodb/app/controllers/Application.scala

@@ -32,21 +32,46 @@ object Application extends Controller {
 
 
   private def collection: JSONCollection = database.collection[JSONCollection]("world")
   private def collection: JSONCollection = database.collection[JSONCollection]("world")
   private val projection = Json.obj("_id" -> 0)
   private val projection = Json.obj("_id" -> 0)
+  /**
+   * Returns the closest number to <code>toRestrict</code> that is within the
+   * specified bounds, inclusive on both ends.
+   */
+  private def restrictWithin(toRestrict: String, lowerBound: Int, upperBound: Int): Option[Int] = {
+    try {
+      Some(math.min(upperBound, math.max(toRestrict.toInt, lowerBound)))
+    } catch {
+      case e: Exception => None
+    }
+  }
 
 
-  def db(queries: Int) = Action.async {
+  def dbqueries(requestedQueries: String) = Action.async {
     import scala.concurrent.ExecutionContext.Implicits.global
     import scala.concurrent.ExecutionContext.Implicits.global
 
 
     val random = ThreadLocalRandom.current()
     val random = ThreadLocalRandom.current()
+    val queries = restrictWithin(requestedQueries, 1, 500).getOrElse(1)
     val futureWorlds = Future.sequence((for {
     val futureWorlds = Future.sequence((for {
       _ <- 1 to queries
       _ <- 1 to queries
     } yield { collection
     } yield { collection
       .find(Json.obj("id" -> (random.nextInt(TestDatabaseRows) + 1)), projection)
       .find(Json.obj("id" -> (random.nextInt(TestDatabaseRows) + 1)), projection)
       .one[JsValue]
       .one[JsValue]
     }))
     }))
-
     futureWorlds.map { worlds =>
     futureWorlds.map { worlds =>
-      Ok(Json.toJson(worlds))
+      Ok(Json.toJson(worlds.map {maybeWorld =>
+        maybeWorld.map {world =>
+          world.as[Map[String, Int]]
+        }
+      }))
     }
     }
   }
   }
+  def singledb() = Action.async {
+    import scala.concurrent.ExecutionContext.Implicits.global
 
 
-}
+    val random = ThreadLocalRandom.current()
+    val futureWorld = collection
+      .find(Json.obj("id" -> (random.nextInt(TestDatabaseRows) + 1)), projection)
+      .one[JsValue]
+    futureWorld.map { world =>
+      Ok(Json.toJson(world.head.as[Map[String, Int]]))
+    }
+  }
+}

+ 1 - 1
frameworks/Scala/play-scala-mongodb/benchmark_config

@@ -4,7 +4,7 @@
     "default": {
     "default": {
       "setup_file": "setup",
       "setup_file": "setup",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/queries?queries=",
       "port": 9000,
       "port": 9000,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Fullstack",
       "classification": "Fullstack",

+ 2 - 1
frameworks/Scala/play-scala-mongodb/conf/routes

@@ -3,7 +3,8 @@
 # ~~~~
 # ~~~~
 
 
 # Home page
 # Home page
-GET     /db                             controllers.Application.db(queries: Int ?= 1)
+GET     /db                             controllers.Application.singledb()
+GET     /queries                        controllers.Application.dbqueries(queries: String ?= "1")
 
 
 # Map static resources from the /public folder to the /assets URL path
 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)

+ 1 - 1
frameworks/Scala/play-scala-mongodb/project/Build.scala

@@ -7,7 +7,7 @@ object ApplicationBuild extends Build {
   val appVersion      = "1.0-SNAPSHOT"
   val appVersion      = "1.0-SNAPSHOT"
 
 
   val appDependencies = Seq(
   val appDependencies = Seq(
-    "org.reactivemongo" %% "play2-reactivemongo" % "0.9" exclude("org.scala-stm", "scala-stm_2.10.0")
+    "org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka22" exclude("org.scala-stm", "scala-stm_2.10.0")
   )
   )
 
 
   val main = play.Project(appName, appVersion, appDependencies).settings(
   val main = play.Project(appName, appVersion, appDependencies).settings(