Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize object merging for common cases used in Json macros (#69)
- Loading branch information
1 parent
8642c48
commit 651b57e
Showing
9 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
import pl.project13.scala.sbt.JmhPlugin | ||
|
||
sourceDirectory in Jmh := (sourceDirectory in Test).value | ||
classDirectory in Jmh := (classDirectory in Test).value | ||
dependencyClasspath in Jmh := (dependencyClasspath in Test).value | ||
// rewire tasks, so that 'jmh:run' automatically invokes 'jmh:compile' (otherwise a clean 'jmh:run' would fail) | ||
compile in Jmh <<= (compile in Jmh) dependsOn (compile in Test) | ||
run in Jmh <<= (run in Jmh) dependsOn (Keys.compile in Jmh) |
29 changes: 29 additions & 0 deletions
29
benchmarks/src/test/scala/play/api/libs/json/Employee.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package play.api.libs.json | ||
|
||
case class Employee( | ||
employeeNumber: Int, | ||
firstName: String, | ||
lastName: String, | ||
city: String, | ||
country: String, | ||
tags: Seq[String] | ||
) | ||
|
||
object Employee { | ||
implicit val employeeFormat: Format[Employee] = Json.format[Employee] | ||
|
||
val manualWrites: Writes[Employee] = Writes { e => | ||
Json.obj( | ||
"employeeNumber" -> e.employeeNumber, | ||
"firstName" -> e.firstName, | ||
"lastName" -> e.lastName, | ||
"city" -> e.city, | ||
"country" -> e.country, | ||
"tags" -> JsArray(e.tags.map(JsString.apply)) | ||
) | ||
} | ||
|
||
val manualSeqWrites: Writes[Seq[Employee]] = Writes { seq => | ||
JsArray(seq.map(manualWrites.writes)) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
benchmarks/src/test/scala/play/api/libs/json/JsonMacros_01_SerializeSimpleCaseClass.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (C) 2009-2017 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.api.libs.json | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
@State(Scope.Benchmark) | ||
class JsonMacros_01_SerializeSimpleCaseClass { | ||
|
||
var employee: Employee = _ | ||
var employeeJson: JsValue = _ | ||
|
||
@Setup(Level.Iteration) | ||
def setup(): Unit = { | ||
employee = Employee( | ||
42, | ||
"Foo", | ||
"Bar", | ||
"New York", | ||
"United States", | ||
Seq("engineering", "new", "bar") | ||
) | ||
} | ||
|
||
@TearDown(Level.Iteration) | ||
def tearDown(): Unit = { | ||
assert(employeeJson == Json.parse( | ||
""" { | ||
| "employeeNumber": 42, | ||
| "firstName": "Foo", | ||
| "lastName": "Bar", | ||
| "city": "New York", | ||
| "country": "United States", | ||
| "tags": ["engineering", "new", "bar"] | ||
| } | ||
""".stripMargin)) | ||
} | ||
|
||
@Benchmark | ||
def toJson(): JsValue = { | ||
employeeJson = Json.toJson(employee) | ||
employeeJson | ||
} | ||
|
||
@Benchmark | ||
def toJsonManualWrites(): JsValue = { | ||
employeeJson = Json.toJson(employee)(Employee.manualWrites) | ||
employeeJson | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
benchmarks/src/test/scala/play/api/libs/json/JsonMacros_02_SerializeList.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package play.api.libs.json | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
@State(Scope.Benchmark) | ||
class JsonMacros_02_SerializeList { | ||
|
||
var employees: Seq[Employee] = _ | ||
var employeesJson: JsValue = _ | ||
|
||
@Setup(Level.Iteration) | ||
def setup(): Unit = { | ||
employees = (1 to 100) map { id => | ||
Employee( | ||
id, | ||
s"Foo$id", | ||
s"Bar$id", | ||
"New York", | ||
"United States", | ||
Seq("a", "b", "c") | ||
) | ||
} | ||
} | ||
|
||
@TearDown(Level.Iteration) | ||
def tearDown(): Unit = { | ||
|
||
} | ||
|
||
@Benchmark | ||
def toJson(): JsValue = { | ||
employeesJson = Json.toJson(employees) | ||
employeesJson | ||
} | ||
|
||
@Benchmark | ||
def toJsonManualWrites(): JsValue = { | ||
employeesJson = Json.toJson(employees)(Employee.manualSeqWrites) | ||
employeesJson | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters