Skip to content

Instantly share code, notes, and snippets.

@optician
Created March 31, 2015 09:30
Show Gist options
  • Save optician/8703b8bb52cbee7b488c to your computer and use it in GitHub Desktop.
Save optician/8703b8bb52cbee7b488c to your computer and use it in GitHub Desktop.
val f = scala.io.Source.fromFile("jm.log")
def trunk(value: Double): Double = BigDecimal(value).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble
def percentile(ms: Map[Long, Long], amount: Long, lvls: List[Int]): List[Long] = {
val ls = ms.toList.sortBy(x => x._1)
val lvlValues = lvls map (lvl => Math.ceil(amount*lvl/100f).toLong)
def search(a: Long, xs: List[(Long, Long)], lvlValue: Long): Long = xs match {
case v::xs if a+v._2 < lvlValue => search(a+v._2, xs, lvlValue)
case v::xs => v._1
case Nil => 0L
}
lvlValues map (lvlValue => search(0, ls, lvlValue))
}
case class Entry(
timeStamp: Long,
elapsed: Long,
label: String,
responseCode: String,
responseMessage: String,
threadName: String,
dataType: String,
success: Boolean,
bytes: Long,
grpThreads: Int,
allThreads: Int,
Latency: Long
)
case class Result(count: Long = 0,
elpsd: Long = 0,
lat: Long = 0,
bytes: Long = 0,
error: Float = 0,
max: Long = 0,
min: Long = Long.MaxValue,
maxTimestamp: Long = 0,
minTimestamp: Long = Long.MaxValue,
elapsedHistogram: Map[Long, Long] = Map())
val entries = f.getLines().filter(!_.startsWith("timeStamp")).
map { l =>
val arr = l.split(",")
Entry.tupled(
arr(0).toLong, arr(1).toLong, arr(2), arr(3),
arr(4), arr(5), arr(6), arr(7).toBoolean,
arr(8).toLong, arr(9).toInt, arr(10).toInt,
arr(11).toLong
)
}
val agg = entries.foldLeft(Result()) { (accum, y) =>
Result(accum.count + 1,
accum.elpsd + y.elapsed,
accum.lat + y.Latency,
accum.bytes + y.bytes,
accum.error + {
if (y.success) 0 else 1
},
Math.max(accum.max, y.elapsed),
Math.min(accum.min, y.elapsed),
Math.max(accum.maxTimestamp, y.timeStamp + y.elapsed),
Math.min(accum.minTimestamp, y.timeStamp),
accum.elapsedHistogram + (y.elapsed -> (accum.elapsedHistogram.getOrElse(y.elapsed, 1L) + 1L))
)
}
println(agg.count,
trunk(agg.elpsd / agg.count) + "msec",
percentile(agg.elapsedHistogram,agg.count, List(50,90,95,99)),
agg.max + "msec",
agg.min + "msec",
trunk(agg.error / agg.count * 100) + "% errors",
trunk(agg.count / Math.round((agg.maxTimestamp - agg.minTimestamp) / 1000)) + "/sec",
trunk(agg.bytes / 1024 / Math.round((agg.maxTimestamp - agg.minTimestamp) / 1000)) + "KB/sec"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment