当前位置: 首页>>代码示例>>Scala>>正文


Scala Timer类代码示例

本文整理汇总了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
      }
    }
} 
开发者ID:ayush03agarwal,项目名称:connekt,代码行数:20,代码来源:MetricsDirectives.scala

示例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)
  }
} 
开发者ID:htimur,项目名称:metrics-annotation-play,代码行数:23,代码来源:TimedInterceptor.scala

示例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))
    }
  }
} 
开发者ID:evolution-gaming,项目名称:metric-tools,代码行数:58,代码来源:MetricHelper.scala


注:本文中的com.codahale.metrics.Timer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。