A
A
Artem2014-11-26 17:17:58
Scala
Artem, 2014-11-26 17:17:58

How to monitor the execution of methods in Scala?

There is some Scala code.

def computations1(params: AnyRef*): Future[ComputaionResults] ={
    //computations
  }

  def computations2(params: AnyRef*): Future[ComputaionResults] ={
    //computations
  }
  
  for{
    results1 <- computations1()
    results2 <- computations2()
  } yield {
    //do with results
  }

How to monitor (timing) the methods being executed? Those. I need to find out in production how long the execution of methods took.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2014-12-04
@sergey-gornostaev

def time[R](block: => R): R = {
    val t0 = System.nanoTime()
    val result = block    // call-by-name
    val t1 = System.nanoTime()
    println("Elapsed time: " + (t1 - t0) + "ns")
    result
}

// Now wrap your method calls, for example change this...
val result = 1 to 1000 sum

// ... into this
val result = time { 1 to 1000 sum }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question