Timer

nl.grons.metrics4.scala.Timer
class Timer(val metric: Timer)

A Scala facade class for DropwizardTimer.

Features:

  • measure the execution duration of a block of code with time
  • measure the time until a future is completed with timeFuture
  • add an execution duration measurement as a side effect to a partial function with timePF
  • direct access to the underlying timer with update, timerContext, count, max, etc.

Example usage:

 class Example(val db: Db) extends Instrumented {
   private[this] val loadTimer = metrics.timer("load")

   def load(id: Long) = loadTimer.time {
     db.load(id)
   }
 }

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def count: Long

The number of durations recorded.

The number of durations recorded.

Attributes

def fifteenMinuteRate: Double

The fifteen-minute rate of timings.

The fifteen-minute rate of timings.

Attributes

def fiveMinuteRate: Double

The five-minute rate of timings.

The five-minute rate of timings.

Attributes

def max: Long

The longest recorded duration in nanoseconds.

The longest recorded duration in nanoseconds.

Attributes

def mean: Double

The arithmetic mean of all recorded durations in nanoseconds.

The arithmetic mean of all recorded durations in nanoseconds.

Attributes

def meanRate: Double

The mean rate of timings.

The mean rate of timings.

Attributes

def min: Long

The shortest recorded duration in nanoseconds.

The shortest recorded duration in nanoseconds.

Attributes

def oneMinuteRate: Double

The one-minute rate of timings.

The one-minute rate of timings.

Attributes

def snapshot: Snapshot

A snapshot of the values in the timer's sample.

A snapshot of the values in the timer's sample.

Attributes

def stdDev: Double

The standard deviation of all recorded durations.

The standard deviation of all recorded durations.

Attributes

def time[A](f: => A): A

Runs f, recording its duration, and returns its result.

Runs f, recording its duration, and returns its result.

Attributes

def timeFuture[A](future: => Future[A])(implicit context: ExecutionContext): Future[A]

Measures 'now' up to the moment that the given future completes, then updates this timer with the measurement.

Measures 'now' up to the moment that the given future completes, then updates this timer with the measurement.

Know what you measure

This method may measure more than is obvious. It measures:

  • the evaluation of the (by name) parameter future
  • in case the future is not yet completed: the delay until the constructed Future is scheduled in the given ExecutionContext
  • in case the future is not yet completed: the actual execution of the Future
  • the time it takes to schedule stopping the timer

To only measure the Future execution time, please use a timer in the code that is executed inside the Future.

The timer is stopped concurrently to the returned future. If you need to verify the timer's value in a unit test you can use something like ScalaTest's eventually, or use a direct execution context. For more information see [https://github.com/erikvanoosten/metrics-scala/pull/144].

Example usage:

class Example extends Instrumented {
 private[this] loadTimer = metrics.timer("loading")

 private def asyncFetchRows(): Future[Seq[Row]] = ...

 def loadStuffEventually(): Future[Seq[Row]] = loadTimer.timeFuture { asyncFetchRows() }
}

Type parameters

A

future result type

Value parameters

context

execution context

future

the expression that results in a future

Attributes

Returns

the result of executing future

def timePF[A, B](pf: PartialFunction[A, B]): PartialFunction[A, B]

Converts partial function pf into a side-effecting partial function that times every invocation of pf for which it is defined. The result is passed unchanged.

Converts partial function pf into a side-effecting partial function that times every invocation of pf for which it is defined. The result is passed unchanged.

Example usage:

class Example extends Instrumented {
  val isEven: PartialFunction[Int, String] = {
    case x if x % 2 == 0 => x+" is even"
  }

  val isEvenTimer = metrics.timer("isEven")
  val timedIsEven: PartialFunction[Int, String] = isEvenTimer.timePF(isEven)

  val sample = 1 to 10
  sample collect timedIsEven   // timer does 5 measurements
}

Attributes

def timerContext(): Context

A timing com.codahale.metrics.Timer.Context, which measures an elapsed time in nanoseconds.

A timing com.codahale.metrics.Timer.Context, which measures an elapsed time in nanoseconds.

Attributes

def update(duration: FiniteDuration): Unit

Adds a recorded duration.

Adds a recorded duration.

Attributes

def update(duration: Long, unit: TimeUnit): Unit

Adds a recorded duration.

Adds a recorded duration.

Attributes