-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScalaTestTestTimesReporter.scala
68 lines (63 loc) · 2.32 KB
/
ScalaTestTestTimesReporter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package test_times
import java.io.File
import java.nio.file.Files
import java.util.concurrent.ConcurrentHashMap
import org.scalatest.Reporter
import org.scalatest.events.Event
import org.scalatest.events.RunCompleted
import org.scalatest.events.RunStarting
import org.scalatest.events.SuiteCompleted
import org.scalatest.events.SuiteStarting
import java.nio.charset.StandardCharsets
import scala.collection.JavaConverters.*
class ScalaTestTestTimesReporter extends Reporter {
private[this] val suiteStart = new ConcurrentHashMap[String, (Long, SuiteStarting)]()
private[this] val suiteEnd = new ConcurrentHashMap[String, (Long, SuiteCompleted)]()
private[this] var filePath: String = null
private[this] def key: String = "test-time-output-file-path"
override def apply(event: Event): Unit = event match {
case e: RunStarting =>
filePath = e.configMap.getRequired[String](key)
case e: SuiteStarting =>
suiteStart.put(e.suiteId, (System.currentTimeMillis(), e))
case e: SuiteCompleted =>
suiteEnd.put(e.suiteId, (System.currentTimeMillis(), e))
case _: RunCompleted =>
Option(filePath).map(_.trim).filter(_.nonEmpty) match {
case Some(path) =>
val values = suiteEnd.asScala
.flatMap {
case (id, (endTime, endEvent)) =>
Option(suiteStart.get(id)).map {
case (startTime, startEvent) =>
ScalaTestTestTimesReporter.Value(
startTime = startTime,
endTime = endTime,
startEvent = startEvent,
endEvent = endEvent
)
}
}
.map { x =>
x.startEvent.suiteId -> (x.endTime - x.startTime)
}
.toList
.sortBy(_.swap)
val text = values.map { case (x1, x2) => s"${x1} ${x2}" }.mkString("", "\n", "\n")
val f = new File(path).getCanonicalFile
f.getParentFile.mkdirs()
Files.write(f.toPath, text.getBytes(StandardCharsets.UTF_8))
case None =>
println(s"[warn] does not provide ${key}!?")
}
case _ =>
}
}
object ScalaTestTestTimesReporter {
private case class Value(
startTime: Long,
endTime: Long,
startEvent: SuiteStarting,
endEvent: SuiteCompleted
)
}