From cfa8856fcd663edea16b5a189f6a050b2802a867 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Wed, 20 Jul 2011 14:06:39 +0200 Subject: [PATCH] Ticket 981: Adjusted how report files are stored and result logged --- .../common/BenchResultRepository.scala | 43 ++++++++++++++---- .../performance/trading/common/Report.scala | 44 +++++-------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/akka-actor-tests/src/test/scala/akka/performance/trading/common/BenchResultRepository.scala b/akka-actor-tests/src/test/scala/akka/performance/trading/common/BenchResultRepository.scala index 2f9ea89dd8..c14bb62f18 100644 --- a/akka-actor-tests/src/test/scala/akka/performance/trading/common/BenchResultRepository.scala +++ b/akka-actor-tests/src/test/scala/akka/performance/trading/common/BenchResultRepository.scala @@ -9,10 +9,10 @@ import java.io.ObjectInputStream import java.io.ObjectOutputStream import java.text.SimpleDateFormat import java.util.Date - import scala.collection.mutable.{ Map ⇒ MutableMap } - import akka.event.EventHandler +import java.io.PrintWriter +import java.io.FileWriter trait BenchResultRepository { def add(stats: Stats) @@ -23,6 +23,10 @@ trait BenchResultRepository { def getWithHistorical(name: String, load: Int): Seq[Stats] + def saveHtmlReport(content: String, name: String): Unit + + def htmlReportUrl(name: String): String + } object BenchResultRepository { @@ -34,8 +38,10 @@ class FileBenchResultRepository extends BenchResultRepository { private val statsByName = MutableMap[String, Seq[Stats]]() private val baselineStats = MutableMap[Key, Stats]() private val historicalStats = MutableMap[Key, Seq[Stats]]() - private val dir = System.getProperty("benchmark.resultDir", "target/benchmark") - private def dirExists: Boolean = new File(dir).exists + private val serDir = System.getProperty("benchmark.resultDir", "target/benchmark") + "/ser" + private def serDirExists: Boolean = new File(serDir).exists + private val htmlDir = System.getProperty("benchmark.resultDir", "target/benchmark") + "/html" + private def htmlDirExists: Boolean = new File(htmlDir).exists protected val maxHistorical = 7 case class Key(name: String, load: Int) @@ -64,10 +70,10 @@ class FileBenchResultRepository extends BenchResultRepository { } private def loadFiles() { - if (dirExists) { + if (serDirExists) { val files = for { - f ← new File(dir).listFiles + f ← new File(serDir).listFiles if f.isFile if f.getName.endsWith(".ser") } yield f @@ -86,11 +92,11 @@ class FileBenchResultRepository extends BenchResultRepository { } private def save(stats: Stats) { - new File(dir).mkdirs - if (!dirExists) return + new File(serDir).mkdirs + if (!serDirExists) return val timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(stats.timestamp)) val name = stats.name + "--" + timestamp + "--" + stats.load + ".ser" - val f = new File(dir, name) + val f = new File(serDir, name) var out: ObjectOutputStream = null try { out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f))) @@ -127,5 +133,24 @@ class FileBenchResultRepository extends BenchResultRepository { loadFiles() + def saveHtmlReport(content: String, fileName: String) { + new File(htmlDir).mkdirs + if (!htmlDirExists) return + val f = new File(htmlDir, fileName) + var writer: PrintWriter = null + try { + writer = new PrintWriter(new FileWriter(f)) + writer.print(content) + writer.flush() + } catch { + case e: Exception ⇒ + EventHandler.error(this, "Failed to save report to [%s], due to [%s]". + format(f.getAbsolutePath, e.getMessage)) + } finally { + if (writer ne null) try { writer.close() } catch { case ignore: Exception ⇒ } + } + } + + def htmlReportUrl(fileName: String): String = new File(htmlDir, fileName).getAbsolutePath } diff --git a/akka-actor-tests/src/test/scala/akka/performance/trading/common/Report.scala b/akka-actor-tests/src/test/scala/akka/performance/trading/common/Report.scala index 9160fa631e..7ec32a5904 100644 --- a/akka-actor-tests/src/test/scala/akka/performance/trading/common/Report.scala +++ b/akka-actor-tests/src/test/scala/akka/performance/trading/common/Report.scala @@ -10,16 +10,13 @@ class Report( resultRepository: BenchResultRepository, compareResultWith: Option[String] = None) { - private val dir = System.getProperty("benchmark.resultDir", "target/benchmark") - - private def dirExists: Boolean = new File(dir).exists - private def log = System.getProperty("benchmark.logResult", "false").toBoolean + private def log = System.getProperty("benchmark.logResult", "true").toBoolean val dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm") val legendTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm") val fileTimestampFormat = new SimpleDateFormat("yyyyMMddHHmmss") - def html(statistics: Seq[Stats]): Unit = if (dirExists) { + def html(statistics: Seq[Stats]): Unit = { val current = statistics.last val sb = new StringBuilder @@ -29,7 +26,8 @@ class Report( sb.append("

%s

\n".format(title)) sb.append("
\n")
-    sb.append(formatResultsTable(statistics))
+    val resultTable = formatResultsTable(statistics)
+    sb.append(resultTable)
     sb.append("\n
\n") sb.append(img(percentilesChart(current))) @@ -43,15 +41,17 @@ class Report( comparePercentilesChart(stats).foreach(url ⇒ sb.append(img(url))) } - if (dirExists) { - val timestamp = fileTimestampFormat.format(new Date(current.timestamp)) - val name = current.name + "--" + timestamp + ".html" - write(sb.toString, name) + val timestamp = fileTimestampFormat.format(new Date(current.timestamp)) + val reportName = current.name + "--" + timestamp + ".html" + resultRepository.saveHtmlReport(sb.toString, reportName) + + if (log) { + EventHandler.info(this, resultTable + "Charts in html report: " + resultRepository.htmlReportUrl(reportName)) } } - private def img(url: String): String = { + def img(url: String): String = { """""".format( url, GoogleChartBuilder.ChartWidth, GoogleChartBuilder.ChartHeight) + "\n" } @@ -59,7 +59,6 @@ class Report( def percentilesChart(stats: Stats): String = { val chartTitle = stats.name + " Percentiles (microseconds)" val chartUrl = GoogleChartBuilder.percentilChartUrl(resultRepository.get(stats.name), chartTitle, _.load + " clients") - if (log) EventHandler.info(this, chartTitle + " Chart:\n" + chartUrl) chartUrl } @@ -70,7 +69,6 @@ class Report( } yield { val chartTitle = stats.name + " vs. " + compareName + ", " + stats.load + " clients" + ", Percentiles (microseconds)" val chartUrl = GoogleChartBuilder.percentilChartUrl(Seq(compareStats, stats), chartTitle, _.name) - if (log) EventHandler.info(this, chartTitle + " Chart:\n" + chartUrl) chartUrl } } @@ -81,7 +79,6 @@ class Report( val chartTitle = stats.name + " vs. historical, " + stats.load + " clients" + ", Percentiles (microseconds)" val chartUrl = GoogleChartBuilder.percentilChartUrl(withHistorical, chartTitle, stats ⇒ legendTimeFormat.format(new Date(stats.timestamp))) - if (log) EventHandler.info(this, chartTitle + " Chart:\n" + chartUrl) Some(chartUrl) } else { None @@ -91,7 +88,6 @@ class Report( def latencyAndThroughputChart(stats: Stats): String = { val chartTitle = stats.name + " Latency (microseconds) and Throughput (TPS)" val chartUrl = GoogleChartBuilder.latencyAndThroughputChartUrl(resultRepository.get(stats.name), chartTitle) - if (log) EventHandler.info(this, chartTitle + " Chart:\n" + chartUrl) chartUrl } @@ -115,8 +111,6 @@ class Report( statsSeq.map(formatStats(_)).mkString("\n") + "\n" + line + "\n" - if (log) EventHandler.info(this, formattedStats) - formattedStats } @@ -146,22 +140,6 @@ class Report( } - def write(content: String, fileName: String) { - val f = new File(dir, fileName) - var writer: PrintWriter = null - try { - writer = new PrintWriter(new FileWriter(f)) - writer.print(content) - writer.flush() - } catch { - case e: Exception ⇒ - EventHandler.error(this, "Failed to save report to [%s], due to [%s]". - format(f.getAbsolutePath, e.getMessage)) - } finally { - if (writer ne null) try { writer.close() } catch { case ignore: Exception ⇒ } - } - } - def header(title: String) = """| |