Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using simple-build-tool for benchmarks

Tags:

scala

sbt

I'm trying to get sbt to compile and build some benchmarks. I've told it to add the benchmarks to the test path so they're recompiled along with tests, but I can't figure out how to write an action to let me actually run them. Is it possible to invoke classes from the Project definition class, or even just from the command line?

like image 816
Dial Z Avatar asked Mar 04 '26 07:03

Dial Z


1 Answers

Yes, it is.

If you'd like to run them in the same VM the SBT is run in, then write a custom task similar to the following in your project definition file:

  lazy val benchmark = task {
    // code to run benchmarks
    None // Some("will return an error message")
  }

Typing benchmark in SBT console will run the task above. To actually run the benchmarks, or, for that matter, any other class you've compiled, you can reuse some of the existing infrastructure of SBT, namely the method runTask which will create a task that runs something for you. It has the following signature:

 def runTask(mainClass: => Option[String], classpath: PathFinder, options: String*): Task

Simply add the following to your file:

  lazy val benchmark = task { args =>
    runTask(Some("whatever.your.mainclass.is"), testClasspath, args)
  }

When running benchmarks, it is sometimes recommended that you run them in a separate jvm invocation, to get more reliable results. SBT allows you to run separate processes by invoking a method ! on a string command. Say you have a command java -jar path-to-artifact.jar you want to run. Then:

"java -jar path-to-artifact.jar" !

runs the command in SBT. You want to put the snippet above in a separate task, same as earlier.

And don't forget to reload when you change your project definition.

like image 175
axel22 Avatar answered Mar 05 '26 23:03

axel22