Skip to content

Commit c660b41

Browse files
author
Dragos Manolescu
committed
Added result rendering via JFreeChart
1 parent 9fe9e51 commit c660b41

File tree

6 files changed

+152
-10
lines changed

6 files changed

+152
-10
lines changed

build.sbt

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ libraryDependencies ++= Seq (
5454
"nl.grons" %% "metrics-scala" % "2.2.0",
5555
"fr.janalyse" %% "janalyse-jmx" % "0.6.1" % "compile",
5656
"net.sf.jopt-simple" % "jopt-simple" % "4.4",
57-
"play" % "play-json_2.10" % "2.2-SNAPSHOT"
57+
"play" % "play-json_2.10" % "2.2-SNAPSHOT",
58+
"org.jfree" % "jfreechart" % "1.0.14"
5859
)
5960

6061
/* you may need these repos */

data/twitter/tweet.json

-1
This file was deleted.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ case class Experiment(exclude: Set[String], warmUpIterations: Int=5) {
2424
}))
2525
}
2626

27-
println("Parsing measurement (%d warmup, %d iterations)...".format(warmUpIterations, iterations))
27+
println("Parsing measurement (%d warmup, %d iterations on %s)...".format(warmUpIterations, iterations, adaptorsToTest.map(_.getName).mkString(", ")))
2828
run(iterations, adaptorsToTest.toSeq)
2929
}
3030

@@ -37,7 +37,7 @@ case class Experiment(exclude: Set[String], warmUpIterations: Int=5) {
3737
}
3838

3939
val targetAdaptors = adaptorsToTest.filter(_.hasMap)
40-
println("Mapping measurement (%d warmup, %d iterations)...".format(warmUpIterations, iterations))
40+
println("Mapping measurement (%d warmup, %d iterations on %s)...".format(warmUpIterations, iterations, adaptorsToTest.map(_.getName).mkString(", ")))
4141
run(iterations, targetAdaptors)
4242
}
4343

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package com.microWorkflow.jsonScalaPerftest
77

88
import joptsimple._
99
import scala.collection.JavaConversions._
10-
import collection.immutable.HashSet
10+
import scala.collection.immutable.HashSet
11+
import com.microWorkflow.jsonScalaPerftest.output.{ConsoleReporter, ChartReporter}
1112

1213

