|
Akka/Scala example source code file (KnownOpsInTimespanTimer.scala)
The KnownOpsInTimespanTimer.scala Akka example source code
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.testkit.metrics
import com.codahale.metrics._
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.TimeUnit._
/**
* Specialised "one-shot" Timer.
* Given a known number of operations performed within a time span (to be measured) it displays the average time one operation took.
*
* Please note that this is a *very coarse* estimation; The gain though is that we do not have to perform the counting inside of the measured thing (we can adding in tight loops).
*/
class KnownOpsInTimespanTimer(expectedOps: Long) extends Metric with Counting {
val startTime = System.nanoTime
val stopTime = new AtomicLong(0)
/**
* Stops the Timer.
* Can be called multiple times, though only the first call will be taken into account.
*
* @return true if this was the first call to `stop`, false otherwise
*/
def stop(): Boolean = stopTime.compareAndSet(0, System.nanoTime)
override def getCount: Long = expectedOps
def elapsedTime: Long = stopTime.get - startTime
def avgDuration: Long = (elapsedTime.toDouble / expectedOps).toLong
def opsPerSecond: Double = expectedOps.toDouble / (elapsedTime.toDouble / NANOSECONDS.convert(1, SECONDS))
}
Other Akka source code examplesHere is a short list of links related to this Akka KnownOpsInTimespanTimer.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.