Skip to content

Commit 72e7b7b

Browse files
committed
Add benchmark for a single int value
1 parent 658145c commit 72e7b7b

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.plokhotnyuk.jsoniter_scala.macros
2+
3+
import java.nio.charset.StandardCharsets._
4+
5+
import com.github.plokhotnyuk.jsoniter_scala.core._
6+
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
7+
import com.github.plokhotnyuk.jsoniter_scala.macros.JacksonSerDesers._
8+
import com.github.plokhotnyuk.jsoniter_scala.macros.JsoniterCodecs._
9+
import io.circe.parser._
10+
import io.circe.syntax._
11+
import org.openjdk.jmh.annotations.Benchmark
12+
import play.api.libs.json.Json
13+
14+
class IntBenchmark extends CommonParams {
15+
val obj: Int = 1234567890
16+
val jsonString: String = obj.toString
17+
val jsonBytes: Array[Byte] = jsonString.getBytes
18+
19+
@Benchmark
20+
def readNaiveScala(): Int = new String(jsonBytes, UTF_8).toInt
21+
22+
@Benchmark
23+
def readCirce(): Int = decode[Int](new String(jsonBytes, UTF_8)).fold(throw _, x => x)
24+
25+
@Benchmark
26+
def readJacksonScala(): Int = jacksonMapper.readValue[Int](jsonBytes)
27+
28+
@Benchmark
29+
def readJsoniterScala(): Int = read[Int](jsonBytes)
30+
31+
@Benchmark
32+
def readPlayJson(): Int = Json.parse(jsonBytes).as[Int]
33+
34+
@Benchmark
35+
def writeNaiveScala(): Array[Byte] = obj.toString.getBytes(UTF_8)
36+
37+
@Benchmark
38+
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)
39+
40+
@Benchmark
41+
def writeJacksonScala(): Array[Byte] = jacksonMapper.writeValueAsBytes(obj)
42+
43+
@Benchmark
44+
def writeJsoniterScala(): Array[Byte] = write(obj)
45+
46+
@Benchmark
47+
def writeJsoniterScalaPrealloc(): Int = write(obj, preallocatedBuf, 0)
48+
49+
@Benchmark
50+
def writePlayJson(): Array[Byte] = Json.toBytes(Json.toJson(obj))
51+
}

benchmark/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsoniterCodecs.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ object JsoniterCodecs {
4343
implicit val mutableIterablesCodec: JsonValueCodec[MutableIterables] = make[MutableIterables](CodecMakerConfig())
4444
implicit val mutableMapsCodec: JsonValueCodec[MutableMaps] = make[MutableMaps](CodecMakerConfig())
4545
implicit val intAndLongMapsCodec: JsonValueCodec[IntAndLongMaps] = make[IntAndLongMaps](CodecMakerConfig())
46+
implicit val intCodec: JsonValueCodec[Int] = make[Int](CodecMakerConfig())
4647
implicit val primitivesCodec: JsonValueCodec[Primitives] = make[Primitives](CodecMakerConfig())
4748
implicit val extractFieldsCodec: JsonValueCodec[ExtractFields] = make[ExtractFields](CodecMakerConfig())
4849
implicit val adtCodec: JsonValueCodec[AdtBase] = make[AdtBase](CodecMakerConfig())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.plokhotnyuk.jsoniter_scala.macros
2+
3+
class IntBenchmarkSpec extends BenchmarkSpecBase {
4+
val benchmark = new IntBenchmark
5+
6+
"IntBenchmark" should {
7+
"deserialize properly" in {
8+
benchmark.readNaiveScala() shouldBe benchmark.obj
9+
benchmark.readCirce() shouldBe benchmark.obj
10+
benchmark.readJacksonScala() shouldBe benchmark.obj
11+
benchmark.readJsoniterScala() shouldBe benchmark.obj
12+
benchmark.readPlayJson() shouldBe benchmark.obj
13+
}
14+
"serialize properly" in {
15+
toString(benchmark.writeNaiveScala()) shouldBe benchmark.jsonString
16+
toString(benchmark.writeCirce()) shouldBe benchmark.jsonString
17+
toString(benchmark.writeJacksonScala()) shouldBe benchmark.jsonString
18+
toString(benchmark.writeJsoniterScala()) shouldBe benchmark.jsonString
19+
toString(benchmark.preallocatedBuf, benchmark.writeJsoniterScalaPrealloc()) shouldBe benchmark.jsonString
20+
toString(benchmark.writePlayJson()) shouldBe benchmark.jsonString
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)