Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何测量Cats IO效应中经过的时间?_Scala_Functional Programming_Scala Cats_Cats Effect - Fatal编程技术网

Scala 如何测量Cats IO效应中经过的时间?

Scala 如何测量Cats IO效应中经过的时间?,scala,functional-programming,scala-cats,cats-effect,Scala,Functional Programming,Scala Cats,Cats Effect,我想测量IO容器中经过的时间。使用普通调用或期货相对容易(例如,类似下面的代码) 但不知道如何包装IO调用 def io_meter[T](effect: IO[T]): IO[T] = { val start = System.currentTimeMillis() ??? } def io_call(): IO[String] = IO.pure { Thread.sleep(500) "hello" } test("metered i

我想测量IO容器中经过的时间。使用普通调用或期货相对容易(例如,类似下面的代码)

但不知道如何包装IO调用

  def io_meter[T](effect: IO[T]): IO[T] = {
    val start = System.currentTimeMillis()
    ???
  }

  def io_call(): IO[String] = IO.pure {
    Thread.sleep(500)
    "hello"
  }

  test("metered io call") {
    whenReady(meter(call()), timeout(Span(550, Milliseconds))) { s =>
      s should be("hello")
    }
  }
谢谢大家!

Cats effect有一个实现,它允许纯粹的时间测量,当您只想模拟时间的流逝时,还可以注入您自己的实现进行测试。他们文档中的示例如下:

def度量值[F[_],A](fa:F[A])
(隐式F:Sync[F],clock:clock[F]):F[(A,Long)]={
为了{

开始这并不完全等同于OP的
Future
实现,尽管它是
Future。onComplete
会延长失败案例的时间
  def io_meter[T](effect: IO[T]): IO[T] = {
    val start = System.currentTimeMillis()
    ???
  }

  def io_call(): IO[String] = IO.pure {
    Thread.sleep(500)
    "hello"
  }

  test("metered io call") {
    whenReady(meter(call()), timeout(Span(550, Milliseconds))) { s =>
      s should be("hello")
    }
  }