|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 Contributor. All rights reserved. |
| 3 | + */ |
| 4 | +package scala.tools.nsc.util |
| 5 | + |
| 6 | +import scala.reflect.io.AbstractFile |
| 7 | +import scala.tools.nsc.Settings |
| 8 | +import scala.tools.nsc.settings.ClassPathRepresentationType |
| 9 | +import scala.tools.util.PathResolverFactory |
| 10 | + |
| 11 | +/** |
| 12 | + * Simple application to compare efficiency of the recursive and the flat classpath representations |
| 13 | + */ |
| 14 | +object ClassPathImplComparator { |
| 15 | + |
| 16 | + private class TestSettings extends Settings { |
| 17 | + val checkClasses = PathSetting("-checkClasses", "Specify names of classes which should be found separated with ;", "") |
| 18 | + val requiredIterations = IntSetting("-requiredIterations", |
| 19 | + "Repeat tests specified number of times (to check e.g. impact of caches)", 1, Some((1, Int.MaxValue)), (_: String) => None) |
| 20 | + val cpCreationRepetitions = IntSetting("-cpCreationRepetitions", |
| 21 | + "Repeat tests specified number of times (to check e.g. impact of caches)", 1, Some((1, Int.MaxValue)), (_: String) => None) |
| 22 | + val cpLookupRepetitions = IntSetting("-cpLookupRepetitions", |
| 23 | + "Repeat tests specified number of times (to check e.g. impact of caches)", 1, Some((1, Int.MaxValue)), (_: String) => None) |
| 24 | + } |
| 25 | + |
| 26 | + private class DurationStats(name: String) { |
| 27 | + private var sum = 0L |
| 28 | + private var iterations = 0 |
| 29 | + |
| 30 | + def noteMeasuredTime(millis: Long): Unit = { |
| 31 | + sum += millis |
| 32 | + iterations += 1 |
| 33 | + } |
| 34 | + |
| 35 | + def printResults(): Unit = { |
| 36 | + val avg = if (iterations == 0) 0 else sum.toDouble / iterations |
| 37 | + println(s"$name - total duration: $sum ms; iterations: $iterations; avg: $avg ms") |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private lazy val defaultClassesToFind = List( |
| 42 | + "scala.collection.immutable.List", |
| 43 | + "scala.Option", |
| 44 | + "scala.Int", |
| 45 | + "scala.collection.immutable.Vector", |
| 46 | + "scala.util.hashing.MurmurHash3" |
| 47 | + ) |
| 48 | + |
| 49 | + private val oldCpCreationStats = new DurationStats("Old classpath - create") |
| 50 | + private val oldCpSearchingStats = new DurationStats("Old classpath - search") |
| 51 | + |
| 52 | + private val flatCpCreationStats = new DurationStats("Flat classpath - create") |
| 53 | + private val flatCpSearchingStats = new DurationStats("Flat classpath - search") |
| 54 | + |
| 55 | + def main(args: Array[String]): Unit = { |
| 56 | + |
| 57 | + if (args contains "-help") |
| 58 | + usage() |
| 59 | + else { |
| 60 | + val oldCpSettings = loadSettings(args.toList, ClassPathRepresentationType.Recursive) |
| 61 | + val flatCpSettings = loadSettings(args.toList, ClassPathRepresentationType.Flat) |
| 62 | + |
| 63 | + val classesToCheck = oldCpSettings.checkClasses.value |
| 64 | + val classesToFind = |
| 65 | + if (classesToCheck.isEmpty) defaultClassesToFind |
| 66 | + else classesToCheck.split(";").toList |
| 67 | + |
| 68 | + def doTest(classPath: => ClassFileLookup[AbstractFile], cpCreationStats: DurationStats, cpSearchingStats: DurationStats, |
| 69 | + cpCreationRepetitions: Int, cpLookupRepetitions: Int)= { |
| 70 | + |
| 71 | + def createClassPaths() = (1 to cpCreationRepetitions).map(_ => classPath).last |
| 72 | + def testClassLookup(cp: ClassFileLookup[AbstractFile]): Boolean = (1 to cpCreationRepetitions).foldLeft(true) { |
| 73 | + case (a, _) => a && checkExistenceOfClasses(classesToFind)(cp) |
| 74 | + } |
| 75 | + |
| 76 | + val cp = withMeasuredTime("Creating classpath", createClassPaths(), cpCreationStats) |
| 77 | + val result = withMeasuredTime("Searching for specified classes", testClassLookup(cp), cpSearchingStats) |
| 78 | + println(s"The end of the test case. All expected classes found = $result \n") |
| 79 | + } |
| 80 | + |
| 81 | + (1 to oldCpSettings.requiredIterations.value) foreach { iteration => |
| 82 | + if (oldCpSettings.requiredIterations.value > 1) |
| 83 | + println(s"Iteration no $iteration") |
| 84 | + |
| 85 | + println("Recursive (old) classpath representation:") |
| 86 | + doTest(PathResolverFactory.create(oldCpSettings).result, oldCpCreationStats, oldCpSearchingStats, |
| 87 | + oldCpSettings.cpCreationRepetitions.value, oldCpSettings.cpLookupRepetitions.value) |
| 88 | + |
| 89 | + println("Flat classpath representation:") |
| 90 | + doTest(PathResolverFactory.create(flatCpSettings).result, flatCpCreationStats, flatCpSearchingStats, |
| 91 | + flatCpSettings.cpCreationRepetitions.value, flatCpSettings.cpLookupRepetitions.value) |
| 92 | + } |
| 93 | + |
| 94 | + if (oldCpSettings.requiredIterations.value > 1) { |
| 95 | + println("\nOld classpath - summary") |
| 96 | + oldCpCreationStats.printResults() |
| 97 | + oldCpSearchingStats.printResults() |
| 98 | + |
| 99 | + println("\nFlat classpath - summary") |
| 100 | + flatCpCreationStats.printResults() |
| 101 | + flatCpSearchingStats.printResults() |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Prints usage information |
| 108 | + */ |
| 109 | + private def usage(): Unit = |
| 110 | + println("""Use classpath and sourcepath options like in the case of e.g. 'scala' command. |
| 111 | + | There are also two additional options: |
| 112 | + | -checkClasses <semicolon separated class names> Specify names of classes which should be found |
| 113 | + | -requiredIterations <int value> Repeat tests specified count of times (to check e.g. impact of caches) |
| 114 | + | Note: Option -YclasspathImpl will be set automatically for each case. |
| 115 | + """.stripMargin.trim) |
| 116 | + |
| 117 | + private def loadSettings(args: List[String], implType: String) = { |
| 118 | + val settings = new TestSettings() |
| 119 | + settings.processArguments(args, processAll = true) |
| 120 | + settings.YclasspathImpl.value = implType |
| 121 | + if (settings.classpath.isDefault) |
| 122 | + settings.classpath.value = sys.props("java.class.path") |
| 123 | + settings |
| 124 | + } |
| 125 | + |
| 126 | + private def withMeasuredTime[T](operationName: String, f: => T, durationStats: DurationStats): T = { |
| 127 | + val startTime = System.currentTimeMillis() |
| 128 | + val res = f |
| 129 | + val elapsed = System.currentTimeMillis() - startTime |
| 130 | + durationStats.noteMeasuredTime(elapsed) |
| 131 | + println(s"$operationName - elapsed $elapsed ms") |
| 132 | + res |
| 133 | + } |
| 134 | + |
| 135 | + private def checkExistenceOfClasses(classesToCheck: Seq[String])(classPath: ClassFileLookup[AbstractFile]): Boolean = |
| 136 | + classesToCheck.foldLeft(true) { |
| 137 | + case (res, classToCheck) => |
| 138 | + val found = classPath.findClass(classToCheck).isDefined |
| 139 | + if (!found) |
| 140 | + println(s"Class $classToCheck not found") // of course in this case the measured time will be affected by IO operation |
| 141 | + found |
| 142 | + } |
| 143 | +} |
0 commit comments