• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Timer类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala JString类代码示例发布时间:2022-05-23
下一篇:
Scala Set类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap