Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Commit

Permalink
Use nanoTime() instead of now() for durations
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Sembera committed Dec 31, 2016
1 parent a7241b5 commit c8819af
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 47 deletions.
14 changes: 0 additions & 14 deletions src/main/scala/eu/semberal/dbstress/Defaults.scala

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/scala/eu/semberal/dbstress/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object Main extends LazyLogging {
val future: Future[Unit] = new Orchestrator(system).run(sc, exports)
val bugMsg = "This is most likely an application bug. Please, file an issue in the dbstress bug tracker"
val exitStatus: Int = try {
Await.result(future, Defaults.ScenarioTimeout)
Await.result(future, Utils.ScenarioTimeout)
logger.info("Scenario finished successfully")
0
} catch {
Expand All @@ -93,7 +93,7 @@ object Main extends LazyLogging {
}

try {
Await.result(system.terminate(), Defaults.ActorSystemShutdownTimeout)
Await.result(system.terminate(), Utils.ActorSystemShutdownTimeout)
} catch {
case e: TimeoutException =>
logger.warn("Unable to shutdown the actor system within the specified time limit", e)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/eu/semberal/dbstress/Orchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Orchestrator(actorSystem: ActorSystem) extends LazyLogging {

val controller = actorSystem.actorOf(ControllerActor.props(sc), "controller")

implicit val timeout: Timeout = Defaults.ScenarioTimeout
implicit val timeout: Timeout = Utils.ScenarioTimeout

implicit val executionContext = actorSystem.dispatcher
val es = new ExportingService(exports)
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/eu/semberal/dbstress/Utils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eu.semberal.dbstress

import java.time.format.DateTimeFormatter

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

object Utils {
val filePathFriendlyDateTimeFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")

val ScenarioTimeout: FiniteDuration = (24 * 60 * 60).seconds

val ActorSystemShutdownTimeout: FiniteDuration = 20.seconds

def toMillis(start: Long, end: Long): Long = Math.round((end - start) / 1000000.0)

}
15 changes: 8 additions & 7 deletions src/main/scala/eu/semberal/dbstress/actor/DatabaseActor.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package eu.semberal.dbstress.actor

import java.sql.{Connection, DriverManager, SQLException}
import java.time.LocalDateTime

import akka.actor.{Actor, Props}
import akka.event.LoggingReceive
import com.typesafe.scalalogging.LazyLogging
import eu.semberal.dbstress.Utils
import eu.semberal.dbstress.actor.ControllerActor.{UnitRunError, UnitRunFinished, UnitRunInitializationFailed, UnitRunInitializationFinished}
import eu.semberal.dbstress.actor.DatabaseActor.{ConnectionTimeoutException, InitConnection, StartUnitRun}
import eu.semberal.dbstress.model.Configuration.UnitRunConfig
Expand Down Expand Up @@ -35,7 +35,7 @@ class DatabaseActor(scenarioId: String, unitName: String, urConfig: UnitRunConfi
override def receive: Receive = LoggingReceive {
case InitConnection =>
val s = sender()
val start = LocalDateTime.now()
val start = System.nanoTime()

val future = {
val initFuture = Future {
Expand All @@ -52,11 +52,12 @@ class DatabaseActor(scenarioId: String, unitName: String, urConfig: UnitRunConfi
}).getOrElse(initFuture)
}


future.onComplete {
case Success(c) =>
s ! UnitRunInitializationFinished
this.connection = Some(c)
this.connInitResult = Some(DbConnInitResult(start, LocalDateTime.now()))
this.connInitResult = Some(DbConnInitResult(Utils.toMillis(start, System.nanoTime())))
case Failure(e) =>
s ! UnitRunInitializationFailed(e)
}(systemDispatcher)
Expand All @@ -69,7 +70,7 @@ class DatabaseActor(scenarioId: String, unitName: String, urConfig: UnitRunConfi
prevFuture.flatMap { l =>
Future {
val dbCallId = DbCallId(scenarioId, connectionId, IdGen.genStatementId())
val start = LocalDateTime.now()
val start = System.nanoTime()
connection.map(c =>
managed(c.createStatement()).map { statement =>
if (statement.execute(urConfig.dbConfig.query.replace(IdGen.IdPlaceholder, dbCallId.toString)))
Expand All @@ -78,14 +79,14 @@ class DatabaseActor(scenarioId: String, unitName: String, urConfig: UnitRunConfi
UpdateCount(statement.getUpdateCount)
}.tried match {
case Success(result) =>
DbCallSuccess(start, LocalDateTime.now(), dbCallId, result) :: l
DbCallSuccess(Utils.toMillis(start, System.nanoTime()), dbCallId, result) :: l
case Failure(e) =>
logger.warn(s"Query execution failed: ${e.getMessage}")
DbCallFailure(start, LocalDateTime.now(), dbCallId, e) :: l
DbCallFailure(Utils.toMillis(start, System.nanoTime()), dbCallId, e) :: l
}
).getOrElse {
val e = new IllegalStateException("Connection not initialized")
DbCallFailure(LocalDateTime.now(), LocalDateTime.now(), dbCallId, e) :: l
DbCallFailure(Utils.toMillis(start, System.nanoTime()), dbCallId, e) :: l
}
}(dbDispatcher)
}(systemDispatcher)
Expand Down
15 changes: 4 additions & 11 deletions src/main/scala/eu/semberal/dbstress/model/Results.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package eu.semberal.dbstress.model

import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

import eu.semberal.dbstress.model.Configuration.UnitConfig

object Results {

trait OperationResult {
val start: LocalDateTime

val finish: LocalDateTime

lazy val duration: Long = ChronoUnit.MILLIS.between(start, finish)
val duration: Long
}

case class DbConnInitResult(start: LocalDateTime, finish: LocalDateTime) extends OperationResult
case class DbConnInitResult(duration: Long) extends OperationResult

sealed trait DbCallResult extends OperationResult {
val dbCallId: DbCallId
Expand All @@ -25,9 +18,9 @@ object Results {
override def toString = s"${scenarioId}_${connectionId}_$statementId"
}

case class DbCallSuccess(start: LocalDateTime, finish: LocalDateTime, dbCallId: DbCallId, stmtResult: StatementResult) extends DbCallResult
case class DbCallSuccess(duration: Long, dbCallId: DbCallId, stmtResult: StatementResult) extends DbCallResult

case class DbCallFailure(start: LocalDateTime, finish: LocalDateTime, dbCallId: DbCallId, e: Throwable) extends DbCallResult
case class DbCallFailure(duration: Long, dbCallId: DbCallId, e: Throwable) extends DbCallResult

sealed trait StatementResult

Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/eu/semberal/dbstress/util/ResultsExport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import java.time.LocalDateTime

import com.github.tototoshi.csv.CSVWriter
import com.typesafe.scalalogging.LazyLogging
import eu.semberal.dbstress.Defaults._
import eu.semberal.dbstress.Utils._
import eu.semberal.dbstress.model.Results.ScenarioResult
import eu.semberal.dbstress.util.ModelExtensions._
import resource._

trait ResultsExport {
protected val curr = filePathFriendlyDateTimeFormat.format(LocalDateTime.now())
protected val curr: String = filePathFriendlyDateTimeFormat.format(LocalDateTime.now())

def export(sr: ScenarioResult): Unit

protected def withWriter(filePath: String)(op: Writer => Unit): Unit =
for (b <- managed(new BufferedWriter(new FileWriter(filePath)))) op(b)

}

class CsvResultsExport(outputDir: File) extends ResultsExport with LazyLogging {
Expand Down
16 changes: 7 additions & 9 deletions src/test/scala/eu/semberal/dbstress/model/UnitSummaryTest.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package eu.semberal.dbstress.model

import java.time.LocalDateTime

import eu.semberal.dbstress.model.Configuration.{DbCommunicationConfig, UnitConfig, UnitRunConfig}
import eu.semberal.dbstress.model.Results._
import eu.semberal.dbstress.model.UnitSummaryTest.unitResult
import org.scalatest.{FlatSpec, Matchers}

class UnitSummaryTest extends FlatSpec with Matchers {

val summary = unitResult.summary
private val summary = unitResult.summary

behavior of "UnitSummary"

Expand All @@ -22,13 +20,13 @@ class UnitSummaryTest extends FlatSpec with Matchers {
}

object UnitSummaryTest {
val repeats = 5
val parallel = 2
val unitResult = {
private val repeats = 5
private val parallel = 2
private val unitResult = {
val dbCommunicationConfig = DbCommunicationConfig("A", Some("B"), "C", "D", "E", Some(10))
val unitRunResults = UnitRunResult(DbConnInitResult(LocalDateTime.now(), LocalDateTime.now()), List(
DbCallSuccess(LocalDateTime.now(), LocalDateTime.now(), DbCallId("1", "2", "3"), FetchedRows(10)),
DbCallFailure(LocalDateTime.now(), LocalDateTime.now(), DbCallId("4", "5", "6"), new RuntimeException)
val unitRunResults = UnitRunResult(DbConnInitResult(1), List(
DbCallSuccess(1, DbCallId("1", "2", "3"), FetchedRows(10)),
DbCallFailure(2, DbCallId("4", "5", "6"), new RuntimeException)
))
UnitResult(UnitConfig("unit1", Some("This is unit1"), UnitRunConfig(dbCommunicationConfig, repeats), parallel), List(unitRunResults))
}
Expand Down

0 comments on commit c8819af

Please sign in to comment.