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


Scala duration类代码示例

本文整理汇总了Scala中scala.concurrent.duration的典型用法代码示例。如果您正苦于以下问题:Scala duration类的具体用法?Scala duration怎么用?Scala duration使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了duration类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: dockerContainers

//设置package包名称以及导入依赖的类
package ru.svd.medan.infrastructure

import com.whisk.docker.{DockerContainer, DockerKit, DockerReadyChecker}
import com.whisk.docker.impl.dockerjava.DockerKitDockerJava

import scala.concurrent.{Await, duration}
import scala.concurrent.duration.Duration


trait MongoDocketTestKit extends DockerKit with DockerKitDockerJava {
  val MONGO_INTERNAL_PORT = 27017

  val mongodbContainer: DockerContainer = DockerContainer("mongo:3.0.15")
    .withPorts(MONGO_INTERNAL_PORT -> None)
    .withReadyChecker(DockerReadyChecker.LogLineContains("waiting for connections on port"))
    .withCommand("mongod", "--nojournal", "--smallfiles", "--syncdelay", "0")

  override def dockerContainers: List[DockerContainer] = mongodbContainer :: super.dockerContainers

  def mongoPort: Int = {
    val ports = Await.result(mongodbContainer.getPorts(), Duration(5, duration.SECONDS))
    ports(MONGO_INTERNAL_PORT)
  }

  def mongoStart(): Unit = {
    startAllOrFail()
    Await.ready(mongodbContainer.isReady(), Duration(15, duration.SECONDS))
  }

  def mongoStop(): Unit = {
    stopAllQuietly()
  }
} 
开发者ID:severiand,项目名称:medan,代码行数:34,代码来源:MongoDocketTestKit.scala

示例2: recordResponseTime

//设置package包名称以及导入依赖的类
package com.lonelyplanet.prometheus.directives

import akka.http.scaladsl.server.ExceptionHandler
import akka.http.scaladsl.server.directives.{BasicDirectives, ExecutionDirectives}
import com.lonelyplanet.prometheus.ResponseTimeRecorder

import scala.concurrent.duration
import scala.concurrent.duration.FiniteDuration
import scala.util.control.NonFatal

trait ResponseTimeRecordingDirectives {
  this: ResponseTimeRecorderProvider =>

  
  def recordResponseTime(endpoint: String) = BasicDirectives.extractRequestContext.flatMap { ctx =>
    val requestStartTime = System.nanoTime()
    BasicDirectives.mapResponse { resp =>
      record(endpoint, requestStartTime)
      resp
    } & ExecutionDirectives.handleExceptions {
      responseTimeRecordingExceptionHandler(endpoint, requestStartTime)
    }
  }

  private def responseTimeRecordingExceptionHandler(endpoint: String, requestStartTime: Long) = ExceptionHandler {
    case NonFatal(e) =>
      record(endpoint, requestStartTime)

      // Rethrow the exception to allow proper handling
      // from handlers higher ip in the hierarchy
      throw e
  }

  private def record(endpoint: String, requestStartTime: Long): Unit = {
    val requestEndTime = System.nanoTime()
    val total = new FiniteDuration(requestEndTime - requestStartTime, duration.NANOSECONDS)

    recorder.recordResponseTime(endpoint, total)
  }
}

object ResponseTimeRecordingDirectives {
  def apply(r: ResponseTimeRecorder) = {
    new ResponseTimeRecordingDirectives with ResponseTimeRecorderProvider {
      override def recorder: ResponseTimeRecorder = r
    }
  }
}

trait ResponseTimeRecorderProvider {
  def recorder: ResponseTimeRecorder
} 
开发者ID:lonelyplanet,项目名称:prometheus-akka-http,代码行数:53,代码来源:ResponseTimeRecordingDirectives.scala

示例3: recordResponseTime

//设置package包名称以及导入依赖的类
package com.lonelyplanet.prometheus

import io.prometheus.client.{CollectorRegistry, Histogram}

import scala.concurrent.duration
import scala.concurrent.duration.{FiniteDuration, TimeUnit}

trait ResponseTimeRecorder {
  def recordResponseTime(endpoint: String, responseTime: FiniteDuration): Unit
}


