Skip to content

Add AVSystem GenCodec benchmarks #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ lazy val commonSettings = Seq(
"-unchecked",
"-Ywarn-dead-code",
"-Xfuture",
"-Xlint",
"-Xmacro-settings:print-codecs"
"-Xlint"//,
//"-Xmacro-settings:print-codecs"
) ++ (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, x)) if x >= 12 => Seq("-opt:l:method")
case Some((2, x)) if x == 11 => Seq("-Ybackend:GenBCode", "-Ydelambdafy:inline")
Expand Down Expand Up @@ -118,6 +118,8 @@ lazy val `jsoniter-scala-benchmark` = project
.settings(
crossScalaVersions := Seq("2.12.6", "2.11.12"),
libraryDependencies ++= Seq(
"com.avsystem.commons" %% "commons-core" % "1.28.2",
"com.github.ghik" %% "silencer-lib" % "0.6",
"com.lihaoyi" %% "upickle" % "0.6.6",
"com.dslplatform" %% "dsl-json-scala" % "1.7.4",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.plokhotnyuk.jsoniter_scala.macros

import java.time._
import java.util.UUID

import com.avsystem.commons.serialization.GenCodec
import com.avsystem.commons.serialization.GenCodec._
import com.github.plokhotnyuk.jsoniter_scala.macros.SuitEnum.SuitEnum

import scala.collection.immutable.{BitSet, IntMap}
import scala.collection.mutable

object AVSystemCodecs {
implicit val adtGenCodec: GenCodec[AdtBase] = materializeRecursively[AdtBase]
implicit val anyRefsGenCodec: GenCodec[AnyRefs] = materialize[AnyRefs]
implicit val durationGenCodec: GenCodec[Duration] = transformed[Duration, String](_.toString, Duration.parse)
implicit val suitEnumGenCodec: GenCodec[SuitEnum] = transformed[SuitEnum, String](_.toString, SuitEnum.withName)
implicit val instantGenCodec: GenCodec[Instant] = transformed[Instant, String](_.toString, Instant.parse)
implicit val localDateGenCodec: GenCodec[LocalDate] = transformed[LocalDate, String](_.toString, LocalDate.parse)
implicit val localDateTimeGenCodec: GenCodec[LocalDateTime] =
transformed[LocalDateTime, String](_.toString, LocalDateTime.parse)
implicit val localTimeGenCodec: GenCodec[LocalTime] = transformed[LocalTime, String](_.toString, LocalTime.parse)
implicit val offsetDateTimeGenCodec: GenCodec[OffsetDateTime] =
transformed[OffsetDateTime, String](_.toString, OffsetDateTime.parse)
implicit val offsetTimeGenCodec: GenCodec[OffsetTime] = transformed[OffsetTime, String](_.toString, OffsetTime.parse)
implicit val periodGenCodec: GenCodec[Period] = transformed[Period, String](_.toString, Period.parse)
implicit val uuidGenCodec: GenCodec[UUID] = transformed[UUID, String](_.toString, UUID.fromString)
implicit val yearGenCodec: GenCodec[Year] = transformed[Year, String](_.toString, Year.parse)
implicit val yearMonthGenCodec: GenCodec[YearMonth] = transformed[YearMonth, String](_.toString, YearMonth.parse)
implicit val zonedDateTimeGenCodec: GenCodec[ZonedDateTime] =
transformed[ZonedDateTime, String](_.toString, ZonedDateTime.parse)
implicit val zoneIdGenCodec: GenCodec[ZoneId] = transformed[ZoneId, String](_.toString, ZoneId.of)
implicit val zoneOffsetGenCodec: GenCodec[ZoneOffset] = transformed[ZoneOffset, String](_.toString, ZoneOffset.of)
implicit val bitSetGenCodec: GenCodec[BitSet] = transformed[BitSet, Array[Int]](_.toArray, x => BitSet(x:_*)) // WARNING: don't do this for open-system
implicit val extractFieldsGenCodec: GenCodec[ExtractFields] = materialize[ExtractFields]
implicit val geoJSONGenCodec: GenCodec[GeoJSON] = materializeRecursively[GeoJSON]
implicit val googleMapsAPIGenCodec: GenCodec[DistanceMatrix] = materializeRecursively[DistanceMatrix]
implicit val intMapOfBooleansGenCodec: GenCodec[IntMap[Boolean]] =
transformed[IntMap[Boolean], Map[Int, Boolean]](_.seq, x => IntMap(x.toArray:_*))
implicit val missingReqFieldGenCodec: GenCodec[MissingReqFields] = materialize[MissingReqFields]
implicit val mutableBitSetGenCodec: GenCodec[mutable.BitSet] = // WARNING: don't do this for open-system
transformed[mutable.BitSet, Array[Int]](_.toArray, x => mutable.BitSet(x:_*))
implicit val mutableLongMapOfBooleansGenCodec: GenCodec[mutable.LongMap[Boolean]] =
transformed[mutable.LongMap[Boolean], mutable.Map[Long, Boolean]](_.seq, x => mutable.LongMap(x.toArray:_*))
implicit val nestedStructsGenCodec: GenCodec[NestedStructs] = materializeRecursively[NestedStructs]
implicit val primitivesGenCodec: GenCodec[Primitives] = materialize[Primitives]
implicit val twitterAPIGenCodec: GenCodec[Tweet] = materializeRecursively[Tweet]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.flatten
import com.avsystem.commons.serialization.json._
import com.fasterxml.jackson.annotation.JsonSubTypes.Type
import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.AVSystemCodecs._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.JacksonSerDesers._
import com.github.plokhotnyuk.jsoniter_scala.macros.JsoniterCodecs._
Expand All @@ -16,6 +19,7 @@ import org.openjdk.jmh.annotations.Benchmark
import play.api.libs.json.Json
//import upickle.default._

@flatten("type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes(Array(
new Type(value = classOf[A], name = "A"),
Expand All @@ -35,6 +39,9 @@ class AdtBenchmark extends CommonParams {
var jsonString2: String = """{"l":{"a":1,"type":"A"},"r":{"b":"VVV","type":"B"},"type":"C"}"""
var jsonBytes: Array[Byte] = jsonString.getBytes(UTF_8)

@Benchmark
def readAVSystemGenCodec(): AdtBase = JsonStringInput.read[AdtBase](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): AdtBase = decode[AdtBase](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

Expand All @@ -51,6 +58,9 @@ class AdtBenchmark extends CommonParams {
@Benchmark
def readUPickle(): AdtBase = read[AdtBase](jsonBytes)
*/
@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)

@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

//import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
//import com.github.plokhotnyuk.jsoniter_scala.macros.AVSystemCodecs._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.DslPlatformJson._
import com.github.plokhotnyuk.jsoniter_scala.macros.JacksonSerDesers._
Expand All @@ -22,7 +24,10 @@ class AnyRefsBenchmark extends CommonParams {
var obj: AnyRefs = AnyRefs("s", 1, Some("os"))
var jsonString: String = """{"s":"s","bd":1,"os":"os"}"""
var jsonBytes: Array[Byte] = jsonString.getBytes(UTF_8)

/* FIXME: AVSystem GenCodec store option field as JSON array
@Benchmark
def readAVSystemGenCodec(): AnyRefs = JsonStringInput.read[AnyRefs](new String(jsonBytes, UTF_8))
*/
@Benchmark
def readCirce(): AnyRefs = decode[AnyRefs](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

Expand All @@ -38,9 +43,13 @@ class AnyRefsBenchmark extends CommonParams {
@Benchmark
def readPlayJson(): AnyRefs = Json.parse(jsonBytes).as[AnyRefs](anyRefsFormat)

/* FIXME: cannot alter uPickle to store BigDecimal as JSON number
/* FIXME: cannot alter uPickle to store BigDecimal as JSON number, and option field as JSON array
@Benchmark
def readUPickle(): AnyRefs = read[AnyRefs](jsonBytes)
*/
/* FIXME: AVSystem GenCodec store option field as JSON array
@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)
*/
@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.JacksonSerDesers._
Expand Down Expand Up @@ -30,7 +31,12 @@ class ArrayBufferOfBooleansBenchmark extends CommonParams {
}

@Benchmark
def readCirce(): ArrayBuffer[Boolean] = decode[ArrayBuffer[Boolean]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)
def readAVSystemGenCodec(): ArrayBuffer[Boolean] =
JsonStringInput.read[ArrayBuffer[Boolean]](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): ArrayBuffer[Boolean] =
decode[ArrayBuffer[Boolean]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

@Benchmark
def readJacksonScala(): ArrayBuffer[Boolean] = jacksonMapper.readValue[ArrayBuffer[Boolean]](jsonBytes)
Expand All @@ -44,6 +50,9 @@ class ArrayBufferOfBooleansBenchmark extends CommonParams {
@Benchmark
def readUPickle(): ArrayBuffer[Boolean] = read[ArrayBuffer[Boolean]](jsonBytes)

@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)

@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.JacksonSerDesers._
Expand Down Expand Up @@ -34,6 +35,9 @@ class ArrayOfBigDecimalsBenchmark extends CommonParams {
private def obj: Array[BigDecimal] =
sourceObj.map(x => BigDecimal(x.bigDecimal.unscaledValue(), x.bigDecimal.scale()))

@Benchmark
def readAVSystemGenCodec(): Array[BigDecimal] = JsonStringInput.read[Array[BigDecimal]](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): Array[BigDecimal] = decode[Array[BigDecimal]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

Expand All @@ -45,10 +49,13 @@ class ArrayOfBigDecimalsBenchmark extends CommonParams {

@Benchmark
def readPlayJson(): Array[BigDecimal] = Json.parse(jsonBytes).as[Array[BigDecimal]]
/* FIXME: uPickle parses BigDecimal from JSON strings only
/* FIXME: uPickle parses BigDecimal from JSON string only
@Benchmark
def readUPickle(): Array[BigDecimal] = read[Array[BigDecimal]](jsonBytes)
*/
@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)

@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)

Expand All @@ -63,7 +70,7 @@ class ArrayOfBigDecimalsBenchmark extends CommonParams {

@Benchmark
def writePlayJson(): Array[Byte] = Json.toBytes(Json.toJson(obj))
/* FIXME: uPickle serializes longs to JSON strings
/* FIXME: uPickle serializes BigDecimal to JSON string
@Benchmark
def writeUPickle(): Array[Byte] = write(obj).getBytes(UTF_8)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
//import com.github.plokhotnyuk.jsoniter_scala.macros.DslPlatformJson._
import play.api.libs.json.Json
Expand Down Expand Up @@ -30,8 +31,10 @@ class ArrayOfBigIntsBenchmark extends CommonParams {
}

@Benchmark
def readCirce(): Array[BigInt] = decode[Array[BigInt]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)
def readAVSystemGenCodec(): Array[BigInt] = JsonStringInput.read[Array[BigInt]](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): Array[BigInt] = decode[Array[BigInt]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)
/* FIXME: dsl-json cannot find decoder for array of BigInt
@Benchmark
def readDslJsonJava(): Array[BigInt] = decodeDslJson[Array[BigInt]](jsonBytes)
Expand All @@ -48,6 +51,8 @@ class ArrayOfBigIntsBenchmark extends CommonParams {
@Benchmark
def readUPickle(): Array[BigInt] = read[Array[BigInt]](jsonBytes)
*/
@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)
/* FIXME: Circe uses an engineering decimal notation to serialize BigInt
@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.DslPlatformJson._
Expand All @@ -28,6 +29,9 @@ class ArrayOfBooleansBenchmark extends CommonParams {
preallocatedBuf = new Array[Byte](jsonBytes.length + preallocatedOff + 100/*to avoid possible out of bounds error*/)
}

@Benchmark
def readAVSystemGenCodec(): Array[Boolean] = JsonStringInput.read[Array[Boolean]](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): Array[Boolean] = decode[Array[Boolean]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

Expand All @@ -46,6 +50,9 @@ class ArrayOfBooleansBenchmark extends CommonParams {
@Benchmark
def readUPickle(): Array[Boolean] = read[Array[Boolean]](jsonBytes)

@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)

@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

//import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
//import com.github.plokhotnyuk.jsoniter_scala.macros.DslPlatformJson._
Expand All @@ -27,7 +28,10 @@ class ArrayOfBytesBenchmark extends CommonParams {
jsonBytes = jsonString.getBytes(UTF_8)
preallocatedBuf = new Array[Byte](jsonBytes.length + preallocatedOff + 100/*to avoid possible out of bounds error*/)
}

/* FIXME: AVSystem GenCodec expects a string of hexadecimal representation of bytes
@Benchmark
def readAVSystemGenCodec(): Array[Byte] = JsonStringInput.read[Array[Byte]](new String(jsonBytes, UTF_8))
*/
@Benchmark
def readCirce(): Array[Byte] = decode[Array[Byte]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)
/*FIXME:dsl-json expects a base64 string for the byte array
Expand All @@ -45,7 +49,10 @@ class ArrayOfBytesBenchmark extends CommonParams {

@Benchmark
def readUPickle(): Array[Byte] = read[Array[Byte]](jsonBytes)

/* FIXME: AVSystem GenCodec serializes a byte array to a string of hexadecimal representation of bytes
@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)
*/
@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)
/* FIXME:dsl-json serializes a byte array to the base64 string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.JacksonSerDesers._
Expand All @@ -28,6 +29,9 @@ class ArrayOfCharsBenchmark extends CommonParams {
preallocatedBuf = new Array[Byte](jsonBytes.length + preallocatedOff + 100/*to avoid possible out of bounds error*/)
}

@Benchmark
def readAVSystemGenCodec(): Array[Char] = JsonStringInput.read[Array[Char]](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): Array[Char] = decode[Array[Char]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

Expand All @@ -43,6 +47,9 @@ class ArrayOfCharsBenchmark extends CommonParams {
@Benchmark
def readUPickle(): Array[Char] = read[Array[Char]](jsonBytes)

@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)

@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.plokhotnyuk.jsoniter_scala.macros

import java.nio.charset.StandardCharsets._

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.macros.DslPlatformJson._
Expand Down Expand Up @@ -29,6 +30,9 @@ class ArrayOfDoublesBenchmark extends CommonParams {
preallocatedBuf = new Array[Byte](jsonBytes.length + preallocatedOff + 100/*to avoid possible out of bounds error*/)
}

@Benchmark
def readAVSystemGenCodec(): Array[Double] = JsonStringInput.read[Array[Double]](new String(jsonBytes, UTF_8))

@Benchmark
def readCirce(): Array[Double] = decode[Array[Double]](new String(jsonBytes, UTF_8)).fold(throw _, x => x)

Expand All @@ -47,6 +51,9 @@ class ArrayOfDoublesBenchmark extends CommonParams {
@Benchmark
def readUPickle(): Array[Double] = read[Array[Double]](jsonBytes)

@Benchmark
def writeAVSystemGenCodec(): Array[Byte] = JsonStringOutput.write(obj).getBytes(UTF_8)

@Benchmark
def writeCirce(): Array[Byte] = printer.pretty(obj.asJson).getBytes(UTF_8)

Expand All @@ -66,7 +73,6 @@ class ArrayOfDoublesBenchmark extends CommonParams {
@Benchmark
def writePlayJson(): Array[Byte] = Json.toBytes(Json.toJson(obj))
*/

@Benchmark
def writeUPickle(): Array[Byte] = write(obj).getBytes(UTF_8)
}
Loading