diff --git a/allClassTestsInOneFile.scala b/allClassTestsInOneFile.scala index 840f764..6a09b2a 100644 --- a/allClassTestsInOneFile.scala +++ b/allClassTestsInOneFile.scala @@ -4,19 +4,21 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/* -def scalaVersion = { +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt } -*/ -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val scalaTestVersion = "2.1.6" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -149,7 +151,7 @@ class ExampleSpec extends SpecificationClass { def is =""" + "\n" + def compile(srcFile: String, classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile) + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -241,7 +243,7 @@ if (scalaVersion != "unknown") { if (!specs2ScalazJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-core_" + scalaVersion + "/" + scalazVersion + "/scalaz-core_" + scalaVersion + "-" + scalazVersion + ".jar", specs2ScalazJar) - val baseDir = new File("allClassTestsInOneFile") + val baseDir = new File("target/" + scalaVersion + "/allClassTestsInOneFile") if (baseDir.exists) deleteDir(baseDir) diff --git a/allMethodTestsInOneFile.scala b/allMethodTestsInOneFile.scala index 29e1e24..e9c1e4c 100644 --- a/allMethodTestsInOneFile.scala +++ b/allMethodTestsInOneFile.scala @@ -4,21 +4,24 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/*def scalaVersion = { - val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion -}*/ +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} +val scalaTestVersion = "2.1.6" val junitVersion = "4.11" // JUnit depends on hamcrestVersion val hamcrestVersion = "1.3" -val testngVersion = "6.8" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val testngVersion = "6.8.8" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -89,7 +92,7 @@ def specs2MutableTestDefFun(x: Int): String = "\"increment " + x + "\" in" def compile(srcFile: String, classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile) + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -174,7 +177,7 @@ if (scalaVersion != "unknown") { if (!testngJar.exists) downloadFile("http://repo1.maven.org/maven2/org/testng/testng/" + testngVersion + "/testng-" + testngVersion + ".jar", testngJar) - val baseDir = new File("allMethodTestsInOneFile") + val baseDir = new File("target/" + scalaVersion + "/allMethodTestsInOneFile") if (baseDir.exists) deleteDir(baseDir) diff --git a/allTestsInOneFile.scala b/allTestsInOneFile.scala index bbb4d0a..c928807 100644 --- a/allTestsInOneFile.scala +++ b/allTestsInOneFile.scala @@ -4,19 +4,21 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/* -def scalaVersion = { +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt } -*/ -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val scalaTestVersion = "2.1.6" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -150,7 +152,7 @@ class ExampleSpec extends Specification { def is =""" + "\n" + def compile(srcFile: String, classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile) + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -221,7 +223,7 @@ if (scalaVersion != "unknown") { if (!specs2ScalazJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-core_" + scalaVersion + "/" + scalazVersion + "/scalaz-core_" + scalaVersion + "-" + scalazVersion + ".jar", specs2ScalazJar) - val baseDir = new File("allTestsInOneFile") + val baseDir = new File("target/" + scalaVersion + "/allTestsInOneFile") if (baseDir.exists) deleteDir(baseDir) diff --git a/assertBeforeAfter.scala b/assertBeforeAfter.scala new file mode 100644 index 0000000..12853cc --- /dev/null +++ b/assertBeforeAfter.scala @@ -0,0 +1,260 @@ +import java.net._ +import java.io._ +import java.nio.channels.Channels +import scala.annotation.tailrec +import scala.math.pow + +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} + +val scalaTestWithoutMacroVersion = "3.0.0-before" +val scalaTestWithMacroVersion = "3.0.0-after" + +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-app_" + scalaVersion + "-" + scalaTestWithoutMacroVersion + ".jar") +if (!scalatestOldJar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestWithoutMacroVersion + "/scalatest-app_" + scalaVersion + "-" + scalaTestWithoutMacroVersion + ".jar", scalatestOldJar) + +val scalatestMacroJar = new File("scalatest-app_" + scalaVersion + "-" + scalaTestWithMacroVersion + ".jar") +if (!scalatestMacroJar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestWithMacroVersion + "/scalatest-app_" + scalaVersion + "-" + scalaTestWithMacroVersion + ".jar", scalatestMacroJar) + +val baseDir = new File("target/" + scalaVersion + "/assertBeforeAfter") +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("Before asert(==)", "BeforeEqual2", Array("org.scalatest._"), Array.empty, assertEqual2TestBodyFun, scalatestOldJar), + TestType("After assert(==)", "AfterEqual2", Array("org.scalatest._"), Array.empty, assertEqual2TestBodyFun, scalatestMacroJar), + TestType("Before asert(===)", "BeforeEqual3", Array("org.scalatest._"), Array.empty, assertEqual3TestBodyFun, scalatestOldJar), + TestType("After assert(===)", "AfterEqual3", 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 " + 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() diff --git a/assertMacro.scala b/assertMacro.scala index 4d372c0..775601a 100644 --- a/assertMacro.scala +++ b/assertMacro.scala @@ -245,4 +245,4 @@ durationFile.close() fileCountFile.flush() fileCountFile.close() fileSizeFile.flush() -fileSizeFile.close() \ No newline at end of file +fileSizeFile.close() diff --git a/assertTestsInOneFile.scala b/assertTestsInOneFile.scala index 9478c8f..c315daa 100644 --- a/assertTestsInOneFile.scala +++ b/assertTestsInOneFile.scala @@ -4,19 +4,21 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/* -def scalaVersion = { +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt } -*/ -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val scalaTestVersion = "2.1.6" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -76,7 +78,7 @@ 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 command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -139,7 +141,7 @@ if (scalaVersion != "unknown") { if (!scalatestJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestVersion + "/scalatest_" + scalaVersion + "-" + scalaTestVersion + ".jar", scalatestJar) - val baseDir = new File("assertTestsInOneFile") + val baseDir = new File("target/" + scalaVersion + "/assertTestsInOneFile") if (baseDir.exists) deleteDir(baseDir) diff --git a/dataTables.scala b/dataTables.scala index d8a6baf..aa3f1c9 100644 --- a/dataTables.scala +++ b/dataTables.scala @@ -4,19 +4,27 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/* -def scalaVersion = { +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt } -*/ -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val scalaTestVersion = "2.1.6" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" +val shapelessVersion = "2.0.0" +val shapelessScalaVersion = + if (scalaVersion == "2.11") + scalaVersion + else + scala.util.Properties.scalaPropOrElse("version.number", "unknown") def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -190,7 +198,7 @@ def generateSpecs2Mutable(testCount: Int, targetDir: File): File = { def compile(srcFile: String, classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile) + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -279,11 +287,11 @@ if (scalaVersion != "unknown") { if (!specs2ScalazJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-core_" + scalaVersion + "/" + scalazVersion + "/scalaz-core_" + scalaVersion + "-" + scalazVersion + ".jar", specs2ScalazJar) - val shapelessJar = new File("shapeless_2.10.2-2.0.0-SNAPSHOT.jar") + val shapelessJar = new File("shapeless_" + scalaVersion + "-" + shapelessVersion + ".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) + downloadFile("http://oss.sonatype.org/content/repositories/releases/com/chuusai/shapeless_" + shapelessScalaVersion + "/" + shapelessVersion + "/shapeless_" + shapelessScalaVersion + "-" + shapelessVersion + ".jar", shapelessJar) - val baseDir = new File("dataTables") + val baseDir = new File("target/" + scalaVersion + "/dataTables") if (baseDir.exists) deleteDir(baseDir) diff --git a/google-chart.scala b/google-chart.scala index a1adb14..6925ba0 100644 --- a/google-chart.scala +++ b/google-chart.scala @@ -135,72 +135,184 @@ def generateChartFile(srcDir: File, targetFile: File, whichChart: WhichChart = B println("Generated " + targetFile.getAbsolutePath) } -val allTestsInOneFileDir = new File("allTestsInOneFile") -val allTestsInOneFileStatDir = new File(allTestsInOneFileDir, "stat") -if (allTestsInOneFileStatDir.exists) - generateChartFile(allTestsInOneFileStatDir, new File(allTestsInOneFileDir, "allTestsInOneFile-graph.html")) +val allTestsInOneFile210Dir = new File("target/2.10/allTestsInOneFile") +val allTestsInOneFile210StatDir = new File(allTestsInOneFile210Dir, "stat") +if (allTestsInOneFile210StatDir.exists) + generateChartFile(allTestsInOneFile210StatDir, new File(allTestsInOneFile210Dir, "allTestsInOneFile-graph.html")) else - println("allTestsInOneFile/stat directory does not exist, allTestsInOneFile/allTestsInOneFile-graph.html will not be generated.") + println("target/2.10/allTestsInOneFile/stat directory does not exist, target/2.10/allTestsInOneFile/allTestsInOneFile-graph.html will not be generated.") + +val allTestsInOneFile211Dir = new File("target/2.11/allTestsInOneFile") +val allTestsInOneFile211StatDir = new File(allTestsInOneFile211Dir, "stat") +if (allTestsInOneFile211StatDir.exists) + generateChartFile(allTestsInOneFile211StatDir, new File(allTestsInOneFile211Dir, "allTestsInOneFile-graph.html")) +else + println("target/2.11/allTestsInOneFile/stat directory does not exist, target/2.11/allTestsInOneFile/allTestsInOneFile-graph.html will not be generated.") -val tenTestsPerFileDir = new File("tenTestsPerFile") -val tenTestsPerFileStatDir = new File(tenTestsPerFileDir, "stat") -if (tenTestsPerFileStatDir.exists) - generateChartFile(tenTestsPerFileStatDir, new File(tenTestsPerFileDir, "tenTestsPerFile-graph.html")) +val tenTestsPerFile210Dir = new File("target/2.10/tenTestsPerFile") +val tenTestsPerFile210StatDir = new File(tenTestsPerFile210Dir, "stat") +if (tenTestsPerFile210StatDir.exists) + generateChartFile(tenTestsPerFile210StatDir, new File(tenTestsPerFile210Dir, "tenTestsPerFile-graph.html")) +else + println("target/2.10/tenTestsPerFile/stat directory does not exist, target/2.10/tenTestsPerFile/tenTestsPerFile-graph.html will not be generated.") + +val tenTestsPerFile211Dir = new File("target/2.11/tenTestsPerFile") +val tenTestsPerFile211StatDir = new File(tenTestsPerFile211Dir, "stat") +if (tenTestsPerFile211StatDir.exists) + generateChartFile(tenTestsPerFile211StatDir, new File(tenTestsPerFile211Dir, "tenTestsPerFile-graph.html")) else - println("tenTestsPerFile/stat directory does not exist, tenTestsPerFile/tenTestsPerFile-graph.html will not be generated.") + println("target/2.11/tenTestsPerFile/stat directory does not exist, target/2.11/tenTestsPerFile/tenTestsPerFile-graph.html will not be generated.") -val testsIn100FilesDir = new File("testsIn100Files") -val testsIn100FilesStatDir = new File(testsIn100FilesDir, "stat") -if (testsIn100FilesStatDir.exists) - generateChartFile(testsIn100FilesStatDir, new File(testsIn100FilesDir, "testsIn100Files-graph.html"), FirstChart) +val testsIn100Files210Dir = new File("target/2.10/testsIn100Files") +val testsIn100Files210StatDir = new File(testsIn100Files210Dir, "stat") +if (testsIn100Files210StatDir.exists) + generateChartFile(testsIn100Files210StatDir, new File(testsIn100Files210Dir, "testsIn100Files-graph.html"), FirstChart) +else + println("target/2.10/testsIn100Files/stat directory does not exist, target/2.10/testsIn100Files/tenTestsPerFile-graph.html will not be generated.") + +val testsIn100Files211Dir = new File("target/2.11/testsIn100Files") +val testsIn100Files211StatDir = new File(testsIn100Files211Dir, "stat") +if (testsIn100Files211StatDir.exists) + generateChartFile(testsIn100Files211StatDir, new File(testsIn100Files211Dir, "testsIn100Files-graph.html"), FirstChart) +else + println("target/2.11/testsIn100Files/stat directory does not exist, target/2.11/testsIn100Files/tenTestsPerFile-graph.html will not be generated.") + +val dataTables210Dir = new File("target/2.10/dataTables") +val dataTables210StatDir = new File(dataTables210Dir, "stat") +if (dataTables210StatDir.exists) + generateChartFile(dataTables210StatDir, new File(dataTables210Dir, "dataTables-graph.html")) else - println("testsIn100Files/stat directory does not exist, testsIn100Files/tenTestsPerFile-graph.html will not be generated.") + println("target/2.10/dataTables/stat directory does not exist, target/2.10/dataTables/dataTables-graph.html will not be generated.") -val dataTablesDir = new File("dataTables") -val dataTablesStatDir = new File(dataTablesDir, "stat") -if (dataTablesStatDir.exists) - generateChartFile(dataTablesStatDir, new File(dataTablesDir, "dataTables-graph.html")) +val dataTables211Dir = new File("target/2.11/dataTables") +val dataTables211StatDir = new File(dataTables211Dir, "stat") +if (dataTables211StatDir.exists) + generateChartFile(dataTables211StatDir, new File(dataTables211Dir, "dataTables-graph.html")) else - println("dataTables/stat directory does not exist, dataTables/dataTables-graph.html will not be generated.") + println("target/2.11/dataTables/stat directory does not exist, target/2.11/dataTables/dataTables-graph.html will not be generated.") -val allMethodTestsInOneFileDir = new File("allMethodTestsInOneFile") -val allMethodTestsInOneFileStatDir = new File(allMethodTestsInOneFileDir, "stat") -if (allMethodTestsInOneFileStatDir.exists) - generateChartFile(allMethodTestsInOneFileStatDir, new File(allMethodTestsInOneFileDir, "allMethodTestsInOneFile-graph.html"), SecondChart) +val allMethodTestsInOneFile210Dir = new File("target/2.10/allMethodTestsInOneFile") +val allMethodTestsInOneFile210StatDir = new File(allMethodTestsInOneFile210Dir, "stat") +if (allMethodTestsInOneFile210StatDir.exists) + generateChartFile(allMethodTestsInOneFile210StatDir, new File(allMethodTestsInOneFile210Dir, "allMethodTestsInOneFile-graph.html"), SecondChart) else - println("allMethodTestsInOneFile/stat directory does not exist, allMethodTestsInOneFile/allMethodTestsInOneFile-graph.html will not be generated.") + println("target/2.10/allMethodTestsInOneFile/stat directory does not exist, target/2.10/allMethodTestsInOneFile/allMethodTestsInOneFile-graph.html will not be generated.") -val assertTestsInOneFileDir = new File("assertTestsInOneFile") -val assertTestsInOneFileStatDir = new File(assertTestsInOneFileDir, "stat") -if (assertTestsInOneFileStatDir.exists) - generateChartFile(assertTestsInOneFileStatDir, new File(assertTestsInOneFileDir, "assertTestsInOneFile-graph.html")) +val allMethodTestsInOneFile211Dir = new File("target/2.11/allMethodTestsInOneFile") +val allMethodTestsInOneFile211StatDir = new File(allMethodTestsInOneFile211Dir, "stat") +if (allMethodTestsInOneFile211StatDir.exists) + generateChartFile(allMethodTestsInOneFile211StatDir, new File(allMethodTestsInOneFile211Dir, "allMethodTestsInOneFile-graph.html"), SecondChart) else - println("assertTestsInOneFile/stat directory does not exist, assertTestsInOneFile/assertTestsInOneFile-graph.html will not be generated.") + println("target/2.11/allMethodTestsInOneFile/stat directory does not exist, target/2.11/allMethodTestsInOneFile/allMethodTestsInOneFile-graph.html will not be generated.") + +val assertTestsInOneFile210Dir = new File("target/2.10/assertTestsInOneFile") +val assertTestsInOneFile210StatDir = new File(assertTestsInOneFile210Dir, "stat") +if (assertTestsInOneFile210StatDir.exists) + generateChartFile(assertTestsInOneFile210StatDir, new File(assertTestsInOneFile210Dir, "assertTestsInOneFile-graph.html")) +else + println("target/2.10/assertTestsInOneFile/stat directory does not exist, target/2.10/assertTestsInOneFile/assertTestsInOneFile-graph.html will not be generated.") + +val assertTestsInOneFile211Dir = new File("target/2.11/assertTestsInOneFile") +val assertTestsInOneFile211StatDir = new File(assertTestsInOneFile211Dir, "stat") +if (assertTestsInOneFile211StatDir.exists) + generateChartFile(assertTestsInOneFile211StatDir, new File(assertTestsInOneFile211Dir, "assertTestsInOneFile-graph.html")) +else + println("target/2.11/assertTestsInOneFile/stat directory does not exist, target/2.11/assertTestsInOneFile/assertTestsInOneFile-graph.html will not be generated.") -val allClassTestsInOneFileDir = new File("allClassTestsInOneFile") -val allClassTestsInOneFileStatDir = new File(allClassTestsInOneFileDir, "stat") -if (allClassTestsInOneFileStatDir.exists) - generateChartFile(allClassTestsInOneFileStatDir, new File(allClassTestsInOneFileDir, "allClassTestsInOneFile-graph.html")) +val allClassTestsInOneFile210Dir = new File("target/2.10/allClassTestsInOneFile") +val allClassTestsInOneFile210StatDir = new File(allClassTestsInOneFile210Dir, "stat") +if (allClassTestsInOneFile210StatDir.exists) + generateChartFile(allClassTestsInOneFile210StatDir, new File(allClassTestsInOneFile210Dir, "allClassTestsInOneFile-graph.html")) +else + println("target/2.10/allClassTestsInOneFile/stat directory does not exist, target/2.10/allClassTestsInOneFile/allClassTestsInOneFile-graph.html will not be generated.") + +val allClassTestsInOneFile211Dir = new File("target/2.11/allClassTestsInOneFile") +val allClassTestsInOneFile211StatDir = new File(allClassTestsInOneFile211Dir, "stat") +if (allClassTestsInOneFile211StatDir.exists) + generateChartFile(allClassTestsInOneFile211StatDir, new File(allClassTestsInOneFile211Dir, "allClassTestsInOneFile-graph.html")) +else + println("target/2.11/allClassTestsInOneFile/stat directory does not exist, target/2.11/allClassTestsInOneFile/allClassTestsInOneFile-graph.html will not be generated.") + +val assertMacro210Dir = new File("target/2.10/assertMacro") +val assertMacro210StatDir = new File(assertMacro210Dir, "stat") +if (assertMacro210StatDir.exists) + generateChartFile(assertMacro210StatDir, new File(assertMacro210Dir, "assertMacro-graph.html")) +else + println("target/2.10/assertMacro/stat directory does not exist, target/2.10/assertMacro/assertMacro-graph.html will not be generated.") + +val assertMacro211Dir = new File("target/2.11/assertMacro") +val assertMacro211StatDir = new File(assertMacro211Dir, "stat") +if (assertMacro211StatDir.exists) + generateChartFile(assertMacro211StatDir, new File(assertMacro211Dir, "assertMacro-graph.html")) +else + println("target/2.11/assertMacro/stat directory does not exist, target/2.11/assertMacro/assertMacro-graph.html will not be generated.") + +val scalautilsScalaz210Dir = new File("target/2.10/scalautilsScalaz") +val scalautilsScalaz210StatDir = new File(scalautilsScalaz210Dir, "stat") +if (scalautilsScalaz210StatDir.exists) + generateChartFile(scalautilsScalaz210StatDir, new File(scalautilsScalaz210Dir, "scalautilsScalaz-graph.html")) +else + println("target/2.10/scalautilsScalaz/stat directory does not exist, target/2.10/scalautilsScalaz/scalautilsScalaz-graph.html will not be generated.") + +val scalautilsScalaz211Dir = new File("target/2.11/scalautilsScalaz") +val scalautilsScalaz211StatDir = new File(scalautilsScalaz211Dir, "stat") +if (scalautilsScalaz211StatDir.exists) + generateChartFile(scalautilsScalaz211StatDir, new File(scalautilsScalaz211Dir, "scalautilsScalaz-graph.html")) +else + println("target/2.11/scalautilsScalaz/stat directory does not exist, target/2.11/scalautilsScalaz/scalautilsScalaz-graph.html will not be generated.") + +val shapelessTables210Dir = new File("target/2.10/shapelessTables") +val shapelessTables210StatDir = new File(shapelessTables210Dir, "stat") +if (shapelessTables210StatDir.exists) + generateChartFile(shapelessTables210StatDir, new File(shapelessTables210Dir, "shapelessTables-graph.html")) +else + println("target/2.10/shapelessTables/stat directory does not exist, target/2.10/shapelessTables/shapelessTables-graph.html will not be generated.") + +val shapelessTables211Dir = new File("target/2.11/shapelessTables") +val shapelessTables211StatDir = new File(shapelessTables211Dir, "stat") +if (shapelessTables211StatDir.exists) + generateChartFile(shapelessTables211StatDir, new File(shapelessTables211Dir, "shapelessTables-graph.html")) +else + println("target/2.11/shapelessTables/stat directory does not exist, target/2.11/shapelessTables/shapelessTables-graph.html will not be generated.") + +val scalatest3211Dir = new File("target/2.11/scalatest3") +val scalatest3211StatDir = new File(scalatest3211Dir, "stat") +if (scalatest3211StatDir.exists) + generateChartFile(scalatest3211StatDir, new File(scalatest3211Dir, "scalatest3-graph.html")) +else + println("target/2.11/scalatest3/stat directory does not exist, target/2.11/scalatest3/scalatest3-graph.html will not be generated.") + +val supersafe211Dir = new File("target/2.11/supersafe") +val supersafe211StatDir = new File(supersafe211Dir, "stat") +if (supersafe211StatDir.exists) + generateChartFile(supersafe211StatDir, new File(supersafe211Dir, "supersafe-graph.html")) +else + println("target/2.11/supersafe/stat directory does not exist, target/2.11/supersafe/supersafe-graph.html will not be generated.") + +val assertBeforeAfter210Dir = new File("target/2.10/assertBeforeAfter") +val assertBeforeAfter210StatDir = new File(assertBeforeAfter210Dir, "stat") +if (assertBeforeAfter210StatDir.exists) + generateChartFile(assertBeforeAfter210StatDir, new File(assertBeforeAfter210Dir, "assertBeforeAfter-graph.html")) else - println("allClassTestsInOneFile/stat directory does not exist, allClassTestsInOneFile/allClassTestsInOneFile-graph.html will not be generated.") + println("target/2.10/assertBeforeAfter/stat directory does not exist, target/2.10/assertBeforeAfter/assertBeforeAfter-graph.html will not be generated.") -val assertMacroDir = new File("assertMacro") -val assertMacroStatDir = new File(assertMacroDir, "stat") -if (assertMacroStatDir.exists) - generateChartFile(assertMacroStatDir, new File(assertMacroDir, "assertMacro-graph.html")) +val assertBeforeAfter211Dir = new File("target/2.11/assertBeforeAfter") +val assertBeforeAfter211StatDir = new File(assertBeforeAfter211Dir, "stat") +if (assertBeforeAfter211StatDir.exists) + generateChartFile(assertBeforeAfter211StatDir, new File(assertBeforeAfter211Dir, "assertBeforeAfter-graph.html")) else - println("assertMacro/stat directory does not exist, assertMacro/assertMacro-graph.html will not be generated.") + println("target/2.11/assertBeforeAfter/stat directory does not exist, target/2.11/assertBeforeAfter/assertBeforeAfter-graph.html will not be generated.") -val scalautilsScalazDir = new File("scalautilsScalaz") -val scalautilsScalazStatDir = new File(scalautilsScalazDir, "stat") -if (scalautilsScalazStatDir.exists) - generateChartFile(scalautilsScalazStatDir, new File(scalautilsScalazDir, "scalautilsScalaz-graph.html")) +val matcherBeforeAfter210Dir = new File("target/2.10/matcherBeforeAfter") +val matcherBeforeAfter210StatDir = new File(matcherBeforeAfter210Dir, "stat") +if (matcherBeforeAfter210StatDir.exists) + generateChartFile(matcherBeforeAfter210StatDir, new File(matcherBeforeAfter210Dir, "matcherBeforeAfter-graph.html")) else - println("scalautilsScalaz/stat directory does not exist, scalautilsScalaz/scalautilsScalaz-graph.html will not be generated.") + println("target/2.10/matcherBeforeAfter/stat directory does not exist, target/2.10/matcherBeforeAfter/matcherBeforeAfter-graph.html will not be generated.") -val shapelessTablesDir = new File("shapelessTables") -val shapelessTablesStatDir = new File(shapelessTablesDir, "stat") -if (shapelessTablesStatDir.exists) - generateChartFile(shapelessTablesStatDir, new File(shapelessTablesDir, "shapelessTables-graph.html")) +val matcherBeforeAfter211Dir = new File("target/2.11/matcherBeforeAfter") +val matcherBeforeAfter211StatDir = new File(matcherBeforeAfter211Dir, "stat") +if (matcherBeforeAfter211StatDir.exists) + generateChartFile(matcherBeforeAfter211StatDir, new File(matcherBeforeAfter211Dir, "matcherBeforeAfter-graph.html")) else - println("shapelessTables/stat directory does not exist, shapelessTables/shapelessTables-graph.html will not be generated.") + println("target/2.11/matcherBeforeAfter/stat directory does not exist, target/2.11/matcherBeforeAfter/matcherBeforeAfter-graph.html will not be generated.") diff --git a/matcherBeforeAfter.scala b/matcherBeforeAfter.scala new file mode 100644 index 0000000..fb7a2ef --- /dev/null +++ b/matcherBeforeAfter.scala @@ -0,0 +1,260 @@ +import java.net._ +import java.io._ +import java.nio.channels.Channels +import scala.annotation.tailrec +import scala.math.pow + +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} + +val scalaTestWithoutMacroVersion = "3.0.0-before" +val scalaTestWithMacroVersion = "3.0.0-after" + +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 shouldBeTestBodyFun(x: Int): String = x + " + 1 should be (" + (x+1) + ")" +// Using assert(===) +def shouldBeShorthandTestBodyFun(x: Int): String = x + " + 1 shouldBe " + (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-app_" + scalaVersion + "-" + scalaTestWithoutMacroVersion + ".jar") +if (!scalatestOldJar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestWithoutMacroVersion + "/scalatest-app_" + scalaVersion + "-" + scalaTestWithoutMacroVersion + ".jar", scalatestOldJar) + +val scalatestMacroJar = new File("scalatest-app_" + scalaVersion + "-" + scalaTestWithMacroVersion + ".jar") +if (!scalatestMacroJar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest_" + scalaVersion + "/" + scalaTestWithMacroVersion + "/scalatest-app_" + scalaVersion + "-" + scalaTestWithMacroVersion + ".jar", scalatestMacroJar) + +val baseDir = new File("target/" + scalaVersion + "/matcherBeforeAfter") +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("Before should be", "BeforeShouldBe", Array("org.scalatest._", "Matchers._"), Array.empty, shouldBeTestBodyFun, scalatestOldJar), + TestType("After should be", "AfterShouldBe", Array("org.scalatest._", "Matchers._"), Array.empty, shouldBeTestBodyFun, scalatestMacroJar), + TestType("Before shouldBe", "BeforeShouldBeShorthand", Array("org.scalatest._", "Matchers._"), Array.empty, shouldBeShorthandTestBodyFun, scalatestOldJar), + TestType("After shouldBe", "AfterShouldBeShorthand", Array("org.scalatest._", "Matchers._"), Array.empty, shouldBeShorthandTestBodyFun, 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 " + 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() diff --git a/runem.sh b/runem.sh old mode 100644 new mode 100755 index e15c1cd..51ec46b --- a/runem.sh +++ b/runem.sh @@ -1,15 +1,28 @@ set -x JAVA_OPTS="-server -Xmx1024M -Xms128M" export JAVA_OPTS -scala tenTestsPerFile.scala +$SCALA_210/bin/scala tenTestsPerFile.scala +$SCALA_211/bin/scala tenTestsPerFile.scala JAVA_OPTS="-server -Xmx2048M -Xms256M" export JAVA_OPTS -scala allTestsInOneFile.scala -scala testsIn100Files.scala -scala dataTables.scala -scala allMethodTestsInOneFile.scala -scala assertTestsInOneFile.scala -scala allClassTestsInOneFile.scala -scala scalautilsScalaz.scala -scala shapelessTables.scala -scala google-chart.scala +$SCALA_210/bin/scala allTestsInOneFile.scala +$SCALA_211/bin/scala allTestsInOneFile.scala +$SCALA_210/bin/scala testsIn100Files.scala +$SCALA_211/bin/scala testsIn100Files.scala +$SCALA_210/bin/scala dataTables.scala +$SCALA_211/bin/scala dataTables.scala +$SCALA_210/bin/scala allMethodTestsInOneFile.scala +$SCALA_211/bin/scala allMethodTestsInOneFile.scala +$SCALA_210/bin/scala assertTestsInOneFile.scala +$SCALA_211/bin/scala assertTestsInOneFile.scala +$SCALA_210/bin/scala allClassTestsInOneFile.scala +$SCALA_211/bin/scala allClassTestsInOneFile.scala +$SCALA_210/bin/scala scalautilsScalaz.scala +$SCALA_211/bin/scala scalautilsScalaz.scala +$SCALA_210/bin/scala shapelessTables.scala +$SCALA_211/bin/scala shapelessTables.scala +$SCALA_210/bin/scala assertBeforeAfter.scala +$SCALA_211/bin/scala assertBeforeAfter.scala +$SCALA_210/bin/scala matcherBeforeAfter.scala +$SCALA_211/bin/scala matcherBeforeAfter.scala +$SCALA_210/bin/scala google-chart.scala diff --git a/scalatest3.scala b/scalatest3.scala new file mode 100644 index 0000000..47fada8 --- /dev/null +++ b/scalatest3.scala @@ -0,0 +1,402 @@ +import java.net._ +import java.io._ +import java.nio.channels.Channels +import scala.annotation.tailrec +import scala.math.pow + +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} +val scalaTest2Version = "2.3.0-SNAP3" +val scalaTest3Version = "3.0.0-SNAP5" + +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, fileNumber: Int, 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" + fileNumber + ".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" + fileNumber + " 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 +} + +def generateMultipleSourceFiles(testCount: Int, maxTestCount:Int, targetDir: File, packageName: String, importStatements: Array[String], + extendsName: String, withNames: Array[String], scopeBracket: Boolean, scopeDef: String, + testDefFun: (Int) => String, testBodyFun: (Int) => String): List[String] = { + targetDir.mkdirs() + val totalFiles = testCount / maxTestCount + val remainder = testCount % maxTestCount + + val sourceFiles = + if (totalFiles == 0) + List(generateSourceFile(testCount, targetDir, 0, packageName, importStatements, + extendsName, withNames, scopeBracket, scopeDef, testDefFun, testBodyFun).getAbsolutePath) + else + for (fileNumber <- 1 to totalFiles) yield + generateSourceFile(maxTestCount, targetDir, fileNumber, packageName, importStatements, + extendsName, withNames, scopeBracket, scopeDef, testDefFun, testBodyFun).getAbsolutePath + + // For remainder + if (remainder > 0) + List(generateSourceFile(remainder, targetDir, totalFiles + 1, packageName, importStatements, + extendsName, withNames, scopeBracket, scopeDef, testDefFun, testBodyFun).getAbsolutePath) ++ sourceFiles + else + sourceFiles.toList +} + +// Using assert(==) +def assert2TestBodyFun(x: Int): String = "assert(" + x + " + 1 == " + (x+1) + ")" +// Using assert(===) +def assert3TestBodyFun(x: Int): String = "assert(" + x + " + 1 === " + (x+1) + ")" +// Using should equal matchers +def shouldEqualTestBodyFun(x: Int): String = x + " + 1 should equal (" + (x+1) + ")" +// Using should be matchers +def shouldBeTestBodyFun(x: Int): String = x + " + 1 should be (" + (x+1) + ")" +// Using should be matchers +def shouldContainTestBodyFun(x: Int): String = "List(" + x + " + 1) should contain (" + (x+1) + ")" +// Using should equal matchers with logical and +def shouldLogicalAndEqualTestBodyFun(x: Int): String = x + " + 1 should (not equal " + x + " and not equal " + (x-1) + ")" +// Using should be matchers with logical and +def shouldLogicalAndBeTestBodyFun(x: Int): String = x + " + 1 should (not be " + x + " and not be " + (x-1) + ")" +// Using should contain matchers with logical and +def shouldLogicalAndContainTestBodyFun(x: Int): String = "List(" + x + " + 1) should (not contain " + x + " and not contain " + (x-1) + ")" + +// Spec +def specTestDefFun(x: Int): String = "def `increment " + x + "`" +// WordSpec +def wordSpecTestDefFun(x: Int): String = "\"increment " + x + "\" in" +//FunSuite +def funSuiteTestDefFun(x: Int): String = "test(\"increment " + x + "\")" +//FunSpec +def funSpecTestDefFun(x: Int): String = "it(\"increment " + x + "\")" +//FreeSpec +def freeSpecTestDefFun(x: Int): String = "\"increment " + x + "\" in" +//FlatSpec +def flatSpecTestDefFun(x: Int): String = "it should \"increment " + x + "\" in" + +def compile(srcFiles: List[String], classpath: String, targetDir: String) = { + import scala.collection.JavaConversions._ + + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir) ::: srcFiles + 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 " + srcFiles.mkString(", ") + "..." + + 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 +} + +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) + +if (scalaVersion != "unknown") { + val scalatest2Jar = new File("scalatest-all_" + scalaVersion + "-" + scalaTest2Version + ".jar") + if (!scalatest2Jar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest-all_" + scalaVersion + "/" + scalaTest2Version + "/scalatest-all_" + scalaVersion + "-" + scalaTest2Version + ".jar", scalatest2Jar) + + val scalatest3Jar = new File("scalatest-all_" + scalaVersion + "-" + scalaTest3Version + ".jar") + if (!scalatest3Jar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest-all_" + scalaVersion + "/" + scalaTest3Version + "/scalatest-all_" + scalaVersion + "-" + scalaTest3Version + ".jar", scalatest3Jar) + + val baseDir = new File("target/" + scalaVersion + "/scalatest3") + 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 scalaTest2Classpath = scalatest2Jar.getName + val scalaTest3Classpath = scalatest3Jar.getName + + val baseOutputDir = new File(baseDir, "output") + baseOutputDir.mkdirs() + + val baseGeneratedDir = new File(baseDir, "generated") + baseGeneratedDir.mkdirs() + + val scalatest2Styles = + Array( + Style("scalatest2", "WordSpec", true, "\"Scala\" can ", wordSpecTestDefFun) + ) + + val scalatest2TestTypes: Array[TestType] = + Array( + TestType("equal", "equals", Array("org.scalatest._", "Matchers._"), Array.empty, shouldEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._", "org.scalactic.TypeCheckedTripleEquals._"), Array.empty, shouldEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._"), Array.empty, shouldLogicalAndEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._", "org.scalactic.TypeCheckedTripleEquals._"), Array.empty, shouldLogicalAndEqualTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._"), Array.empty, shouldBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._", "org.scalactic.TypeCheckedTripleEquals._"), Array.empty, shouldBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._"), Array.empty, shouldLogicalAndBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._", "org.scalactic.TypeCheckedTripleEquals._"), Array.empty, shouldLogicalAndBeTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._"), Array.empty, shouldContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._", "org.scalactic.TypeCheckedTripleEquals._"), Array.empty, shouldContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._"), Array.empty, shouldLogicalAndContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._", "org.scalactic.TypeCheckedTripleEquals._"), Array.empty, shouldLogicalAndContainTestBodyFun) + ) + + val scalatest3Styles = + Array( + Style("scalatest3", "WordSpec", true, "\"Scala\" can ", wordSpecTestDefFun) + ) + + val scalatest3TestTypes: Array[TestType] = + Array( + TestType("equal", "equals", Array("org.scalatest._", "Matchers._"), Array.empty, shouldEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._", "org.scalactic.CheckedEquality._"), Array.empty, shouldEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._", "org.scalactic.StrictCheckedEquality._"), Array.empty, shouldEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._"), Array.empty, shouldLogicalAndEqualTestBodyFun), + TestType("equal", "equals", Array("org.scalatest._", "Matchers._", "org.scalactic.CheckedEquality._"), Array.empty, shouldLogicalAndEqualTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._"), Array.empty, shouldBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._", "org.scalactic.CheckedEquality._"), Array.empty, shouldBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._", "org.scalactic.StrictCheckedEquality._"), Array.empty, shouldBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._"), Array.empty, shouldLogicalAndBeTestBodyFun), + TestType("be", "be", Array("org.scalatest._", "Matchers._", "org.scalactic.CheckedEquality._"), Array.empty, shouldLogicalAndBeTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._"), Array.empty, shouldContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._", "org.scalactic.CheckedEquality._"), Array.empty, shouldContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._", "org.scalactic.StrictCheckedEquality._"), Array.empty, shouldContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._"), Array.empty, shouldLogicalAndContainTestBodyFun), + TestType("contain", "contain", Array("org.scalatest._", "Matchers._", "org.scalactic.CheckedEquality._"), Array.empty, shouldLogicalAndContainTestBodyFun) + ) + + 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) + + scalatest2Styles.foreach { style => + scalatest2TestTypes.foreach { testType => + try { + durationFile.write(style.name + "-" + testType.shortName) // Don't write with MustMatchers to get all 4 names to fit on graph + durationFile.flush() + fileCountFile.write(style.name + "-" + testType.shortName) + fileCountFile.flush() + fileSizeFile.write(style.name + "-" + testType.shortName) + fileSizeFile.flush() + testCounts.foreach { testCount => + println("Working on " + style.className +" " + testType.name + " test count " + testCount + "...") + val outputDir = getOutputDir(baseOutputDir, testCount) + val generatedDir = new File(baseGeneratedDir, "generated-" + testCount) + + val generatedSrc = generateMultipleSourceFiles( + testCount, + 10, // maximum number of tests in a file + new File(generatedDir, "ScalaTest2" + style.className), // target dir + style.className + testType.shortName + "2", // 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, scalaTest2Classpath, outputDir.getAbsolutePath) + durationFile.write("," + duration) + durationFile.flush() + + val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, style.className + testType.shortName + "2")) + 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() + } + } + } + + scalatest3Styles.foreach { style => + scalatest3TestTypes.foreach { testType => + try { + durationFile.write(style.name + "-" + testType.shortName) // Don't write with MustMatchers to get all 4 names to fit on graph + durationFile.flush() + fileCountFile.write(style.name + "-" + testType.shortName) + fileCountFile.flush() + fileSizeFile.write(style.name + "-" + testType.shortName) + fileSizeFile.flush() + testCounts.foreach { testCount => + println("Working on " + style.className +" " + testType.name + " test count " + testCount + "...") + val outputDir = getOutputDir(baseOutputDir, testCount) + val generatedDir = new File(baseGeneratedDir, "generated-" + testCount) + + val generatedSrc = generateMultipleSourceFiles( + testCount, + 10, // maximum number of tests in a file + new File(generatedDir, "ScalaTest3" + style.className), // target dir + style.className + testType.shortName + "3", // 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, scalaTest3Classpath, outputDir.getAbsolutePath) + durationFile.write("," + duration) + durationFile.flush() + + val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, style.className + testType.shortName + "3")) + 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() +} +else + println("ERROR: Unable to detect Scala version.") diff --git a/scalautilsScalaz.scala b/scalautilsScalaz.scala index a0eefa1..c2930b8 100644 --- a/scalautilsScalaz.scala +++ b/scalautilsScalaz.scala @@ -3,9 +3,20 @@ import java.io._ import java.nio.channels.Channels import scala.annotation.tailrec -def scalaVersion = "2.10" -val scalautilsVersion = "2.0" -val scalazVersion = "7.0.4" +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} +val scalautilsVersion = "2.1.6" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -47,7 +58,7 @@ def equalFun(x: Int): String = x + " + 1 === " + (x+1) def compile(srcFile: String, classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile) + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -110,7 +121,7 @@ val scalazJar = new File("scalaz-core_" + scalaVersion + "-" + scalazVersion + " if (!scalazJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-core_" + scalaVersion + "/" + scalazVersion + "/scalaz-core_" + scalaVersion + "-" + scalazVersion + ".jar", scalazJar) -val baseDir = new File("scalautilsScalaz") +val baseDir = new File("target/" + scalaVersion + "/scalautilsScalaz") if (baseDir.exists) deleteDir(baseDir) @@ -229,4 +240,4 @@ durationFile.close() fileCountFile.flush() fileCountFile.close() fileSizeFile.flush() -fileSizeFile.close() \ No newline at end of file +fileSizeFile.close() diff --git a/shapelessTables.scala b/shapelessTables.scala index bd761d1..ac26158 100644 --- a/shapelessTables.scala +++ b/shapelessTables.scala @@ -3,8 +3,25 @@ import java.io._ import java.nio.channels.Channels import scala.annotation.tailrec -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} +val scalaTestVersion = "2.1.6" +val shapelessVersion = "2.0.0" +val shapelessScalaVersion = + if (scalaVersion == "2.11") + scalaVersion + else + scala.util.Properties.scalaPropOrElse("version.number", "unknown") def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -128,7 +145,7 @@ def generateShapelessSourceFile(testCount: Int, targetDir: File): File = { def compile(srcFile: String, classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir, srcFile) + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir, srcFile) val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -208,11 +225,11 @@ val scalatestJar = new File("scalatest_" + scalaVersion + "-" + scalaTestVersion 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") +val shapelessJar = new File("shapeless_" + scalaVersion + "-" + shapelessVersion + ".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) + downloadFile("http://oss.sonatype.org/content/repositories/releases/com/chuusai/shapeless_" + shapelessScalaVersion + "/" + shapelessVersion + "/shapeless_" + shapelessScalaVersion + "-" + shapelessVersion + ".jar", shapelessJar) -val baseDir = new File("shapelessTables") +val baseDir = new File("target/" + scalaVersion + "/shapelessTables") if (baseDir.exists) deleteDir(baseDir) @@ -348,4 +365,4 @@ finally { fileCountFile.flush() fileSizeFile.write("\n") fileSizeFile.flush() -} \ No newline at end of file +} diff --git a/supersafe.scala b/supersafe.scala new file mode 100644 index 0000000..eeaac93 --- /dev/null +++ b/supersafe.scala @@ -0,0 +1,399 @@ +import java.net._ +import java.io._ +import java.nio.channels.Channels +import scala.annotation.tailrec +import scala.math.pow + +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { + val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt +} +val scalaTest2Version = "2.3.0-SNAP3" +val scalaTest3Version = "3.0.0-SNAP5" + +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, fileNumber: Int, 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" + fileNumber + ".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" + fileNumber + " 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 +} + +def generateMultipleSourceFiles(testCount: Int, maxTestCount:Int, targetDir: File, packageName: String, importStatements: Array[String], + extendsName: String, withNames: Array[String], scopeBracket: Boolean, scopeDef: String, + testDefFun: (Int) => String, testBodyFun: (Int) => String): List[String] = { + targetDir.mkdirs() + val totalFiles = testCount / maxTestCount + val remainder = testCount % maxTestCount + + val sourceFiles = + if (totalFiles == 0) + List(generateSourceFile(testCount, targetDir, 0, packageName, importStatements, + extendsName, withNames, scopeBracket, scopeDef, testDefFun, testBodyFun).getAbsolutePath) + else + for (fileNumber <- 1 to totalFiles) yield + generateSourceFile(maxTestCount, targetDir, fileNumber, packageName, importStatements, + extendsName, withNames, scopeBracket, scopeDef, testDefFun, testBodyFun).getAbsolutePath + + // For remainder + if (remainder > 0) + List(generateSourceFile(remainder, targetDir, totalFiles + 1, packageName, importStatements, + extendsName, withNames, scopeBracket, scopeDef, testDefFun, testBodyFun).getAbsolutePath) ++ sourceFiles + else + sourceFiles.toList +} + +// Using assert(==) +def assert2TestBodyFun(x: Int): String = "assert(" + x + " + 1 == " + (x+1) + ")" +// Using assert(===) +def assert3TestBodyFun(x: Int): String = "assert(" + x + " + 1 === " + (x+1) + ")" +// Using should equal matchers +def shouldEqualTestBodyFun(x: Int): String = x + " + 1 should equal (" + (x+1) + ")" +// Using should be matchers +def shouldBeTestBodyFun(x: Int): String = x + " + 1 should be (" + (x+1) + ")" +// Using should be matchers +def shouldContainTestBodyFun(x: Int): String = "List(" + x + " + 1) should contain (" + (x+1) + ")" +// Using should equal matchers with logical and +def shouldLogicalAndEqualTestBodyFun(x: Int): String = x + " + 1 should (not equal " + x + " and not equal " + (x-1) + ")" +// Using should be matchers with logical and +def shouldLogicalAndBeTestBodyFun(x: Int): String = x + " + 1 should (not be " + x + " and not be " + (x-1) + ")" +// Using should contain matchers with logical and +def shouldLogicalAndContainTestBodyFun(x: Int): String = "List(" + x + " + 1) should (not contain " + x + " and not contain " + (x-1) + ")" + +// Spec +def specTestDefFun(x: Int): String = "def `increment " + x + "`" +// WordSpec +def wordSpecTestDefFun(x: Int): String = "\"increment " + x + "\" in" +//FunSuite +def funSuiteTestDefFun(x: Int): String = "test(\"increment " + x + "\")" +//FunSpec +def funSpecTestDefFun(x: Int): String = "it(\"increment " + x + "\")" +//FreeSpec +def freeSpecTestDefFun(x: Int): String = "\"increment " + x + "\" in" +//FlatSpec +def flatSpecTestDefFun(x: Int): String = "it should \"increment " + x + "\" in" + +def compile(srcFiles: List[String], classpath: String, targetDir: String) = { + import scala.collection.JavaConversions._ + + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir) ::: srcFiles + 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 " + srcFiles.mkString(", ") + "..." + + while (line != null) { + println (line) + line = stdout.readLine + } + + val end = System.currentTimeMillis + end - start +} + +def compileWithSuperSafe(srcFiles: List[String], classpath: String, targetDir: String) = { + import scala.collection.JavaConversions._ + + val command = List(scalaHome + "/bin/scalac", "-Xplugin:supersafe_2.11-1.1.0-SNAPSHOT.jar", "-classpath", classpath, "-d", targetDir) ::: srcFiles + 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 " + srcFiles.mkString(", ") + "..." + + 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 +} + +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) + +if (scalaVersion != "unknown") { + val scalatest2Jar = new File("scalatest-all_" + scalaVersion + "-" + scalaTest2Version + ".jar") + if (!scalatest2Jar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest-all_" + scalaVersion + "/" + scalaTest2Version + "/scalatest-all_" + scalaVersion + "-" + scalaTest2Version + ".jar", scalatest2Jar) + + val scalatest3Jar = new File("scalatest-all_" + scalaVersion + "-" + scalaTest3Version + ".jar") + if (!scalatest3Jar.exists) + downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalatest/scalatest-all_" + scalaVersion + "/" + scalaTest3Version + "/scalatest-all_" + scalaVersion + "-" + scalaTest3Version + ".jar", scalatest3Jar) + + val baseDir = new File("target/" + scalaVersion + "/supersafe") + 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 scalaTest2Classpath = scalatest2Jar.getName + val scalaTest3Classpath = scalatest3Jar.getName + + val baseOutputDir = new File(baseDir, "output") + baseOutputDir.mkdirs() + + val baseGeneratedDir = new File(baseDir, "generated") + baseGeneratedDir.mkdirs() + + val scalatest2Styles = + Array( + Style("scalatest2", "WordSpec", true, "\"Scala\" can ", wordSpecTestDefFun) + ) + + val scalatest2TestTypes: Array[TestType] = + Array( + TestType("equal", "equals", Array("org.scalatest._", "Matchers._"), Array.empty, shouldEqualTestBodyFun) + ) + + val supersafeStyles = + Array( + Style("supersafe", "WordSpec", true, "\"Scala\" can ", wordSpecTestDefFun) + ) + + val supersafeTestTypes: Array[TestType] = + Array( + TestType("equal", "equals", Array("org.scalatest._", "Matchers._"), Array.empty, shouldEqualTestBodyFun) + ) + + 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) + + scalatest2Styles.foreach { style => + scalatest2TestTypes.foreach { testType => + try { + durationFile.write(style.name + "-" + testType.shortName) // Don't write with MustMatchers to get all 4 names to fit on graph + durationFile.flush() + fileCountFile.write(style.name + "-" + testType.shortName) + fileCountFile.flush() + fileSizeFile.write(style.name + "-" + testType.shortName) + fileSizeFile.flush() + testCounts.foreach { testCount => + println("Working on " + style.className +" " + testType.name + " test count " + testCount + "...") + val outputDir = getOutputDir(baseOutputDir, testCount) + val generatedDir = new File(baseGeneratedDir, "generated-" + testCount) + + val generatedSrc = generateMultipleSourceFiles( + testCount, + 10, // maximum number of tests in a file + new File(generatedDir, "ScalaTest2" + style.className), // target dir + style.className + testType.shortName + "2", // 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, scalaTest2Classpath, outputDir.getAbsolutePath) + durationFile.write("," + duration) + durationFile.flush() + + val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, style.className + testType.shortName + "2")) + 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() + } + } + } + + supersafeStyles.foreach { style => + supersafeTestTypes.foreach { testType => + try { + durationFile.write(style.name + "-" + testType.shortName) // Don't write with MustMatchers to get all 4 names to fit on graph + durationFile.flush() + fileCountFile.write(style.name + "-" + testType.shortName) + fileCountFile.flush() + fileSizeFile.write(style.name + "-" + testType.shortName) + fileSizeFile.flush() + testCounts.foreach { testCount => + println("Working on " + style.className +" " + testType.name + " test count " + testCount + "...") + val outputDir = getOutputDir(baseOutputDir, testCount) + val generatedDir = new File(baseGeneratedDir, "generated-" + testCount) + + val generatedSrc = generateMultipleSourceFiles( + testCount, + 10, // maximum number of tests in a file + new File(generatedDir, "SuperSafe" + style.className), // target dir + style.className + testType.shortName + "3", // 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 = compileWithSuperSafe(generatedSrc, scalaTest2Classpath, outputDir.getAbsolutePath) + durationFile.write("," + duration) + durationFile.flush() + + val (fileCount, fileSize) = getFileAndByteCount(new File(outputDir, style.className + testType.shortName + "3")) + 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() +} +else + println("ERROR: Unable to detect Scala version.") diff --git a/tenTestsPerFile.scala b/tenTestsPerFile.scala index 495179d..d1be85b 100644 --- a/tenTestsPerFile.scala +++ b/tenTestsPerFile.scala @@ -4,19 +4,21 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/* -def scalaVersion = { +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt } -*/ -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val scalaTestVersion = "2.1.6" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -212,7 +214,7 @@ def generateMultipleSpecs2Immutable(testCount: Int, maxTestCount: Int, targetDir def compile(srcFiles: List[String], classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir) ::: srcFiles + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir) ::: srcFiles val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -284,7 +286,7 @@ if (scalaVersion != "unknown") { if (!specs2ScalazJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-core_" + scalaVersion + "/" + scalazVersion + "/scalaz-core_" + scalaVersion + "-" + scalazVersion + ".jar", specs2ScalazJar) - val baseDir = new File("tenTestsPerFile") + val baseDir = new File("target/" + scalaVersion + "/tenTestsPerFile") if (baseDir.exists) deleteDir(baseDir) diff --git a/testsIn100Files.scala b/testsIn100Files.scala index af9b4e1..42d5521 100644 --- a/testsIn100Files.scala +++ b/testsIn100Files.scala @@ -4,19 +4,21 @@ import java.nio.channels.Channels import scala.annotation.tailrec import scala.math.pow -/* -def scalaVersion = { +val scalaHome = { + val home = scala.util.Properties.scalaHome + println("Scala Home: " + home) + home +} + +val scalaVersion = { val rawVersion = scala.util.Properties.scalaPropOrElse("version.number", "unknown") - if (rawVersion.endsWith(".final")) - rawVersion.substring(0, rawVersion.length - 6) - else - rawVersion + println("Detected Scala version: " + rawVersion) + val versionParts = rawVersion.split("\\.") + versionParts(0).toInt + "." + versionParts(1).toInt } -*/ -def scalaVersion = "2.10" -val scalaTestVersion = "2.0" -val specs2Version = "2.3.4" -val scalazVersion = "7.0.4" +val scalaTestVersion = "2.1.6" +val specs2Version = "2.3.11" +val scalazVersion = "7.0.6" def downloadFile(urlString: String, targetFile: File) { println("Downloading " + urlString) @@ -181,7 +183,7 @@ def generateMultipleSpecs2Immutable(testCount: Int, targetDir: File): List[Strin def compile(srcFiles: List[String], classpath: String, targetDir: String) = { import scala.collection.JavaConversions._ - val command = List("scalac", "-classpath", classpath, "-d", targetDir) ::: srcFiles + val command = List(scalaHome + "/bin/scalac", "-classpath", classpath, "-d", targetDir) ::: srcFiles val builder = new ProcessBuilder(command) builder.redirectErrorStream(true) val start = System.currentTimeMillis @@ -253,7 +255,7 @@ if (scalaVersion != "unknown") { if (!specs2ScalazJar.exists) downloadFile("https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-core_" + scalaVersion + "/" + scalazVersion + "/scalaz-core_" + scalaVersion + "-" + scalazVersion + ".jar", specs2ScalazJar) - val baseDir = new File("testsIn100Files") + val baseDir = new File("target/" + scalaVersion + "/testsIn100Files") if (baseDir.exists) deleteDir(baseDir)