class PrometheusResponseTimeRecorder(
    metricName: String,
    metricHelp: String,
    buckets: List[Double],
    endpointLabelName: String,
    registry: CollectorRegistry,
    timeUnit: TimeUnit
) extends ResponseTimeRecorder {

  private val responseTimes = buildHistogram.register(registry)

  override def recordResponseTime(endpoint: String, responseTime: FiniteDuration): Unit = {
    responseTimes.labels(endpoint).observe(responseTime.toUnit(timeUnit))
  }

  private def buildHistogram = Histogram.build()
    .name(metricName)
    .help(metricHelp)
    .labelNames(endpointLabelName)
    .buckets(buckets: _*)

}

object PrometheusResponseTimeRecorder {
  val DefaultBuckets = List(.01, .025, .05, .075, .10, .125, .15, .175, .20, .225, .25, .275,
    .30, .325, .35, .40, .45, .50, .60, .70, 1.0, 2.0, 3.0, 5.0, 10.0)
  val DefaultMetricName = "request_processing_seconds"
  val DefaultMetricHelp = "Time spent processing request"
  val DefaultEndpointLabel = "endpoint"
  val DefaultTimeUnit = duration.SECONDS

  lazy val DefaultRegistry = CollectorRegistry.defaultRegistry

  lazy val Default = {
    new PrometheusResponseTimeRecorder(
      DefaultMetricName,
      DefaultMetricHelp,
      DefaultBuckets,
      DefaultEndpointLabel,
      DefaultRegistry,
      DefaultTimeUnit
    )
  }
}

class NoOpResponseTimeRecorder extends ResponseTimeRecorder {
  def recordResponseTime(endpoint: String, responseTime: FiniteDuration): Unit = ()
} 
开发者ID:lonelyplanet,项目名称:prometheus-akka-http,代码行数:61,代码来源:ResponseTimeRecorder.scala

示例4: PrometheusResponseTimeRecorderSpec

//设置package包名称以及导入依赖的类
package com.lonelyplanet.prometheus

import io.prometheus.client.{Collector, CollectorRegistry}
import org.scalamock.scalatest.MockFactory
import org.scalatest.{FlatSpec, Matchers}
import com.lonelyplanet.prometheus.Utils._

import scala.concurrent.duration
import scala.concurrent.duration.FiniteDuration
import scala.util.Random

class PrometheusResponseTimeRecorderSpec extends FlatSpec with Matchers with MockFactory {

  "PrometheusLatencyRecorder" should "register a histogram and record request latencies" in {
    val registry = new CollectorRegistry()
    val randomMetricName = generateRandomString
    val randomMetricHelp = generateRandomString
    val randomLabelName = generateRandomString
    val randomEndpointName = generateRandomString
    val randomLatency = Math.abs(Random.nextInt(10000))

    // our random value will end up in the second bucket
    val buckets = List((randomLatency - 1).toDouble, (randomLatency + 1).toDouble)

    val recorder = new PrometheusResponseTimeRecorder(
      randomMetricName,
      randomMetricHelp,
      buckets,
      randomLabelName,
      registry,
      duration.MILLISECONDS
    )

    recorder.recordResponseTime(randomEndpointName, FiniteDuration(randomLatency, duration.MILLISECONDS))

    val first = getBucketValue(registry, randomMetricName, List(randomLabelName), List(randomEndpointName), buckets.head)
    val second = getBucketValue(registry, randomMetricName, List(randomLabelName), List(randomEndpointName), buckets.last)
    val positiveInf = getBucketValue(registry, randomMetricName, List(randomLabelName), List(randomEndpointName), Double.PositiveInfinity)

    first shouldBe 0
    second shouldBe 1
    positiveInf shouldBe 1
  }

  private def getBucketValue(registry: CollectorRegistry, metricName: String, labelNames: List[String], labelValues: List[String], bucket: Double) = {
    val name = metricName + "_bucket"

    // 'le' should be the first label in the list
    val allLabelNames = (Array("le") ++ labelNames).reverse
    val allLabelValues = (Array(Collector.doubleToGoString(bucket)) ++ labelValues).reverse
    registry.getSampleValue(name, allLabelNames, allLabelValues).intValue()
  }

} 
开发者ID:lonelyplanet,项目名称:prometheus-akka-http,代码行数:55,代码来源:PrometheusResponseTimeRecorderSpec.scala


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