|
| 1 | +package org.apalachemc.integration.framework |
| 2 | + |
| 3 | +import java.io.{BufferedWriter, IOException} |
| 4 | +import java.nio.charset.StandardCharsets |
| 5 | +import java.nio.file.{Files, Path, Paths, StandardOpenOption} |
| 6 | + |
| 7 | +import scala.collection.mutable |
| 8 | + |
| 9 | +import org.scalatest.ResourcefulReporter |
| 10 | +import org.scalatest.events.{ |
| 11 | + Event, |
| 12 | + RunAborted, |
| 13 | + RunCompleted, |
| 14 | + RunStopped, |
| 15 | + TestCanceled, |
| 16 | + TestFailed, |
| 17 | + TestPending, |
| 18 | + TestStarting, |
| 19 | + TestSucceeded, |
| 20 | +} |
| 21 | + |
| 22 | +/** Writes configuration-aware ScalaTest timing events as JSON Lines. |
| 23 | + * |
| 24 | + * A start record is flushed before each test runs. If the worker is killed, the |
| 25 | + * report generator can therefore identify the test that never produced a |
| 26 | + * matching completion record. |
| 27 | + */ |
| 28 | +final class IntegrationTimingReporter private[integration] ( |
| 29 | + outputDirectory: Path, |
| 30 | + forcedOutputPath: Option[Path], |
| 31 | + forcedConfiguration: Option[String]) |
| 32 | + extends ResourcefulReporter { |
| 33 | + import IntegrationTimingReporter._ |
| 34 | + |
| 35 | + private val startedAt = mutable.Map.empty[TestId, Long] |
| 36 | + private val writers = mutable.Map.empty[String, BufferedWriter] |
| 37 | + private var closed = false |
| 38 | + |
| 39 | + /** Constructor used by focused reporter tests. */ |
| 40 | + private[integration] def this(outputPath: Path, configuration: String) = |
| 41 | + this(outputPath.getParent, Some(outputPath), Some(configuration)) |
| 42 | + |
| 43 | + /** Constructor used to test configuration routing without changing process environment. */ |
| 44 | + private[integration] def this(outputDirectory: Path) = |
| 45 | + this(outputDirectory, None, None) |
| 46 | + |
| 47 | + /** Public no-argument constructor required by ScalaTest's `-C` option. */ |
| 48 | + def this() = this( |
| 49 | + IntegrationTimingReporter.defaultOutputDirectory(), |
| 50 | + None, |
| 51 | + None, |
| 52 | + ) |
| 53 | + |
| 54 | + override def apply(event: Event): Unit = synchronized { |
| 55 | + if (!closed) { |
| 56 | + event match { |
| 57 | + case event: TestStarting => |
| 58 | + val (configuration, suiteName) = configurationAndSuiteName(event.suiteName) |
| 59 | + val id = TestId(configuration, event.suiteId, event.testName) |
| 60 | + startedAt.put(id, event.timeStamp) |
| 61 | + write( |
| 62 | + configuration, |
| 63 | + ujson.Obj( |
| 64 | + "schemaVersion" -> SchemaVersion, |
| 65 | + "event" -> "started", |
| 66 | + "configuration" -> configuration, |
| 67 | + "suiteId" -> event.suiteId, |
| 68 | + "suiteName" -> suiteName, |
| 69 | + "testName" -> event.testName, |
| 70 | + "timestampEpochMillis" -> ujson.Num(event.timeStamp.toDouble), |
| 71 | + )) |
| 72 | + |
| 73 | + case event: TestSucceeded => |
| 74 | + finish(event.suiteId, event.suiteName, event.testName, "succeeded", event.duration, event.timeStamp) |
| 75 | + case event: TestFailed => |
| 76 | + finish(event.suiteId, event.suiteName, event.testName, "failed", event.duration, event.timeStamp) |
| 77 | + case event: TestCanceled => |
| 78 | + finish(event.suiteId, event.suiteName, event.testName, "canceled", event.duration, event.timeStamp) |
| 79 | + case event: TestPending => |
| 80 | + finish(event.suiteId, event.suiteName, event.testName, "pending", event.duration, event.timeStamp) |
| 81 | + |
| 82 | + case _: RunCompleted | _: RunAborted | _: RunStopped => dispose() |
| 83 | + case _ => () |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + override def dispose(): Unit = synchronized { |
| 89 | + if (!closed) { |
| 90 | + closed = true |
| 91 | + writers.values.foreach(_.close()) |
| 92 | + writers.clear() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private def finish( |
| 97 | + suiteId: String, |
| 98 | + suiteName: String, |
| 99 | + testName: String, |
| 100 | + status: String, |
| 101 | + reportedDuration: Option[Long], |
| 102 | + timestamp: Long): Unit = { |
| 103 | + val (configuration, unqualifiedSuiteName) = configurationAndSuiteName(suiteName) |
| 104 | + val id = TestId(configuration, suiteId, testName) |
| 105 | + val measuredDuration = startedAt.remove(id).map(start => math.max(0L, timestamp - start)) |
| 106 | + val duration = reportedDuration.orElse(measuredDuration).getOrElse(0L) |
| 107 | + write( |
| 108 | + configuration, |
| 109 | + ujson.Obj( |
| 110 | + "schemaVersion" -> SchemaVersion, |
| 111 | + "event" -> "finished", |
| 112 | + "configuration" -> configuration, |
| 113 | + "suiteId" -> suiteId, |
| 114 | + "suiteName" -> unqualifiedSuiteName, |
| 115 | + "testName" -> testName, |
| 116 | + "status" -> status, |
| 117 | + "timestampEpochMillis" -> ujson.Num(timestamp.toDouble), |
| 118 | + "durationMillis" -> ujson.Num(duration.toDouble), |
| 119 | + )) |
| 120 | + } |
| 121 | + |
| 122 | + private def write(configuration: String, record: ujson.Obj): Unit = { |
| 123 | + val outputPath = forcedOutputPath.getOrElse(outputDirectory.resolve(s"$configuration.jsonl")) |
| 124 | + val writer = writers.getOrElseUpdate(configuration, open(outputPath)) |
| 125 | + writer.write(record.render()) |
| 126 | + writer.newLine() |
| 127 | + writer.flush() |
| 128 | + } |
| 129 | + |
| 130 | + private def configurationAndSuiteName(suiteName: String): (String, String) = { |
| 131 | + forcedConfiguration |
| 132 | + .map(_ -> suiteName) |
| 133 | + .getOrElse { |
| 134 | + ConfigurationIds |
| 135 | + .collectFirst { |
| 136 | + case configuration if suiteName.endsWith(s" [$configuration]") => |
| 137 | + configuration -> suiteName.stripSuffix(s" [$configuration]") |
| 138 | + } |
| 139 | + .getOrElse("general" -> suiteName) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +private[integration] object IntegrationTimingReporter { |
| 145 | + val SchemaVersion = 1 |
| 146 | + val TimingDirectoryProperty = "apalache.cli.test.timing-dir" |
| 147 | + |
| 148 | + private case class TestId(configuration: String, suiteId: String, testName: String) |
| 149 | + |
| 150 | + private val ConfigurationIds = IntegrationTestConfiguration.values.map(_.id) |
| 151 | + |
| 152 | + private[integration] def defaultOutputDirectory(): Path = { |
| 153 | + sys.env |
| 154 | + .get("APALACHE_CLI_TEST_TIMING_DIR") |
| 155 | + .filter(_.nonEmpty) |
| 156 | + .orElse(Option(System.getProperty(TimingDirectoryProperty)).filter(_.nonEmpty)) |
| 157 | + .map(Paths.get(_)) |
| 158 | + .getOrElse(Paths.get("target", "cli-integration-timings")) |
| 159 | + } |
| 160 | + |
| 161 | + private def open(outputPath: Path): BufferedWriter = { |
| 162 | + Option(outputPath.getParent).foreach(Files.createDirectories(_)) |
| 163 | + try { |
| 164 | + Files.newBufferedWriter( |
| 165 | + outputPath, |
| 166 | + StandardCharsets.UTF_8, |
| 167 | + StandardOpenOption.CREATE, |
| 168 | + StandardOpenOption.TRUNCATE_EXISTING, |
| 169 | + StandardOpenOption.WRITE, |
| 170 | + ) |
| 171 | + } catch { |
| 172 | + case exception: IOException => |
| 173 | + throw new IllegalStateException(s"Could not open integration-test timing report $outputPath", exception) |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments