Created
September 14, 2013 17:41
-
-
Save anonymous/6563971 to your computer and use it in GitHub Desktop.
early Json benchmark for Parboiled2
This file contains 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
/** | |
* Following libs are required to compile and run the benchmark: | |
* - jackson-core-asl-1.0.0.jar | |
* - jackson-mapper-asl-1.0.0.jar | |
* - lift-json-???.jar | |
*/ | |
package org.parboiled2.examples.v2 | |
object Jsonbench { | |
import scala.util.parsing.json._ | |
import org.codehaus.jackson._ | |
import org.codehaus.jackson.map._ | |
import net.liftweb.json.JsonParser | |
import spray.json._ | |
import DefaultJsonProtocol._ | |
def main(args: Array[String]) = { | |
val mapper = new ObjectMapper | |
benchmark("Jackson") { mapper.readValue(oneLineJson, classOf[JsonNode]) } | |
benchmark("Lift Json") { JsonParser.parse(oneLineJson) } | |
benchmark("Parboiled2") { | |
val jsonSimpleParser = new JsonSimpleParser(oneLineJson) | |
jsonSimpleParser.run(_.InputLine) | |
} | |
benchmark("Parboiled") { JSON.parseRaw(oneLineJson) } | |
benchmark("Scala std") { JSON.parseRaw(json) } | |
} | |
def benchmark(name: String)(f: => Any) = { | |
println("warmup") | |
(1 to 50000).foreach(i => f) | |
println("warmup done") | |
val t = time { | |
(1 to 50000).foreach(i => f) | |
} | |
println(name + "\t" + t + "ms") | |
} | |
def time(f: => Any): Long = { | |
val start = System.currentTimeMillis | |
f | |
System.currentTimeMillis - start | |
} | |
val oneLineJson = """{"glossary":{"title":"exampleglossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"StandardGeneralizedMarkupLanguage","Acronym":"SGML","Abbrev":"ISO8879","GlossDef":{"para":"Ametamarkuplanguage","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}""" | |
val json = """ | |
{ | |
"glossary": { | |
"title": "example glossary", | |
"GlossDiv": { | |
"title": "S", | |
"GlossList": { | |
"GlossEntry": { | |
"ID": "SGML", | |
"SortAs": "SGML", | |
"GlossTerm": "Standard Generalized Markup Language", | |
"Acronym": "SGML", | |
"Abbrev": "ISO 8879:1986", | |
"GlossDef": { | |
"para": "A meta-markup language, used to create markup languages such as DocBook.", | |
"GlossSeeAlso": ["GML", "XML"] | |
}, | |
"GlossSee": "markup" | |
} | |
} | |
} | |
} | |
} | |
""" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment