/
shapelessTables.scala
351 lines (294 loc) · 10.9 KB
/
shapelessTables.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import java.net._
import java.io._
import java.nio.channels.Channels
import scala.annotation.tailrec
def scalaVersion = "2.10"
val scalaTestVersion = "2.0"
def downloadFile(urlString: String, targetFile: File) {
println("Downloading " + urlString)
val url = new URL(urlString)
val connection = url.openConnection
val in = connection.getInputStream
val out = new FileOutputStream(targetFile)
out getChannel() transferFrom(Channels.newChannel(in), 0, Long.MaxValue)
in.close()
out.flush()
out.close()
}
sealed trait Align
object Left extends Align
object Right extends Align
def padValue(value: String, columnWidth:Int, align: Align): String = {
val padCount = columnWidth - value.length
require(padCount >= 0)
align match {
case Left =>
value + (" " * padCount)
case Right =>
(" " * padCount) + value
}
}
val leftColHeader = "\"left\""
val rightColHeader = "\"right\""
val sumColHeader = "\"sum\""
def generateScalaTestSourceFile(testCount: Int, targetDir: File): File = {
targetDir.mkdirs()
val targetFile = new File(targetDir, "ExampleSpec.scala")
val targetOut = new BufferedWriter(new FileWriter(targetFile))
val leftColValueWidth = testCount.toString.length
val sumColValueWidth = (testCount + 1).toString.length
val leftColWidth = List(leftColValueWidth, leftColHeader.length).max
val rightColWidth = rightColHeader.length
val sumColWidth = List(sumColValueWidth, sumColHeader.length).max
try {
targetOut.write("package scalatest\n\n")
targetOut.write("import org.scalatest._\n")
targetOut.write("import prop.TableDrivenPropertyChecks._\n\n")
targetOut.write("class ExampleSpec extends WordSpec with Matchers {\n\n")
targetOut.write(" \"Scala\" can {\n")
targetOut.write(" \"increment integers\" in {\n\n")
targetOut.write(" val examples = \n")
targetOut.write(" Table(\n")
// print headers
targetOut.write(" (" + padValue(leftColHeader, leftColWidth, Left) + ", " + padValue(rightColHeader, rightColWidth, Left) + ", " + padValue(sumColHeader, sumColWidth, Left) + ")" + (if (testCount > 0) ", \n" else "\n"))
// print data rows
for (x <- 1 to testCount) {
targetOut.write(" (" + padValue(x.toString, leftColWidth, Right) + ", " + padValue("1", rightColWidth, Right) + ", " + padValue((x + 1).toString, sumColWidth, Right) + ")" + (if (x < testCount) ", \n" else "\n"))
}
targetOut.write(" )\n\n")
targetOut.write(" }\n")
targetOut.write(" }\n")
targetOut.write("}\n")
}
finally {
targetOut.flush()
targetOut.close()
}
targetFile
}
def generateShapelessSourceFile(testCount: Int, targetDir: File): File = {
targetDir.mkdirs()
val targetFile = new File(targetDir, "ExampleSpec.scala")
val targetOut = new BufferedWriter(new FileWriter(targetFile))
val leftColValueWidth = testCount.toString.length
val sumColValueWidth = (testCount + 1).toString.length
val leftColWidth = List(leftColValueWidth, leftColHeader.length).max
val rightColWidth = rightColHeader.length
val sumColWidth = List(sumColValueWidth, sumColHeader.length).max
try {
targetOut.write("package shapelessTables\n\n")
targetOut.write("import org.scalatest._\n")
targetOut.write("import shapelessTable._\n\n")
targetOut.write("class ExampleSpec extends WordSpec with Matchers {\n\n")
targetOut.write(" \"Scala\" can {\n")
targetOut.write(" \"increment integers\" in {\n\n")
targetOut.write(" val examples = \n")
targetOut.write(" Table(\n")
// print headers
targetOut.write(" (" + padValue(leftColHeader, leftColWidth, Left) + ", " + padValue(rightColHeader, rightColWidth, Left) + ", " + padValue(sumColHeader, sumColWidth, Left) + ")" + (if (testCount > 0) ", \n" else "\n"))
// print data rows
for (x <- 1 to testCount) {
targetOut.write(" (" + padValue(x.toString, leftColWidth, Right) + ", " + padValue("1", rightColWidth, Right) + ", " + padValue((x + 1).toString, sumColWidth, Right) + ")" + (if (x < testCount) ", \n" else "\n"))
}
targetOut.write(" )\n\n")
targetOut.write(" }\n")
targetOut.write(" }\n")
targetOut.write("}\n")
}
finally {
targetOut.flush()
targetOut.close()
}
targetFile
}
def compile(srcFile: String, classpath: String, targetDir: String) = {
import scala.collection.JavaConversions._
val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile)
val builder = new ProcessBuilder(command)
builder.redirectErrorStream(true)
val start = System.currentTimeMillis
val process = builder.start()
val stdout = new BufferedReader(new InputStreamReader(process.getInputStream))
var line = "Compiling " + srcFile + "..."
while (line != null) {
println (line)
line = stdout.readLine
}
val end = System.currentTimeMillis
end - start
}
def jar(jarFileName: String, classesDir: String) = {
import scala.collection.JavaConversions._
val command = List("jar", "cf", jarFileName, "-C", classesDir, ".")
val builder = new ProcessBuilder(command)
builder.redirectErrorStream(true)
val start = System.currentTimeMillis
val process = builder.start()
val stdout = new BufferedReader(new InputStreamReader(process.getInputStream))
var line = "Creating jar file " + jarFileName + "..."
while (line != null) {
println (line)
line = stdout.readLine
}
val end = System.currentTimeMillis
end - start
}
def getFileAndByteCount(srcDir: File) = {
@tailrec
def getFileAndByteCountAcc(dirList: Array[File], fileCount: Long, byteCount: Long): Tuple2[Long, Long] = {
val (files, subDirs) = dirList.partition(_.isFile)
val classFiles = files.filter(f => f.getName.endsWith(".class"))
val newFileCount = fileCount + classFiles.size
val newByteCount = byteCount + classFiles.map { f => f.length.toLong }.foldLeft(0l) { (a, b) => a + b }
if (subDirs.isEmpty)
(newFileCount, newByteCount)
else
getFileAndByteCountAcc(subDirs.flatMap(d => d.listFiles), newFileCount, newByteCount)
}
getFileAndByteCountAcc(srcDir.listFiles, 0l, 0l)
}
def deleteDir(targetDir: File) {
val children = targetDir.listFiles
if (children != null) {
targetDir.listFiles.foreach { child =>
if (child.isFile)
child.delete()
else
deleteDir(child)
}
targetDir.delete()
}
else
println("Unable to list files in " + targetDir.getAbsolutePath)
}
def getOutputDir(baseOutputDir: File, testCount: Int): File = {
val outputDirName = "output-" + testCount
val outputDir = new File(baseOutputDir, outputDirName)
outputDir.mkdirs()
outputDir
}
val scalatestJar = new File("scalatest_" + scalaVersion + "-" + scalaTestVersion + ".jar")
if (!scalatestJar.exists)
downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestVersion + "/scalatest_" + scalaVersion + "-" + scalaTestVersion + ".jar", scalatestJar)
val shapelessJar = new File("shapeless_2.10.2-2.0.0-SNAPSHOT.jar")
if (!shapelessJar.exists)
downloadFile("http://oss.sonatype.org/content/repositories/snapshots/com/chuusai/shapeless_2.10.2/2.0.0-SNAPSHOT/shapeless_2.10.2-2.0.0-SNAPSHOT.jar", shapelessJar)
val baseDir = new File("shapelessTables")
if (baseDir.exists)
deleteDir(baseDir)
baseDir.mkdirs()
val shapelessTableJar = new File("shapeless-table.jar")
if (!shapelessTableJar.exists) {
val shapelessTableSource = new File("ShapelessTable.scala")
val shapelessTableClassDir = new File(baseDir, "shapeless-table")
shapelessTableClassDir.mkdirs()
compile(shapelessTableSource.getAbsolutePath, shapelessJar.getName, shapelessTableClassDir.getAbsolutePath)
jar(shapelessTableJar.getName, shapelessTableClassDir.getAbsolutePath)
}
val statDir = new File(baseDir, "stat")
statDir.mkdirs()
val durationFile = new FileWriter(new File(statDir, "duration.csv"))
val fileCountFile = new FileWriter(new File(statDir, "filecount.csv"))
val fileSizeFile = new FileWriter(new File(statDir, "filesize.csv"))
val scalaTestClasspath = scalatestJar.getName
val shapelessClasspath = scalatestJar.getName + File.pathSeparator + shapelessJar.getName + File.pathSeparator + shapelessTableJar.getName
val baseOutputDir = new File(baseDir, "output")
baseOutputDir.mkdirs()
val baseGeneratedDir = new File(baseDir, "generated")
baseGeneratedDir.mkdirs()
val testCounts =
Array(
10,
20,
30,
40,
50,
60,
70,
80,
90,
100,
200,
300,
400,
500,
600,
700,
800,
900,
1000
)
val headers = "TestCount," + testCounts.mkString(",") + "\n"
durationFile.write(headers)
fileCountFile.write(headers)
fileSizeFile.write(headers)
// ScalaTest Table
durationFile.write("scalatest")
durationFile.flush()
fileCountFile.write("scalatest")
fileCountFile.flush()
fileSizeFile.write("scalatest")
fileSizeFile.flush()
try {
testCounts.foreach { testCount =>
println("Working on ScalaTest test count " + testCount + "...")
val outputDir = getOutputDir(baseOutputDir, testCount)
val generatedDir = new File(baseGeneratedDir, "generated-" + testCount)
val generatedSrc = generateScalaTestSourceFile(testCount, new File(generatedDir, "scalatest"))
val duration = compile(generatedSrc.getAbsolutePath, scalaTestClasspath, outputDir.getAbsolutePath)
durationFile.write("," + duration)
durationFile.flush()
val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, "scalatest"))
fileCountFile.write("," + fileCount)
fileCountFile.flush()
fileSizeFile.write("," + fileSize)
fileSizeFile.flush()
}
}
catch {
case e: Throwable =>
e.printStackTrace()
}
finally {
durationFile.write("\n")
durationFile.flush()
fileCountFile.write("\n")
fileCountFile.flush()
fileSizeFile.write("\n")
fileSizeFile.flush()
}
// Shapeless Table
durationFile.write("shapeless")
durationFile.flush()
fileCountFile.write("shapeless")
fileCountFile.flush()
fileSizeFile.write("shapeless")
fileSizeFile.flush()
try {
testCounts.foreach { testCount =>
println("Working on Shapeless test count " + testCount + "...")
val outputDir = getOutputDir(baseOutputDir, testCount)
val generatedDir = new File(baseGeneratedDir, "generated-" + testCount)
val generatedSrc = generateShapelessSourceFile(testCount, new File(generatedDir, "shapelessTables"))
val duration = compile(generatedSrc.getAbsolutePath, shapelessClasspath, outputDir.getAbsolutePath)
durationFile.write("," + duration)
durationFile.flush()
val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, "shapelessTables"))
fileCountFile.write("," + fileCount)
fileCountFile.flush()
fileSizeFile.write("," + fileSize)
fileSizeFile.flush()
}
}
catch {
case e: Throwable =>
e.printStackTrace()
}
finally {
durationFile.write("\n")
durationFile.flush()
fileCountFile.write("\n")
fileCountFile.flush()
fileSizeFile.write("\n")
fileSizeFile.flush()
}