Skip to content

Commit 46b8f7b

Browse files
author
Dragos Manolescu
committed
Added map filtering and command-line options.
1 parent d02bb30 commit 46b8f7b

File tree

12 files changed

+86
-251
lines changed

12 files changed

+86
-251
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ libraryDependencies ++= Seq (
4646
"com.persist" % "persist-json_2.9.2" % "0.9-RC1",
4747
"com.codahale" % "jerkson_2.9.1" % "0.5.0",
4848
"net.liftweb" % "lift-json_2.9.1" % "2.4",
49-
"net.minidev" % "json-smart" % "2.0-RC2",
49+
"net.minidev" % "json-smart" % "1.1.1",
5050
"com.rojoma" %% "rojoma-json" % "2.0.0",
5151
"io.spray" %% "spray-json" % "1.2.3" cross CrossVersion.full,
5252
"com.yammer.metrics" % "metrics-core" % "2.1.3",

src/main/scala/com/microWorkflow/jsonScalaPerftest/LibraryAdaptor.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ abstract class LibraryAdaptor(name: String) extends TimeMeasurements {
1616

1717
def getName = name
1818

19+
def hasMap: Boolean
20+
1921
def initialize()
2022

2123
def runOnce(json: String, doMap:Boolean): Any

src/main/scala/com/microWorkflow/jsonScalaPerftest/Main.scala

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ case class Category(directoryName: String, name: String) {
4242
sb.toString()
4343
}
4444

45-
def measure(adapter: LibraryAdapter, doMap: Boolean) {
45+
def measure(adapter: LibraryAdaptor, doMap: Boolean) {
4646
for (dataset <- datasets) {
4747
adapter.measure(dataset, doMap)
4848
}
@@ -59,15 +59,15 @@ object Category {
5959

6060
case class Experiment(measurementsPath: String) {
6161

62-
val adapters = Array(new liftjson.LiftJsonAdapter("lift")
63-
, new jerkson.JerksonAdapter("jerkson")
64-
, new jsonsmart.JsonSmartAdapter("JsonSmart")
65-
, new spray.SprayAdapter("spray")
66-
, new persist.PersistAdapter("persist")
67-
, new twitter.TwitterAdapter("twitter")
68-
, new socrata.SocrataAdapter("socrata")
69-
, new scalalib.ScalaLibAdapter("scalalib")
70-
, new jackson.JacksonAdapter("jackson")
62+
val adapters = Array(new liftjson.LiftJsonAdaptor("lift")
63+
, new jerkson.JerksonAdaptor("jerkson")
64+
, new jsonsmart.JsonSmartAdaptor("JsonSmart")
65+
, new spray.SprayAdaptor("spray")
66+
, new persist.PersistAdaptor("persist")
67+
, new twitter.TwitterAdaptor("twitter")
68+
, new socrata.SocrataAdaptor("socrata")
69+
, new scalalib.ScalaLibAdaptor("scalalib")
70+
, new jackson.JacksonAdaptor("jackson")
7171
)
7272

7373
val loopCounter = Metrics.newCounter(this.getClass, "main loop")
@@ -77,28 +77,26 @@ case class Experiment(measurementsPath: String) {
7777
def run(iterations: Int, doMap: Boolean) = {
7878
loopCounter.clear()
7979
val oldFiles = Category.getFilesMatching(measurementsPath, f => f.isFile)
80+
println("Removing %d old files".format(oldFiles.length))
8081
if (oldFiles != null)
8182
oldFiles.foreach(f => f.delete())
8283
CsvReporter.enable(new File(measurementsPath), 100, TimeUnit.MILLISECONDS)
84+
val adaptersToTest = if (doMap) adapters.filter(_.hasMap) else adapters
8385
for (count <- 1 to iterations) {
84-
categories.flatMap(c => (adapters map {
86+
categories.flatMap(c => (adaptersToTest.map {
8587
each => c.measure(each, doMap)
8688
}))
8789
loopCounter.inc()
8890
}
8991
}
9092

91-
def takeMeasurements(iterations: Int) {
93+
def takeMeasurements(iterations: Int, doMap: Boolean) {
9294
val where = new File(measurementsPath)
9395
where.mkdirs()
9496
print("Priming caches...")
95-
run(iterations, true)
96-
print("Measuring timings w/o mapping...")
97-
run(iterations, false)
98-
Category.getFilesMatching(measurementsPath, f => f.isFile && f.getName.contains(".csv".toCharArray))
99-
.foreach(f => f.renameTo(new File("nomap" + f.getName)))
100-
print("Measuring timings w/ mapping...")
101-
run(iterations, true)
97+
run(iterations, doMap)
98+
print("Measuring timings...")
99+
run(iterations, doMap)
102100
}
103101

104102
def printMeanValues() {
@@ -118,8 +116,16 @@ case class Experiment(measurementsPath: String) {
118116
}
119117

120118

121-
object Main extends App {
122-
val e = Experiment("/tmp/measurements1")
123-
e.takeMeasurements(10)
124-
println("Done")
119+
object Main {
120+
121+
def main(args: Array[String]) {
122+
val where = if (args.length>0) args(0) else "/tmp/measurements"
123+
val iterations:Int = if (args.length>1) args(1).toInt else 100
124+
val doMap = if (args.length>2) args(2).equalsIgnoreCase("-map") else false
125+
println("Runing %d iterations %s object mapping; test results in %s"
126+
.format(iterations, if (doMap) "with" else "without", where))
127+
val e = Experiment(where)
128+
e.takeMeasurements(iterations, doMap)
129+
println("Done")
130+
}
125131
}

src/main/scala/com/microWorkflow/jsonScalaPerftest/jackson/JacksonAdaptor.scala

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.microWorkflow.jsonScalaPerftest.jackson
22

3-
import com.persist.JsonOps._
4-
import com.persist.JsonMapper._
5-
import com.microWorkflow.jsonScalaPerftest.{LibraryAdaptor, TimeMeasurements}
3+
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
64
import org.codehaus.jackson.map.ObjectMapper
75
import org.codehaus.jackson.map.DeserializationConfig
86

@@ -14,14 +12,6 @@ import org.codehaus.jackson.map.DeserializationConfig
1412
* To change this template use File | Settings | File Templates.
1513
*/
1614

17-
/*
18-
case class Url(indices: Seq[Int], url: String)
19-
case class Hashtag(indices: Seq[Int], text: String)
20-
case class UserMention(indices: Seq[Int], name: String)
21-
case class Entities(hashtags: Seq[Hashtag], urls: Seq[Url], user_mentions: Seq[UserMention])
22-
case class Tweet(id_str: String, text: String, entities: Entities)
23-
*/
24-
2515
class Url {
2616
private var indices:Array[Int] = _
2717
private var url:String = _
@@ -70,9 +60,9 @@ class Tweet {
7060
def setEntities(e:Entities) { entities = e}
7161
}
7262

73-
class JacksonAdaptor(name: String) extends LibraryAdapter(name) {
63+
class JacksonAdaptor(name: String) extends LibraryAdaptor(name) {
7464

75-
var m:ObjectMapper = null
65+
var m:ObjectMapper = _
7666

7767
override def initialize() {
7868
m = new ObjectMapper()
@@ -83,18 +73,11 @@ class JacksonAdaptor(name: String) extends LibraryAdapter(name) {
8373
if (doMap) {
8474
val rootNode:Tweet = m.readValue(json, classOf[Tweet])
8575
rootNode
86-
8776
} else {
8877
val rootNode = m.readTree(json)
8978
rootNode
9079
}
91-
/*
92-
Json(json) match {
93-
case obj: JsonObject => if (doMap) ToObject[Tweet](obj)
94-
case array: JsonArray => //array.extract[List[Tweet]]
95-
case _ => List[Tweet]()
96-
}
97-
*/
9880
}
9981

82+
override def hasMap = true
10083
}

src/main/scala/com/microWorkflow/jsonScalaPerftest/jerkson/JerksonAdaptor.scala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.microWorkflow.jsonScalaPerftest.jerkson
22

33
import com.codahale.jerkson.JsonSnakeCase
4-
import org.codehaus.jackson.map.ObjectMapper
54
import com.codahale.jerkson.Json._
6-
import com.microWorkflow.jsonScalaPerftest.{LibraryAdaptor, TimeMeasurements}
5+
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
76
import com.codahale.jerkson.AST.JValue
87

98
/**
@@ -35,26 +34,20 @@ case class Entities(hashtags: Array[Hashtag], urls: Array[Url], userMentions: Ar
3534
case class Tweet(idStr: String, text: String, entities: Entities)
3635

3736

38-
class JerksonAdaptor(name: String) extends LibraryAdapter(name) {
39-
var mapper: ObjectMapper = _
37+
class JerksonAdaptor(name: String) extends LibraryAdaptor(name) {
4038

4139
override def initialize() {
42-
//mapper = new ObjectMapper()
40+
/* nop */
4341
}
4442

4543
override def runOnce(json: String, doMap:Boolean) = {
46-
try {
47-
if (doMap) {
48-
parse[Tweet](json)
49-
} else {
50-
parse[JValue](json)
51-
}
52-
} catch {
53-
case pe: com.codahale.jerkson.ParsingException =>
54-
null
55-
case iae: java.lang.IllegalArgumentException =>
56-
null
57-
}
44+
if (doMap) {
45+
parse[Tweet](json)
46+
} else {
47+
parse[JValue](json)
48+
}
5849
}
5950

51+
override def hasMap = true
52+
6053
}

src/main/scala/com/microWorkflow/jsonScalaPerftest/jsonsmart/JsonSmartAdaptor.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
44
import net.minidev.json.JSONValue.parse
55

66
case class User ( utc_offset: Int
7-
, time_zone: String
8-
)
7+
, time_zone: String
8+
)
99

1010
case class Url(indices: Array[Int], url: String)
1111

@@ -25,9 +25,11 @@ class JsonSmartAdaptor(name: String) extends LibraryAdaptor(name) {
2525

2626
override def runOnce(json: String, doMap:Boolean) = {
2727
if (doMap)
28-
parse(json /* , classOf[Tweet] */)
28+
parse(json /* , classOf[Tweet] */) // TODO: add map (JsonSmart v.2)
2929
else
3030
parse(json)
3131
}
3232

33+
override def hasMap = false
34+
3335
}
Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.microWorkflow.jsonScalaPerftest.liftjson
22

33
import net.liftweb.json._
4-
import com.microWorkflow.jsonScalaPerftest.{LibraryAdaptor, TimeMeasurements}
4+
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
55

66
/**
77
* Created with IntelliJ IDEA.
@@ -11,92 +11,13 @@ import com.microWorkflow.jsonScalaPerftest.{LibraryAdaptor, TimeMeasurements}
1111
* To change this template use File | Settings | File Templates.
1212
*/
1313

14-
/*
15-
{
16-
"contributors": null,
17-
"coordinates": null,
18-
"created_at": "Mon Jun 27 21:45:46 +0000 2011",
19-
"entities": {
20-
"hashtags": [],
21-
"urls": [
22-
{
23-
"display_url": "mercynotes.com",
24-
"expanded_url": "http://www.mercynotes.com/",
25-
"indices": [
26-
61,
27-
80
28-
],
29-
"url": "http://t.co/lKzLFOd"
30-
}
31-
],
32-
"user_mentions": []
33-
},
34-
"favorited": false,
35-
"geo": null,
36-
"id": 85463859615379456,
37-
"id_str": "85463859615379456",
38-
"in_reply_to_screen_name": null,
39-
"in_reply_to_status_id": null,
40-
"in_reply_to_status_id_str": null,
41-
"in_reply_to_user_id": null,
42-
"in_reply_to_user_id_str": null,
43-
"place": null,
44-
"retweet_count": 0,
45-
"retweeted": false,
46-
"source": "web",
47-
"text": "Been watching Wimbledon? Check out new post Love and Tennis: http://t.co/lKzLFOd",
48-
"truncated": false,
49-
"user": {
50-
"contributors_enabled": false,
51-
"created_at": "Mon May 30 16:35:44 +0000 2011",
52-
"default_profile": true,
53-
"default_profile_image": false,
54-
"description": "",
55-
"favourites_count": 0,
56-
"follow_request_sent": null,
57-
"followers_count": 6,
58-
"following": null,
59-
"friends_count": 12,
60-
"geo_enabled": false,
61-
"id": 307978890,
62-
"id_str": "307978890",
63-
"is_translator": false,
64-
"lang": "en",
65-
"listed_count": 0,
66-
"location": "NC",
67-
"name": "Julie LaJoe",
68-
"notifications": null,
69-
"profile_background_color": "C0DEED",
70-
"profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png",
71-
"profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme1/bg.png",
72-
"profile_background_tile": false,
73-
"profile_image_url": "http://a0.twimg.com/profile_images/1375001769/JulieMNnew__2__normal.jpg",
74-
"profile_image_url_https": "https://si0.twimg.com/profile_images/1375001769/JulieMNnew__2__normal.jpg",
75-
"profile_link_color": "0084B4",
76-
"profile_sidebar_border_color": "C0DEED",
77-
"profile_sidebar_fill_color": "DDEEF6",
78-
"profile_text_color": "333333",
79-
"profile_use_background_image": true,
80-
"protected": false,
81-
"screen_name": "mercynotes",
82-
"show_all_inline_media": false,
83-
"statuses_count": 13,
84-
"time_zone": "Quito",
85-
"url": "http://mercynotes.com",
86-
"utc_offset": -18000,
87-
"verified": false
88-
}
89-
}
90-
91-
*/
92-
9314
case class Url(indices: Array[Int], url: String)
9415
case class Hashtag(indices: Array[Int], text: String)
9516
case class UserMention(indices: Array[Int], name: String)
9617
case class Entities(hashtags: Array[Hashtag], urls: Array[Url], userMentions: Array[UserMention])
9718
case class Tweet(id_str: String, text: String, entities: Entities)
9819

99-
class LiftJsonAdaptor(name: String) extends LibraryAdapter(name) {
20+
class LiftJsonAdaptor(name: String) extends LibraryAdaptor(name) {
10021

10122
override def initialize() { /* nop */ }
10223

@@ -120,4 +41,6 @@ class LiftJsonAdaptor(name: String) extends LibraryAdapter(name) {
12041
case _ => List[Tweet]()
12142
}
12243
}
44+
45+
override def hasMap = true
12346
}

0 commit comments

Comments
 (0)