本文整理汇总了Scala中com.codahale.metrics.Timer类的典型用法代码示例。如果您正苦于以下问题:Scala Timer类的具体用法?Scala Timer怎么用?Scala Timer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Timer类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: meteredResource
//设置package包名称以及导入依赖的类
package com.flipkart.connekt.receptors.directives
import akka.http.scaladsl.server.Directive0
import akka.http.scaladsl.server.directives.BasicDirectives
import com.codahale.metrics.Timer
import com.flipkart.connekt.commons.metrics.Instrumented
trait MetricsDirectives extends BasicDirectives with Instrumented {
def meteredResource(resourceId: String): Directive0 =
extractRequestContext.flatMap { ctx =>
val context: Timer.Context = registry.timer(getMetricName(resourceId)).time()
mapResponse { r =>
context.stop()
counter(s"$resourceId.${r.status.intValue()}").inc()
r
}
}
}
示例2: TimedInterceptor
//设置package包名称以及导入依赖的类
package de.khamrakulov.play.metrics.annotation.guice.interceptor
import java.util.concurrent.TimeUnit
import com.codahale.metrics.Timer
import org.aopalliance.intercept.{MethodInterceptor, MethodInvocation}
private[annotation] object TimedInterceptor {
def apply(timer: Timer) = new TimedInterceptor(timer)
}
private[annotation] class TimedInterceptor(timer: Timer) extends MethodInterceptor {
override def invoke(invocation: MethodInvocation): AnyRef = {
// Since these timers are always created via the default ctor (via MetricRegister#timer), they always use
// nanoTime, so we can save an allocation here by not using Context.
val start: Long = System.nanoTime
try
invocation.proceed
finally timer.update(System.nanoTime - start, TimeUnit.NANOSECONDS)
}
}
示例3: MetricHelper
//设置package包名称以及导入依赖的类
package com.evolutiongaming.util
import com.codahale.metrics.{Gauge, Histogram, MetricRegistry, Timer}
import scala.compat.Platform
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal
object MetricHelper {
object GaugeF {
def apply[T](f: => T): Gauge[T] = new Gauge[T] { def getValue: T = f }
}
implicit class RichTimer(val timer: Timer) extends AnyVal {
def timeExceed[T](limit: FiniteDuration)(f: => T): T = {
val start = Platform.currentTime
try f finally {
val stop = Platform.currentTime
val duration = stop - start
if (duration >= limit.toMillis) timer.update(duration, MILLISECONDS)
}
}
def timeFuture[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] = {
val time = timer.time()
try f andThen { case _ => time.stop() } catch {
case NonFatal(e) => time.stop(); throw e
}
}
def timeFunc[T](f: => T): T = {
val time = timer.time()
try f finally time.stop()
}
}
implicit class HistogramOps(val histogram: Histogram) extends AnyVal {
def timeFunc[T](f: => T): T = {
val start = Platform.currentTime
try f finally histogram.update(Platform.currentTime - start)
}
def timeFuture[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] = {
val start = Platform.currentTime
f andThen { case _ => histogram.update(Platform.currentTime - start) }
}
}
implicit class MetricRegistryOps(val self: MetricRegistry) extends AnyVal {
def gauge[T](name: String, f: => T): Gauge[T] = {
self remove name
self.register(name, GaugeF(f))
}
}
}