Skip to content

Commit bb375c3

Browse files
2 parents 4f43803 + 135396c commit bb375c3

File tree

8 files changed

+101
-74
lines changed

8 files changed

+101
-74
lines changed

play-scala/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
logs
22
project/project
33
project/target
4+
public
45
target
6+
test
57
tmp
68
.history
7-
dist
9+
dist
10+
conf/evolutions

play-scala/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ This is the Play portion of a [benchmarking test suite](../) comparing a variety
44

55
### JSON Encoding Test
66

7-
* [JSON test source](app/controllers/Application.java)
7+
* [JSON test source](app/controllers/Application.scala)
88

99
### Data-Store/Database Mapping Test
1010

1111
* [Database test controller](app/controllers/Application.scala)
12-
* [Database test model](app/models/World.java)
12+
* [Database test model](app/models/World.scala)
1313

1414
## Infrastructure Software Versions
1515
The tests were run with:
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
package controllers
22

3-
import play._
4-
import play.api.libs.concurrent._
3+
import play.api._
54
import play.api.mvc._
6-
import play.libs.Json
7-
import org.codehaus.jackson.node.ObjectNode
8-
import views.html._
9-
import models._
10-
import java.util._
5+
import play.api.libs.json.Json
6+
import play.api.libs.concurrent._
117
import java.util.concurrent.ThreadLocalRandom
128
import scala.concurrent._
9+
import models._
1310

1411
object Application extends Controller {
12+
1513
private val TEST_DATABASE_ROWS = 10000
1614

1715
def json() = Action {
18-
val result = Json.newObject()
19-
result.put("message", "Hello World!")
20-
Ok(result.toString)
16+
Ok(Json.obj("message" -> "Hello World!"))
2117
}
2218

2319
def db(queries: Int) = Action {
2420
import play.api.libs.concurrent.Execution.Implicits._
2521

26-
val random = ThreadLocalRandom.current()
27-
2822
Async {
29-
Future {
30-
(1 to queries) map {
31-
_ =>
32-
World.find.byId(random.nextInt(TEST_DATABASE_ROWS) + 1)
33-
}
34-
} map {
35-
worlds =>
36-
Ok(Json.toJson(worlds).toString())
37-
}
23+
val random = ThreadLocalRandom.current()
24+
25+
val worlds = Future.sequence( (for {
26+
_ <- (1 to queries).par
27+
} yield Future(World.findById(random.nextInt(TEST_DATABASE_ROWS) + 1))).toList)
28+
29+
worlds map {
30+
w => Ok(Json.toJson(w))
31+
}
3832
}
39-
}
40-
}
33+
}
34+
}

play-scala/app/models/World.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

play-scala/app/models/World.scala

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package models
2+
3+
import play.api.db._
4+
import play.api.Play.current
5+
import anorm._
6+
import anorm.SqlParser._
7+
import play.api.libs.json._
8+
import play.api.libs.functional.syntax._
9+
10+
case class World(id: Pk[Long], randomNumber: Long)
11+
12+
object World {
13+
/**
14+
* Convert a World to Json object
15+
*/
16+
implicit val toJson = new Writes[World] {
17+
def writes(w: World): JsValue = {
18+
Json.obj(
19+
"id" -> w.id.get,
20+
"randomNumber" -> w.randomNumber
21+
)
22+
}
23+
}
24+
25+
/**
26+
* Parse a World from a ResultSet
27+
*/
28+
val simpleRowParser = {
29+
get[Pk[Long]]("world.id") ~
30+
get[Long]("world.randomNumber") map {
31+
case id~randomNumber => World(id, randomNumber)
32+
}
33+
}
34+
35+
/**
36+
* Retrieve a World by id.
37+
*/
38+
def findById(id: Long): World = {
39+
DB.withConnection { implicit connection =>
40+
SQL("SELECT * FROM world WHERE id = {id}").on(
41+
"id" -> id
42+
).as(World.simpleRowParser.single)
43+
}
44+
}
45+
}

play-scala/conf/application.conf

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ application.langs="en"
2222
# You can declare as many datasources as you want.
2323
# By convention, the default datasource is named `default`
2424
#
25-
# db.default.driver=org.h2.Driver
26-
# db.default.url="jdbc:h2:mem:play"
27-
# db.default.user=sa
25+
#db.default.driver=org.h2.Driver
26+
#db.default.url="jdbc:h2:mem:play"
27+
#db.default.user=sa
2828
# db.default.password=
2929
#
3030
# You can expose this datasource via JNDI if needed (Useful for JPA)
3131
# db.default.jndiName=DefaultDS
3232
db.default.driver= com.mysql.jdbc.Driver
3333
db.default.url="jdbc:mysql://localhost:3306/hello_world"
34+
#db.default.url="jdbc:mysql://192.168.100.101:3306/hello_world"
3435
db.default.user=benchmarkdbuser
3536
db.default.password=benchmarkdbpass
3637
db.default.jndiName=DefaultDS
@@ -52,13 +53,6 @@ db.default.minConnectionsPerPartition=5
5253
# You can disable evolutions if needed
5354
# evolutionplugin=disabled
5455

55-
# Ebean configuration
56-
# ~~~~~
57-
# You can declare as many Ebean servers as you want.
58-
# By convention, the default server is named `default`
59-
#
60-
ebean.default="models.*"
61-
6256
# Logger
6357
# ~~~~~
6458
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
@@ -72,3 +66,22 @@ logger.play=ERROR
7266
# Logger provided to your application:
7367
logger.application=ERROR
7468

69+
play {
70+
akka {
71+
event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
72+
loglevel = WARNING
73+
actor {
74+
default-dispatcher = {
75+
fork-join-executor {
76+
parallelism-factor = 1.0
77+
parallelism-max = 50
78+
}
79+
}
80+
application = {
81+
fork-join-executor {
82+
parallelism-max = 300
83+
}
84+
}
85+
}
86+
}
87+
}

play-scala/conf/routes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ GET /json controllers.Application.json
77
GET /db controllers.Application.db(queries: Int ?= 1)
88

99
# Map static resources from the /public folder to the /assets URL path
10-
GET /assets/*file controllers.Assets.at(path="/public", file)
10+
GET /assets/*file controllers.Assets.at(path="/public", file)

play-scala/project/Build.scala

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import PlayProject._
44

55
object ApplicationBuild extends Build {
66

7-
val appName = "play-scala"
8-
val appVersion = "1.0-SNAPSHOT"
7+
val appName = "play-scala"
8+
val appVersion = "1.0-SNAPSHOT"
99

10-
val appDependencies = Seq(
11-
// Add your project dependencies here,
12-
javaCore,
13-
javaJdbc,
14-
javaEbean,
15-
"mysql" % "mysql-connector-java" % "5.1.22"
10+
val appDependencies = Seq(
11+
jdbc,
12+
anorm,
13+
"mysql" % "mysql-connector-java" % "5.1.22"
14+
)
1615

17-
)
18-
19-
val main = play.Project(appName, appVersion, appDependencies).settings(
20-
// Add your own project settings here
21-
)
16+
val main = play.Project(appName, appVersion, appDependencies).settings(
17+
)
2218

2319
}

0 commit comments

Comments
 (0)