本文整理汇总了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()
}
}
示例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
}
示例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 = ()
}
示例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()
}
}