How to profile the time spent for a task in SBT

2020-02-26 02:03发布

I'd like to know why sometimes my build is much slower.

So I've decided to measure the time of long-running taks.

When running pure Scala code, it is quite easy to do so:

def myMethod() = {
   val initTime = System.currentTimeMillis
   ...
   val elapsedTime = System.currentTimeMillis-initTime
}

But for tasks like packageBin or compile, whose source code I cannot change, I don't know how to measure it, because I cannot control when someTask.value is run.

Any hint?

Related questions:

标签: scala sbt
2条回答
我命由我不由天
2楼-- · 2020-02-26 02:23

add -Dsbt.task.timings=true to your JAVA_OPTS when launching sbt

查看更多
够拽才男人
3楼-- · 2020-02-26 02:34

For a more complete analysis, you can also use jrudolph/sbt-optimizer/

sbt-optimizer is an experimental plugin that hooks into sbt's task execution engine and offers a graphical ASCII report once a tree of tasks has been run

Add the plugin to project/plugins.sbt:

addSbtPlugin("net.virtual-void" % "sbt-optimizer" % "0.1.2")

and enable it in a project with:

enablePlugins(net.virtualvoid.optimizer.SbtOptimizerPlugin)

https://raw.githubusercontent.com/jrudolph/sbt-optimizer/master/docs/example-line.png

Each output line corresponds to one task that has been executed.

  • The first time is the total time this task was running.
  • The second time displayed in green is the actual execution time.
  • The third time displayed in red is time the task wanted to run but was waiting for the global ivy lock.
  • The last time displayed in cyan is the time the task was blocked waiting for Ivy downloads.
查看更多
登录 后发表回答