使用 simple-build-tool 进行基准测试

发布于 2024-09-28 06:50:20 字数 106 浏览 3 评论 0原文

我正在尝试让 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平安喜乐 2024-10-05 06:50:21

是的。

如果您想在运行 SBT 的同一虚拟机中运行它们,请在项目定义文件中编写类似于以下内容的自定义任务:

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

在 SBT 控制台中键入 benchmark 将运行上面的任务。要实际运行基准测试,或者您编译的任何其他类,您可以重用 SBT 的一些现有基础结构,即方法 runTask ,它将创建一个运行某些内容的任务为你。它具有以下签名:

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

只需将以下内容添加到您的文件中:

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

运行基准测试时,有时建议您在单独的 jvm 调用中运行它们,以获得更可靠的结果。 SBT 允许您通过在字符串命令上调用方法 ! 来运行单独的进程。假设您有一个要运行的命令 java -jar path-to-artifact.jar 。然后:

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

在 SBT 中运行命令。您想将上面的代码片段放在一个单独的任务中,与之前一样。

当您更改项目定义时,不要忘记重新加载

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.

樱娆 2024-10-05 06:50:21

难道您不能简单地将基准编写为测试,以便当您在 SBT 中调用“测试”时运行它们吗?

您还可以使用“test-only”运行特定测试,或者使用“run”或“exec”运行主程序(请参阅http://code.google.com/p/simple-build-tool/wiki/RunningSbt 了解详细信息)。

Couldn't you simply write the benchmarks as tests, so they will be run when you call 'test' in SBT?

You could also run a specific test with 'test-only', or run a main with 'run' or 'exec' (see http://code.google.com/p/simple-build-tool/wiki/RunningSbt for details).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文