-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJsonDecodeBenchmark.scala
More file actions
106 lines (94 loc) · 2.69 KB
/
Copy pathJsonDecodeBenchmark.scala
File metadata and controls
106 lines (94 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package benchmark
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit
import java.nio.file.{Files, Path}
import com.gu.contentapi.client.model.v1._
import com.gu.contentapi.json.CirceDecoders._
import com.gu.contentapi.json.CirceEncoders._
import io.circe.syntax._
import io.circe.Json
case class SearchResp(
status: String,
userTier: String,
total: Int,
startIndex: Int,
pageSize: Int,
currentPage: Int,
pages: Int,
orderBy: String,
results: List[Json]
)
case class WrappedLegacyResponse(response: SearchResp)
@State(Scope.Thread)
class JsonDecodeBenchmark {
def loadJson(filename: String): Json = {
val rawJson = Files.readString(Path.of(s"benchmarks/src/main/resources/$filename"))
val circeJson = {
import io.circe.jawn._
parse(rawJson).fold(_ => Json.Null, identity)
}
circeJson
}
val contentJson = loadJson("content-with-blocks.json")
val massiveContentsListJson = loadJson("massive-contents-list.json")
val contentOpt = contentJson.as[Content].fold(e => throw e, identity)
@Benchmark
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
def circeDecode(bh: Blackhole) = {
bh.consume(contentJson.as[Content])
}
@Benchmark
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
def circeEncode(bh: Blackhole) = {
bh.consume(contentOpt.asJson)
}
@Benchmark
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
def circeDecodeEachContent(bh: Blackhole) = {
val results =
for {
i <- 1 to 10
content = contentJson.as[Content].fold(e => throw e, identity)
} yield content
val response = SearchResponse(
status = "ok",
userTier = "foo",
total = 123,
startIndex = 123,
pageSize = 123,
currentPage = 123,
pages = 123,
orderBy = "foo",
results = results
)
bh.consume(response)
}
@Benchmark
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MILLISECONDS)
def circeDecodeEachMassiveContent(bh: Blackhole) = {
massiveContentsListJson.asArray.foreach { contentJsonArray: Vector[Json] =>
val results =
for {
contentJson <- contentJsonArray
content = contentJson.as[Content].fold(e => throw e, identity)
} yield content
val response = SearchResponse(
status = "ok",
userTier = "foo",
total = 123,
startIndex = 123,
pageSize = 123,
currentPage = 123,
pages = 123,
orderBy = "foo",
results = results
)
bh.consume(response)
}
}
}