/
HtmlReporter.scala
176 lines (147 loc) · 4.44 KB
/
HtmlReporter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.scalameter
package reporting
import java.io._
import org.scalameter.Key._
import org.scalameter.utils.Tree
import scala.collection._
import scala.util.parsing.json.{JSONObject, JSONArray}
case class HtmlReporter[T: Numeric](embedDsv: Boolean = true) extends Reporter[T] {
import HtmlReporter._
def report(result: CurveData[T], persistor: Persistor) {
// nothing - the charts are generated only at the end
}
def report(results: Tree[CurveData[T]], persistor: Persistor) = {
if (!embedDsv) {
new DsvReporter(dsvDelimiter).report(results, persistor)
}
// resultDir is global setting
val resultdir = currentContext(reports.resultDir)
val root = new File(resultdir, "report")
root.mkdirs()
val curvesJSONIndex = JSONIndex(results)
resourceDirs.foreach {
new File(root, _).mkdirs()
}
resourceFiles.foreach { filename =>
copyResource(filename, new File(root, filename))
}
printToFile(new File(root, jsDataFile)) { pw =>
pw.println("var ScalaMeter = (function(parent) {")
pw.println(" var my = { name: \"data\" };")
pw.println(s" my.index = $curvesJSONIndex;")
if (embedDsv) {
printTsv(results, persistor, pw)
}
pw.println(" parent[my.name] = my;")
pw.println(" return parent;")
pw.println("})(ScalaMeter || {});")
}
true
}
def machineInformation =
<div>
<h1>Machine information</h1>
<ul>
{
for ((k, v) <- Context.machine.properties.toList.sortBy(_._1)) yield <li>
{k + ": " + v}
</li>
}
</ul>
</div>
def date(results: Tree[CurveData[T]]) = {
val dateoption = for {
start <- results.context(reports.startDate)
end <- results.context(reports.endDate)
} yield <div>
<div>Started: {start}</div>
<div>Finished: {end}</div>
<div>Running time: {(end.getTime - start.getTime) / 1000} seconds</div>
</div>
dateoption.getOrElse(<div>No date information.</div>)
}
def JSONIndex(results: Tree[CurveData[T]]) = {
def JSONCurve(context: Context, curve: CurveData[T]) = JSONObject(
immutable.Map(
"scope" -> new JSONArray(curve.context.scopeList),
"name" -> curve.context.curve,
"file" -> s"../${context.scope}.${curve.context.curve}.dsv"
)
)
val JSONCurves = for {
(ctx, curves) <- results.scopes if curves.nonEmpty
curve <- curves
} yield {
JSONCurve(ctx, curve)
}
new JSONArray(JSONCurves.toList)
}
def printTsv(results: Tree[CurveData[T]], persistor: Persistor, pw: PrintWriter) {
val allCurves = for {
(ctx, curves) <- results.scopes if curves.nonEmpty
curve <- curves
} yield curve
val separators = "" #:: Stream.continually(", ")
pw.print(" my.tsvData = [")
for ((curve, sep) <- allCurves.toStream zip separators) {
pw.print(sep)
pw.print("'")
DsvReporter.writeCurveData(curve, persistor, pw, dsvDelimiter, "\\n")
pw.print("'")
}
pw.println("];")
}
}
object HtmlReporter {
val resourceDirs = List("css", "img", "js", "js/ScalaMeter")
val resourceFiles = List(
"index.html",
"css/bootstrap.min.css",
"css/bootstrap-slider.css",
"css/icons.gif",
"css/index.css",
"css/jquery-ui-1.10.3.custom.css",
"css/ui.dynatree.css",
"css/vline.gif",
"img/arrow.png",
"img/glyphicons-halflings.png",
"js/bootstrap.min.js",
"js/crossfilter.min.js",
"js/d3.v3.min.js",
"js/jquery.dynatree.js",
"js/jquery-1.9.1.js",
"js/jquery-compat.js",
"js/jquery-ui-1.10.3.custom.min.js",
"js/ScalaMeter/chart.js",
"js/ScalaMeter/dimensions.js",
"js/ScalaMeter/filter.js",
"js/ScalaMeter/helper.js",
"js/ScalaMeter/main.js",
"js/ScalaMeter/permalink.js" )
val jsDataFile = "js/ScalaMeter/data.js"
val dsvDelimiter = '\t'
def copyResource(from: String, to: File) {
val res = getClass.getClassLoader.getResourceAsStream(from)
try {
val buffer = new Array[Byte](1024)
val fos = new FileOutputStream(to)
var nBytesRead = 0
def read() = {
nBytesRead = res.read(buffer)
nBytesRead != -1
}
while (read()) {
fos.write(buffer, 0, nBytesRead)
}
if (fos != null) {
fos.close()
}
} finally {
res.close()
}
}
def printToFile(f: File)(op: PrintWriter => Unit) {
val p = new PrintWriter(f)
try { op(p) } finally { p.close() }
}
}