Skip to content

Instantly share code, notes, and snippets.

@crakjie
Created September 3, 2015 13:06
Show Gist options
  • Save crakjie/a256baf41d94bfe59d0b to your computer and use it in GitHub Desktop.
Save crakjie/a256baf41d94bfe59d0b to your computer and use it in GitHub Desktop.
A simple test to know if there is a performence pb in circe
package perf
import play.api.libs.json.Json
import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
import io.circe._
import io.circe.generic.auto._
import io.circe.jawn._
import io.circe.syntax._
//use 0.1.1 circe
//use play 2.3.8
case class IP(ip : Option[String], remote_ip : String)
object IP {
implicit val formatOrder = play.api.libs.json.Json.format[IP]
}
object Test {
def time[T](f: => T, message: String = "" ): T = {
val a = System.nanoTime()
val r = f
val b = System.nanoTime()
println(message + " elapsed time :"+ (b - a) / 1000000.0 + "ms" + Console.RESET )
r
}
def run = {
import Console._
val v = IP(Some("1"),"2")
val v2 = IP(Some("3"),"4")
time(v.asJson,BLUE + "circe") //look like init something it's very long 200ms
time(v.asJson,BLUE + "circe") // avg 6-8 ms on my computer
time(v.asJson,BLUE + "circe") // idem
time(v.asJson.toString,BLUE + "circe try to avoid lazy things") //little more
time(play.api.libs.json.Json.toJson(v),GREEN + "play") //look like init something it's very long 60ms
time(play.api.libs.json.Json.toJson(v),GREEN + "play") //avg 0.1ms
time(play.api.libs.json.Json.toJson(v),GREEN + "play") //0.1 ms
time(play.api.libs.json.Json.toJson(v).toString,GREEN + "play try to avoid lazy things") // little more
println("second object")
time(v2.asJson,BLUE + "circe") //avg 6-8 ms on my computer
time(v2.asJson,BLUE + "circe") // avg 6-8 ms on my computer
time(v2.asJson,BLUE + "circe") // idem
time(v2.asJson.toString,BLUE + "circe try to avoid lazy things") // idem
time(play.api.libs.json.Json.toJson(v2),GREEN + "play") //avg 0.1ms
time(play.api.libs.json.Json.toJson(v2),GREEN + "play") //avg 0.1ms
time(play.api.libs.json.Json.toJson(v2).toString,GREEN + "play try to avoid lazy things") //0.1 ms
}
}
/*
Thing you maybe want to put into your build.sbt
name := "CircePoc"
version := "1.0"
scalaVersion := "2.11.7"
resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies ++= Seq(
"io.circe" %% "circe-core" % "0.1.1",
"io.circe" %% "circe-generic" % "0.1.1",
"io.circe" %% "circe-jawn" % "0.1.1",
"com.typesafe.play" %% "play-json" % "2.3.4"
)
*/
@travisbrown
Copy link

Here's what I'm seeing after a few warmup rounds:

circe elapsed time :0.062ms
circe elapsed time :0.045ms
circe elapsed time :0.04ms
circe try to avoid lazy things elapsed time :0.086ms
play elapsed time :0.122ms
play elapsed time :0.08ms
play elapsed time :0.074ms
play try to avoid lazy things elapsed time :0.124ms
second object
circe elapsed time :0.036ms
circe elapsed time :0.03ms
circe elapsed time :0.032ms
circe try to avoid lazy things elapsed time :0.074ms
play elapsed time :0.079ms
play elapsed time :0.071ms
play  try to avoid lazy things elapsed time :0.117ms
res13: String = {"ip":"3","remote_ip":"4"}

And with the circe 0.2.0 snapshot:

scala> perf.Test.run
circe elapsed time :0.059ms
circe elapsed time :0.034ms
circe elapsed time :0.038ms
circe try to avoid lazy things elapsed time :0.102ms
play elapsed time :0.145ms
play elapsed time :0.08ms
play elapsed time :0.092ms
play try to avoid lazy things elapsed time :0.109ms
second object
circe elapsed time :0.035ms
circe elapsed time :0.032ms
circe elapsed time :0.032ms
circe try to avoid lazy things elapsed time :0.06ms
play elapsed time :0.061ms
play elapsed time :0.057ms
play  try to avoid lazy things elapsed time :0.078ms
res13: String = {"ip":"3","remote_ip":"4"}

So circe generally looks a little faster in both cases, but you'd want to use a more sophisticated approach to timing to take that as meaningful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment