Scala 如何测量Cats IO效应中经过的时间?
我想测量IO容器中经过的时间。使用普通调用或期货相对容易(例如,类似下面的代码) 但不知道如何包装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
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")
}
}