Skip to content

Instantly share code, notes, and snippets.

@OlegYch
Last active July 18, 2023 14:50
Show Gist options
  • Save OlegYch/a31a4a5c6195fc630b87bd823bc952bb to your computer and use it in GitHub Desktop.
Save OlegYch/a31a4a5c6195fc630b87bd823bc952bb to your computer and use it in GitHub Desktop.
get compile times by file/dir in sbt
val compileTimes = inputKey[Unit]("Get compilation time for each folder")
compileTimes / aggregate := false,
compileTimes := {
import complete.DefaultParsers._
// val ((depth, _), filter) = (NatBasic ~ " " ~ StringBasic).parsed
val (depth, filter) = spaceDelimited("depth filter").parsed match {
case Seq(depth, filter) => (depth.toInt, filter)
case _ => sys.error("expected depth and filter arguments, eg '2 /'")
}
val s = state.value
s.log.info(s"depth $depth, filter $filter")
val extracted = Project.extract(s)
extracted.runTask(Compile / compile, s)
val source = (Compile / scalaSource).value
val files = ((source / filter) ** "*.scala").get()
s.log.info(s"Running on ${files.size} files")
def pathParts(f: File) = f.relativeTo(source / filter).get.getPath.split(java.io.File.separatorChar)
val times = files.groupBy(pathParts(_).take(depth).mkString("/")).toSeq.sortBy(_._1).map { case (path, files) =>
def runCompile = {
val start = System.currentTimeMillis()
extracted.runTask(Compile / compile, s)
val time = System.currentTimeMillis() - start
s.log.info(s"compiled ${path} in $time ms")
time
}
files.foreach(f => IO.append(f, " "))
val time1 = runCompile
files.foreach(f => IO.write(f, IO.readBytes(f).dropRight(1)))
val time2 = runCompile
(path, time1.min(time2))
}
s.log.info(times.sortBy(_._2).mkString("\n"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment