@@ -25,11 +25,11 @@ object Something {
25
25
2017 ,
26
26
List (
27
27
Stuff (Map (), 3.15 )
28
- // Stuff(Map("fuu" -> true, "boo" -> false, "fag" -> true), 3.14),
29
- // Stuff(Map("fuu" -> true), 3.16),
30
- // Stuff(Map("fuu" -> true, "boo \n\r\t" -> false, "fag" -> true, "moar" -> false), 3.17),
31
- // Stuff(Map.empty, 3.18),
32
- // Stuff(Map("fuu" -> true, "boo" -> false, "fag" -> true), 3.19),
28
+ // Stuff(Map("fuu" -> true, "boo" -> false, "fag" -> true), 3.14),
29
+ // Stuff(Map("fuu" -> true), 3.16),
30
+ // Stuff(Map("fuu" -> true, "boo \n\r\t" -> false, "fag" -> true, "moar" -> false), 3.17),
31
+ // Stuff(Map.empty, 3.18),
32
+ // Stuff(Map("fuu" -> true, "boo" -> false, "fag" -> true), 3.19),
33
33
),
34
34
Set (
35
35
1 // 5, 62, -23, 454, 123, 75, -234,
@@ -47,45 +47,152 @@ object Stuff {
47
47
implicit val decoder : Decoder [Stuff ] = deriveDecoder[Stuff ]
48
48
}
49
49
50
+ sealed trait SealedStuff
51
+ case class Case1 (i : Int ) extends SealedStuff
52
+ case class Case2 (i : Int ) extends SealedStuff
53
+ case class Case3 (i : Int ) extends SealedStuff
54
+ case class Case4 (i : Int ) extends SealedStuff
55
+ case class Case5 (i : Int ) extends SealedStuff
56
+ case class Case6 (i : Int ) extends SealedStuff
57
+ case class Case7 (i : Int ) extends SealedStuff
58
+ object SealedStuff {
59
+ implicit val codec : GenCodec [SealedStuff ] = GenCodec .materialize
60
+ implicit val encoder : Encoder [SealedStuff ] = deriveEncoder[SealedStuff ]
61
+ implicit val decoder : Decoder [SealedStuff ] = deriveDecoder[SealedStuff ]
62
+
63
+ final val ExampleList = List [SealedStuff ](Case5 (5 ), Case3 (3 ), Case1 (1 ), Case7 (7 ), Case2 (2 ), Case4 (4 ), Case6 (6 ))
64
+ final val ExampleJson = ExampleList .asJson
65
+ final val ExampleJsonString = ExampleJson .noSpaces
66
+ }
67
+
68
+ case class Foo (s : String , d : Double , i : Int , l : Long , bs : List [Boolean ])
69
+ object Foo {
70
+ implicit val circeEncodeFoo : Encoder [Foo ] = deriveEncoder
71
+ implicit val circeDecodeFoo : Decoder [Foo ] = deriveDecoder
72
+ implicit val codec : GenCodec [Foo ] = GenCodec .materialize
73
+
74
+ final val ExampleMap : Map [String , Foo ] = List .tabulate(100 ) { i =>
75
+ (" b" * i) -> Foo (" a" * i, (i + 2.0 ) / (i + 1.0 ), i, i * 1000L , (0 to i).map(_ % 2 == 0 ).toList)
76
+ }.toMap
77
+
78
+ final val ExampleJson = ExampleMap .asJson
79
+ final val ExampleJsonString = ExampleJson .noSpaces
80
+ }
81
+
50
82
@ Warmup (iterations = 10 )
51
83
@ Measurement (iterations = 20 )
52
84
@ Fork (1 )
53
85
@ BenchmarkMode (Array (Mode .Throughput ))
54
- class JsonSerializationBenchmark {
86
+ abstract class JsonSerializationBenchmark
87
+
88
+ class JsonEncodingBenchmark extends JsonSerializationBenchmark {
89
+ @ Benchmark
90
+ def encodeCCCirce : Json =
91
+ Something .Example .asJson
92
+
93
+ @ Benchmark
94
+ def encodeCCGenCodec : Json =
95
+ CirceJsonOutput .write(Something .Example )
96
+
97
+ @ Benchmark
98
+ def encodeSHCirce : Json =
99
+ SealedStuff .ExampleList .asJson
100
+
101
+ @ Benchmark
102
+ def encodeSHGenCodec : Json =
103
+ CirceJsonOutput .write(SealedStuff .ExampleList )
104
+
105
+ @ Benchmark
106
+ def encodeFoosCirce : Json =
107
+ Foo .ExampleMap .asJson
108
+
109
+ @ Benchmark
110
+ def encodeFoosGenCodec : Json =
111
+ CirceJsonOutput .write(Foo .ExampleMap )
112
+ }
113
+
114
+ class JsonDecodingBenchmark extends JsonSerializationBenchmark {
55
115
@ Benchmark
56
- def genCodecJsonStringWriting : String = JsonStringOutput .write(Something .Example )
116
+ def decodeCCCirce : Something =
117
+ Something .ExampleJson .as[Something ].fold(e => throw e, identity)
57
118
58
119
@ Benchmark
59
- def genCodecJsonStringReading : Something = JsonStringInput .read[Something ](Something .ExampleJsonString )
120
+ def decodeCCGenCodec : Something =
121
+ CirceJsonInput .read[Something ](Something .ExampleJson )
60
122
61
123
@ Benchmark
62
- def circeJsonStringWriting : String = Something .Example .asJson.noSpaces
124
+ def decodeSHCirce : List [SealedStuff ] =
125
+ SealedStuff .ExampleJson .as[List [SealedStuff ]].fold(e => throw e, identity)
63
126
64
127
@ Benchmark
65
- def circeJsonStringReading : Something = decode[Something ](Something .ExampleJsonString ).fold(e => throw e, identity)
128
+ def decodeSHGenCodec : List [SealedStuff ] =
129
+ CirceJsonInput .read[List [SealedStuff ]](SealedStuff .ExampleJson )
66
130
67
131
@ Benchmark
68
- def genCodecJsonWriting : Json = CirceJsonOutput .write(Something .Example )
132
+ def decodeFoosCirce : Map [String , Foo ] =
133
+ Foo .ExampleJson .as[Map [String , Foo ]].fold(e => throw e, identity)
69
134
70
135
@ Benchmark
71
- def genCodecJsonReading : Something = CirceJsonInput .read[Something ](Something .ExampleJson )
136
+ def decodeFoosGenCodec : Map [String , Foo ] =
137
+ CirceJsonInput .read[Map [String , Foo ]](Foo .ExampleJson )
138
+ }
139
+
140
+ class JsonWritingBenchmark extends JsonSerializationBenchmark {
141
+ @ Benchmark
142
+ def writeCCCirce : String =
143
+ Something .Example .asJson.noSpaces
144
+
145
+ @ Benchmark
146
+ def writeCCGenCodec : String =
147
+ JsonStringOutput .write(Something .Example )
148
+
149
+ @ Benchmark
150
+ def writeSHCirce : String =
151
+ SealedStuff .ExampleList .asJson.noSpaces
152
+
153
+ @ Benchmark
154
+ def writeSHGenCodec : String =
155
+ JsonStringOutput .write(SealedStuff .ExampleList )
72
156
73
157
@ Benchmark
74
- def circeJsonWriting : Json = Something .Example .asJson
158
+ def writeFoosCirce : String =
159
+ Foo .ExampleMap .asJson.noSpaces
75
160
76
161
@ Benchmark
77
- def circeJsonReading : Something = Something .ExampleJson .as[Something ].fold(e => throw e, identity)
162
+ def writeFoosGenCodec : String =
163
+ JsonStringOutput .write(Foo .ExampleMap )
164
+ }
165
+
166
+ class JsonReadingBenchmark extends JsonSerializationBenchmark {
167
+ @ Benchmark
168
+ def readCCCirce : Something =
169
+ decode[Something ](Something .ExampleJsonString ).fold(e => throw e, identity)
170
+
171
+ @ Benchmark
172
+ def readCCGenCodec : Something =
173
+ JsonStringInput .read[Something ](Something .ExampleJsonString )
174
+
175
+ @ Benchmark
176
+ def readSHCirce : List [SealedStuff ] =
177
+ decode[List [SealedStuff ]](SealedStuff .ExampleJsonString ).fold(e => throw e, identity)
178
+
179
+ @ Benchmark
180
+ def readSHGenCodec : List [SealedStuff ] =
181
+ JsonStringInput .read[List [SealedStuff ]](SealedStuff .ExampleJsonString )
182
+
183
+ @ Benchmark
184
+ def readFoosCirce : Map [String , Foo ] =
185
+ decode[Map [String , Foo ]](Foo .ExampleJsonString ).fold(e => throw e, identity)
186
+
187
+ @ Benchmark
188
+ def readFoosGenCodec : Map [String , Foo ] =
189
+ JsonStringInput .read[Map [String , Foo ]](Foo .ExampleJsonString )
78
190
}
79
191
80
192
object JsonSerializationBenchmark {
81
193
def main (args : Array [String ]): Unit = {
82
- println(JsonStringOutput .write(Something .Example ) == Something .ExampleJsonString )
83
- println(JsonStringInput .read[Something ](Something .ExampleJsonString ) == Something .Example )
84
- println(CirceJsonOutput .write(Something .Example ) == Something .ExampleJson )
85
- println(CirceJsonInput .read[Something ](Something .ExampleJson ) == Something .Example )
86
-
87
- while (true ) {
88
- CirceJsonInput .read[Something ](Something .ExampleJson )
194
+ while (true ) {
195
+ JsonStringOutput .write[List [SealedStuff ]](SealedStuff .ExampleList )
89
196
}
90
197
}
91
198
}
0 commit comments