6

これは私が今試していることですが、メトリックではなく「ちょっと」のみを出力します。メイン関数にメトリック関連のものを追加したくありません。

import java.util.Date

import monix.eval.Task
import monix.execution.Scheduler.Implicits.global

import scala.concurrent.Await
import scala.concurrent.duration.Duration

class A {
  def fellow(): Task[Unit] = {
    val result = Task {
      println("hey")
      Thread.sleep(1000)
    }
    result
  }
}

trait AA extends A {
  override def fellow(): Task[Unit] = {
    println("AA")
    val result = super.fellow()
    val start = new Date()
    result.foreach(e => {
      println("AA", new Date().getTime - start.getTime)
    })
    result
  }
}

val a = new A with AA
val res: Task[Unit] = a.fellow()
Await.result(res.runAsync, Duration.Inf)
4

2 に答える 2

2

@Pierreが述べたように、Monixタスクの最新バージョンにはTask.timed、次のことができます

timed <- task.timed
(duration, t) = timed
于 2020-12-21T22:20:44.087 に答える