Skip to content

Commit

Permalink
#50: More robust journey duration calculation.
Browse files Browse the repository at this point in the history
By accepting 'yyyy-MM-dd'T'HH:mm:ss.SSSxxx' and 'yyyy-MM-dd'T'HH:mm:ssxxx' as possible startedDateTime values in HARs.
  • Loading branch information
manuelkiessling committed Mar 3, 2017
1 parent d1ee702 commit a3cc43e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,28 @@ object HarAnalyzer {
private def calculateTotalRequestTime(entries: List[JsonAST.JValue]): Int = {
implicit val formats = org.json4s.DefaultFormats

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
val formatterWithMilliseconds = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
val formatterWithoutMilliseconds = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx")

val starttimesEpochMilli = for { entry <- entries } yield {
val startedDateTime = (entry \ "startedDateTime").extract[String]
java.time.ZonedDateTime.parse(startedDateTime, formatter).toInstant.toEpochMilli
try {
java.time.ZonedDateTime.parse(startedDateTime, formatterWithMilliseconds).toInstant.toEpochMilli
} catch {
case e: java.time.format.DateTimeParseException =>
java.time.ZonedDateTime.parse(startedDateTime, formatterWithoutMilliseconds).toInstant.toEpochMilli
}
}

val endtimesEpochMilli = for { entry <- entries } yield {
val startedDateTime = (entry \ "startedDateTime").extract[String]
val time = (entry \ "time").extract[Int]
java.time.ZonedDateTime.parse(startedDateTime, formatter).toInstant.toEpochMilli + time
try {
java.time.ZonedDateTime.parse(startedDateTime, formatterWithMilliseconds).toInstant.toEpochMilli + time
} catch {
case e: java.time.format.DateTimeParseException =>
java.time.ZonedDateTime.parse(startedDateTime, formatterWithoutMilliseconds).toInstant.toEpochMilli + time
}
}

(endtimesEpochMilli.max - starttimesEpochMilli.min).toInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object FixtureGenerator {
testcaseId = "testcaseId1",
testresultId = "testresultId1",
datetimeRun = datetimeRun1,
// Note how we expect startedDateTime to sometimes lack milliseconds
har = parse(
"""
|{
Expand All @@ -27,7 +28,7 @@ object FixtureGenerator {
| "status": 200
| },
| "time": 10,
| "startedDateTime": "2017-03-02T06:10:43.436+01:00"
| "startedDateTime": "2017-03-02T06:10:43+01:00"
| },
| {
| "response": {
Expand Down Expand Up @@ -175,7 +176,7 @@ class SparkExampleSpec extends FunSpec with BeforeAndAfter with Matchers {
statistics(0).testresultDatetimeRun.toString.substring(24) should be("2015")
statistics(0).numberOfRequestsWithStatus200 should be(1)
statistics(0).numberOfRequestsWithStatus400 should be(1)
statistics(0).totalRequestTime should be(2015)
statistics(0).totalRequestTime should be(2451)

statistics(1).testcaseId should be("testcaseId1")
statistics(1).dayBucket should be("2015-11-18")
Expand Down

0 comments on commit a3cc43e

Please sign in to comment.