1314
object Main {
@@ -34,6 +35,12 @@ object Main {
3435
.withValuesSeparatedBy(',')
3536
.describedAs("exclude")
3637
.ofType(classOf[String])
38+
val reportOpt = argParser
39+
.accepts("report", "Result generation, c=console, b=bar chart.")
40+
.withRequiredArg()
41+
.describedAs("Character; default is 'c'")
42+
.ofType(classOf[String])
43+
.defaultsTo("c")
3744

3845
val options = try {
3946
argParser.parse(args: _*)
@@ -57,12 +64,15 @@ object Main {
5764
println("Running %d iterations %s object mapping".format(iterations, if (doMap) "with" else "without"))
5865
val experiment = Experiment(exclude, warmUpIterations)
5966
val ms = if (doMap) experiment.measureMapping(iterations) else experiment.measureParsing(iterations)
60-
for (m <- ms) {
61-
print("Category: %s\n".format(m._1))
62-
for (d <- m._2.iterator)
63-
print("\t dataset '%s', measurement: %s\n".format(d._1, d._2))
64-
}
6567

68+
if (options.has(reportOpt) && options.valueOf[String](reportOpt) == "b") {
69+
val chart = new ChartReporter(if (doMap) "JSON Parsing and Mapping" else "JSON Parsing", ms)
70+
chart.view
71+
System.in.read()
72+
} else {
73+
val reporter = new ConsoleReporter(ms)
74+
reporter.printResults()
75+
}
6676
sys.exit()
6777
}
6878
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.microWorkflow.jsonScalaPerftest.output
2+
3+
import scala.collection.immutable.HashMap
4+
import com.microWorkflow.jsonScalaPerftest.Measurement
5+
import org.jfree.data.category.{CategoryDataset, DefaultCategoryDataset}
6+
import org.jfree.ui.{RefineryUtilities, ApplicationFrame}
7+
import org.jfree.chart.{ChartFactory, JFreeChart, ChartPanel}
8+
import scala.Predef._
9+
import org.jfree.chart.plot.PlotOrientation
10+
import org.jfree.chart.axis.{CategoryLabelPositions, NumberAxis}
11+
import org.jfree.chart.renderer.category.BarRenderer
12+
import java.awt.image.BufferedImage
13+
import java.io.FileOutputStream
14+
import java.awt.geom.Rectangle2D
15+
import com.sun.image.codec.jpeg.JPEGCodec
16+
17+
18+
class ChartReporter(title: String, results: Array[(String, HashMap[String, Measurement])]) extends ApplicationFrame(title) {
19+
20+
import java.awt.Color
21+
import java.awt.Dimension
22+
23+
val graphDataSet = createDataset()
24+
25+
val chart = createChart(graphDataSet)
26+
val chartPanel = new ChartPanel(chart)
27+
chartPanel.setPreferredSize(new Dimension(500, 270))
28+
setContentPane(chartPanel)
29+
30+
def createDataset(): DefaultCategoryDataset = {
31+
val categoryDataSet = new DefaultCategoryDataset()
32+
33+
for ((category, datasets) <- results) {
34+
for ((dataset, measurement) <- datasets) {
35+
categoryDataSet.addValue(measurement.value, measurement.name, dataset)
36+
}
37+
}
38+
categoryDataSet
39+
}
40+
41+
def createChart(dataset: CategoryDataset): JFreeChart = {
42+
43+
// create the chart...
44+
val chart = ChartFactory.createBarChart(
45+
title, // chart title
46+
"JSON Data set", // domain axis label
47+
"Time (ms)", // range axis label
48+
dataset, // data
49+
PlotOrientation.VERTICAL, // orientation
50+
true, // include legend
51+
true, // tooltips?
52+
false // URLs?
53+
)
54+
55+
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
56+
57+
// set the background color for the chart...
58+
chart.setBackgroundPaint(Color.white)
59+
60+
// get a reference to the plot for further customisation...
61+
val plot = chart.getCategoryPlot()
62+
plot.setBackgroundPaint(Color.lightGray)
63+
plot.setDomainGridlinePaint(Color.white)
64+
plot.setRangeGridlinePaint(Color.white)
65+
66+
// set the range axis to display integers only...
67+
val rangeAxis = plot.getRangeAxis().asInstanceOf[NumberAxis]
68+
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits())
69+
70+
// disable bar outlines...
71+
val renderer = plot.getRenderer().asInstanceOf[BarRenderer]
72+
renderer.setDrawBarOutline(false)
73+
renderer.setShadowVisible(false)
74+
75+
val domainAxis = plot.getDomainAxis()
76+
domainAxis.setCategoryLabelPositions(
77+
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
78+
)
79+
chart
80+
}
81+
82+
83+
def view {
84+
this.pack()
85+
RefineryUtilities.centerFrameOnScreen(this)
86+
this.setVisible(true)
87+
}
88+
89+
def draw(chart: JFreeChart, width: Int, height: Int): BufferedImage = {
90+
val img =
91+
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
92+
val g2 = img.createGraphics()
93+
94+
chart.draw(g2, new Rectangle2D.Double(0, 0, width, height))
95+
96+
g2.dispose()
97+
img
98+
}
99+
100+
def saveToFile(
101+
aFileName: String,
102+
width: Int,
103+
height: Int,
104+
quality: Double) {
105+
val img = draw(chart, width, height)
106+
107+
val fos = new FileOutputStream(aFileName)
108+
val encoder2 =
109+
JPEGCodec.createJPEGEncoder(fos)
110+
val param2 = encoder2.getDefaultJPEGEncodeParam(img)
111+
param2.setQuality(quality.toFloat, true)
112+
encoder2.encode(img, param2)
113+
fos.close()
114+
}
115+
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.microWorkflow.jsonScalaPerftest.output
2+
3+
import scala.collection.immutable.HashMap
4+
import com.microWorkflow.jsonScalaPerftest.Measurement
5+
6+
class ConsoleReporter(ms: Array[(String, HashMap[String, Measurement])]) {
7+
8+
def printResults() {
9+
for ((category, measurements) <- ms) {
10+
println("Category: %s".format(category))
11+
for ((dataset, measurement) <- measurements)
12+
print("\t dataset '%s', measurement: %s\n".format(dataset, measurement))
13+
}
14+
15+
}
16+
}

0 commit comments

Comments
 (0)