Browse Source

Merge branch 'play-scala-test5' of https://github.com/Skamander/FrameworkBenchmarks into Skamander-play-scala-test5

Patrick Falls 12 years ago
parent
commit
e5fc8cc884

+ 29 - 1
play-scala/app/controllers/Application.scala

@@ -62,10 +62,38 @@ object Application extends Controller {
     Action {
       Async {
         Future(Fortune.getAll())(dbEc).map { fs =>
-          val fortunes =  fs :+ Fortune(anorm.NotAssigned, "Additional fortune added at request time.")
+          val fortunes =  Fortune(anorm.NotAssigned, "Additional fortune added at request time.") +: fs
           Ok(views.html.fortune(fortunes))
         }
       }
     }
   }
+
+  def update(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
+    Action {
+      Async {
+        val random = ThreadLocalRandom.current()
+
+        val boundsCheckedQueries = queries match {
+          case q if q > 500 => 500
+          case q if q <   1 => 1
+          case _ => queries
+        }
+
+        val worlds = Future.sequence((for {
+          _ <- 1 to boundsCheckedQueries
+        } yield Future {
+            val world = World.findById(random.nextInt(TestDatabaseRows) + 1)
+            val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
+            World.updateRandom(updatedWorld)
+            updatedWorld
+          }(dbEc)
+        ).toList)
+
+        worlds.map {
+          w => Ok(Json.toJson(w)).withHeaders("Server" -> "Netty")
+        }
+      }
+    }
+  }
 }

+ 27 - 18
play-scala/app/models/World.scala

@@ -13,33 +13,42 @@ object World {
     /**
     * Convert a World to Json object
     */
-    implicit val toJson = new Writes[World] {
-        def writes(w: World): JsValue = {
-            Json.obj(
-                "id" -> w.id.get,
-                "randomNumber" -> w.randomNumber
-            )
-        }
+  implicit val toJson = new Writes[World] {
+    def writes(w: World): JsValue = {
+      Json.obj(
+        "id" -> w.id.get,
+        "randomNumber" -> w.randomNumber
+      )
     }
+  }
 
   /**
    * Parse a World from a ResultSet
    */
-    val simpleRowParser = {
-        get[Pk[Long]]("world.id") ~
-        get[Long]("world.randomNumber") map {
-            case id~randomNumber => World(id, randomNumber)
-        }
+  val simpleRowParser = {
+    get[Pk[Long]]("world.id") ~
+    get[Long]("world.randomNumber") map {
+      case id~randomNumber => World(id, randomNumber)
     }
+  }
 
   /**
    * Retrieve a World by id.
    */
-    def findById(id: Long): World = {
-        DB.withConnection { implicit connection =>
-            SQL("SELECT * FROM World WHERE id = {id}").on(
-                "id" -> id
-            ).as(World.simpleRowParser.single)
-        }
+  def findById(id: Long): World = {
+    DB.withConnection { implicit connection =>
+      SQL("SELECT * FROM World WHERE id = {id}").on(
+          "id" -> id
+      ).as(World.simpleRowParser.single)
     }
+  }
+
+  def updateRandom(world: World) {
+    DB.withConnection { implicit connection =>
+      SQL("UPDATE World SET randomNumber = {randomNumber} WHERE id = {id}").on(
+        "id" -> world.id.get,
+        "randomNumber" -> world.randomNumber
+      ).executeUpdate()
+    }
+  }
 }

+ 1 - 0
play-scala/benchmark_config

@@ -7,6 +7,7 @@
       "db_url": "/db",
       "query_url": "/db?queries=",
       "fortune_url": "/fortunes",
+      "update_url": "/update?queries=",
       "port": 9000,
       "sort": 32
     }

+ 1 - 0
play-scala/conf/routes

@@ -6,6 +6,7 @@
 GET     /json                           controllers.Application.json
 GET     /db                             controllers.Application.db(queries: Int ?= 1)
 GET     /fortunes                       controllers.Application.fortunes
+GET     /update                         controllers.Application.update(queries: Int ?= 1)
 
 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)