Created
September 3, 2015 13:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's what I'm seeing after a few warmup rounds:
And with the circe 0.2.0 snapshot:
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.