如何测量和显示单个测试的运行时间? [英] How to measure and display the running time of a single test?

查看:91
本文介绍了如何测量和显示单个测试的运行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能使用 scalatest 编写的长期运行的测试:

I have a potentially long-running test written with scalatest:

test("a long running test") {
  failAfter(Span(60, Seconds)) {
    // ...
  }
}

即使测试在超时限制内完成,它的运行时间对运行测试的人也很有价值。如何在scalatest的输出中测量和显示此单个特定测试的运行时间?

Even if the test finishes within the timeout limit, its running time can be valuable to the person who runs the test. How can I measure and display the running time of this single particular test in scalatest's output?

更新:当前我用自己的函数来衡量运行时间,就像r.v的答案一样。我想知道 scalatest 是否已提供此功能。

Update: Currently I measure the running time with my own function, just as in r.v's answer. I'd like to know if scalatest offers this functionality already.

推荐答案

-oD 选项将给出测试的持续时间。例如,我在build.sbt中使用以下命令。

the -oD option will give the duration of the test. For example, I use the following in my build.sbt.

testOptions in Test += Tests.Argument("-oD")

编辑:

您还可以单独运行以下内容:

You can also use the following for individual runs:

> test-only org.acme.RedSuite -- -oD

请参见http://www.scalatest.org/user_guide/using_scalatest_with_sbt

此外,您可以定义以下是用于一般时间测量的函数:

Moreover, you can define the following function for general time measurements:

def time[T](str: String)(thunk: => T): T = {
  print(str + "... ")
  val t1 = System.currentTimeMillis
  val x = thunk
  val t2 = System.currentTimeMillis
  println((t2 - t1) + " msecs")
  x
}

并在任何地方使用它(不依赖于ScalaTest)

and use it anywhere (not dependent on ScalaTest)

test("a long running test") {
  time("test running"){
    failAfter(Span(60, Seconds)) {
    // ...
  }
}

这篇关于如何测量和显示单个测试的运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