/
assertMacro.scala
248 lines (218 loc) · 8.45 KB
/
assertMacro.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
import java.net._
import java.io._
import java.nio.channels.Channels
import scala.annotation.tailrec
import scala.math.pow
def scalaVersion = "2.10"
val scalaTestWithoutMacroVersion = "2.0.M8"
val scalaTestWithMacroVersion = "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()
}
val classFooter = """
}
}"""
def generateSourceFile(testCount: Int, targetDir: File, packageName: String, importStatements: Array[String],
extendsName: String, withNames: Array[String], scopeBracket: Boolean, scopeDef: String,
testDefFun: (Int) => String, testBodyFun: (Int) => String): File = {
targetDir.mkdirs()
val targetFile = new File(targetDir, "ExampleSpec.scala")
val targetOut = new BufferedWriter(new FileWriter(targetFile))
try {
targetOut.write("package " + packageName + "\n\n")
importStatements.foreach { s =>
targetOut.write("import " + s + "\n")
}
targetOut.write("\n")
targetOut.write("class ExampleSpec extends " + extendsName + " " + withNames.map(n => " with " + n).mkString(" ") + " {\n")
targetOut.write(" " + scopeDef + (if (scopeBracket) "{" else "") + " \n")
for (x <- 1 to testCount) {
targetOut.write(" " + testDefFun(x) + " {\n")
targetOut.write(" " + testBodyFun(x) + "\n")
targetOut.write(" }\n")
}
targetOut.write(" " + (if (scopeBracket) "}" else "") + "\n")
targetOut.write("}\n")
}
finally {
targetOut.flush()
targetOut.close()
}
targetFile
}
// Using assert(==)
def assertEqual2TestBodyFun(x: Int): String = "assert(" + x + " + 1 == " + (x+1) + ")"
// Using assert(===)
def assertEqual3TestBodyFun(x: Int): String = "assert(" + x + " + 1 === " + (x+1) + ")"
// Spec
def specTestDefFun(x: Int): String = "def `increment " + x + "`"
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 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 scalatestOldJar = new File("scalatest_" + scalaVersion + "-" + scalaTestWithoutMacroVersion + ".jar")
if (!scalatestOldJar.exists)
downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestWithoutMacroVersion + "/scalatest_" + scalaVersion + "-" + scalaTestWithoutMacroVersion + ".jar", scalatestOldJar)
val scalatestMacroJar = new File("scalatest_" + scalaVersion + "-" + scalaTestWithMacroVersion + ".jar")
if (!scalatestMacroJar.exists)
downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestWithMacroVersion + "/scalatest_" + scalaVersion + "-" + scalaTestWithMacroVersion + ".jar", scalatestMacroJar)
val baseDir = new File("assertMacro")
if (baseDir.exists)
deleteDir(baseDir)
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 baseOutputDir = new File(baseDir, "output")
baseOutputDir.mkdirs()
val baseGeneratedDir = new File(baseDir, "generated")
baseGeneratedDir.mkdirs()
case class Style(name: String, className: String, scopeBracket: Boolean, scopeDef: String, testDefFun: (Int) => String)
case class TestType(name: String, shortName: String, importNames: Array[String], mixinNames: Array[String], testBodyFun: (Int) => String, scalatestJar: File)
val styles =
Array(
Style("scalatest.Spec", "Spec", true, "object `Scala can ` ", specTestDefFun)
)
val testTypes =
Array(
TestType("Old asert(==)", "OldEqual2", Array("org.scalatest._"), Array.empty, assertEqual2TestBodyFun, scalatestOldJar),
TestType("Macro assert(==)", "MacroEqual2", Array("org.scalatest._"), Array.empty, assertEqual2TestBodyFun, scalatestMacroJar),
TestType("Old asert(===)", "OldEqual3", Array("org.scalatest._"), Array.empty, assertEqual3TestBodyFun, scalatestOldJar),
TestType("Macro assert(===)", "MacroEqual3", Array("org.scalatest._"), Array.empty, assertEqual3TestBodyFun, scalatestMacroJar)
)
val testCounts =
Array(
0,
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)
styles.foreach { style =>
testTypes.foreach { testType =>
durationFile.write(testType.shortName)
durationFile.flush()
fileCountFile.write(testType.shortName)
fileCountFile.flush()
fileSizeFile.write(testType.shortName)
fileSizeFile.flush()
try {
testCounts.foreach { testCount =>
println("Working on " + style.name + " " + testType.name + " test count " + testCount + "...")
val outputDir = getOutputDir(baseOutputDir, testCount)
val generatedDir = new File(baseGeneratedDir, "generated-" + testCount)
val generatedSrc = generateSourceFile(
testCount,
new File(generatedDir, style.className + testType.shortName), // target dir
style.className + testType.shortName, // package name
testType.importNames, // imports
style.className, // extends
testType.mixinNames, // mixin
style.scopeBracket, // scope requires bracket or not
style.scopeDef, // scope definition
style.testDefFun,
testType.testBodyFun)
val duration = compile(generatedSrc.getAbsolutePath, testType.scalatestJar.getName, outputDir.getAbsolutePath)
durationFile.write("," + duration)
durationFile.flush()
val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, style.className + testType.shortName))
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()
}
}
}
durationFile.flush()
durationFile.close()
fileCountFile.flush()
fileCountFile.close()
fileSizeFile.flush()
fileSizeFile.close()