From 4757691e67ef3c1ced8f2deda2c990bcfeb7ffd8 Mon Sep 17 00:00:00 2001 From: Till Schallau <9063061+tillschallau@users.noreply.github.com> Date: Tue, 20 Aug 2024 10:21:11 +0200 Subject: [PATCH] Add global deletion of test-result-logs folders (#164) Co-authored-by: Dominik Schmid --- .../stars/core/evaluation/TSCEvaluation.kt | 165 +++++++++--------- .../stars/core/metric/providers/Loggable.kt | 25 +-- .../utils/ApplicationConstantsHolder.kt | 5 + 3 files changed, 97 insertions(+), 98 deletions(-) diff --git a/stars-core/src/main/kotlin/tools/aqua/stars/core/evaluation/TSCEvaluation.kt b/stars-core/src/main/kotlin/tools/aqua/stars/core/evaluation/TSCEvaluation.kt index 6fa53298..1970d0a6 100644 --- a/stars-core/src/main/kotlin/tools/aqua/stars/core/evaluation/TSCEvaluation.kt +++ b/stars-core/src/main/kotlin/tools/aqua/stars/core/evaluation/TSCEvaluation.kt @@ -85,104 +85,95 @@ class TSCEvaluation< * @throws IllegalArgumentException When there are no [MetricProvider]s registered. */ fun runEvaluation(writePlots: Boolean = true, writePlotDataCSV: Boolean = false) { - try { - require(metricProviders.any()) { - "There needs to be at least one registered MetricProviders." + require(metricProviders.any()) { "There needs to be at least one registered MetricProviders." } + + val totalEvaluationTime = measureTime { + /** Holds the [List] of [TSCProjection] based on the base [tsc]. */ + var tscProjections: List> + + // Build all projections of the base TSC + val tscProjectionCalculationTime = measureTime { + tscProjections = tsc.buildProjections(projectionIgnoreList) } - val totalEvaluationTime = measureTime { - /** Holds the [List] of [TSCProjection] based on the base [tsc]. */ - var tscProjections: List> - - // Build all projections of the base TSC - val tscProjectionCalculationTime = measureTime { - tscProjections = tsc.buildProjections(projectionIgnoreList) - } - - logFine( - "The calculation of the projections for the given tsc took: $tscProjectionCalculationTime") - - val segmentsEvaluationTime = measureTime { - segments - .forEachIndexed { index, segment -> - print("\rCurrently evaluating segment $index") - val segmentEvaluationTime = measureTime { - // Run the "evaluate" function for all SegmentMetricProviders on the current - // segment - metricProviders.filterIsInstance>().forEach { - it.evaluate(segment) - } - val projectionsEvaluationTime = measureTime { - tscProjections.forEach { projection -> - val projectionEvaluationTime = measureTime { - // Run the "evaluate" function for all ProjectionMetricProviders on the - // current - // segment - metricProviders - .filterIsInstance>() - .forEach { it.evaluate(projection) } - // Holds the PredicateContext for the current segment - val context = PredicateContext(segment) - - // Holds the [TSCInstanceNode] of the current [projection] using the - // [PredicateContext], representing a whole TSC. - val segmentProjectionTSCInstance = projection.tsc.evaluate(context) - - // Run the "evaluate" function for all TSCInstanceMetricProviders on the - // current segment - metricProviders - .filterIsInstance>() - .forEach { it.evaluate(segmentProjectionTSCInstance) } - - // Run the "evaluate" function for all - // ProjectionAndTSCInstanceNodeMetricProviders on the current projection and - // instance - metricProviders - .filterIsInstance< - ProjectionAndTSCInstanceNodeMetricProvider>() - .forEach { it.evaluate(projection, segmentProjectionTSCInstance) } - } - logFine( - "The evaluation of projection '${projection.id}' for segment '$segment' took: $projectionEvaluationTime") + logFine( + "The calculation of the projections for the given tsc took: $tscProjectionCalculationTime") + + val segmentsEvaluationTime = measureTime { + segments + .forEachIndexed { index, segment -> + print("\rCurrently evaluating segment $index") + val segmentEvaluationTime = measureTime { + // Run the "evaluate" function for all SegmentMetricProviders on the current + // segment + metricProviders.filterIsInstance>().forEach { + it.evaluate(segment) + } + val projectionsEvaluationTime = measureTime { + tscProjections.forEach { projection -> + val projectionEvaluationTime = measureTime { + // Run the "evaluate" function for all ProjectionMetricProviders on the + // current + // segment + metricProviders + .filterIsInstance>() + .forEach { it.evaluate(projection) } + // Holds the PredicateContext for the current segment + val context = PredicateContext(segment) + + // Holds the [TSCInstanceNode] of the current [projection] using the + // [PredicateContext], representing a whole TSC. + val segmentProjectionTSCInstance = projection.tsc.evaluate(context) + + // Run the "evaluate" function for all TSCInstanceMetricProviders on the + // current segment + metricProviders + .filterIsInstance>() + .forEach { it.evaluate(segmentProjectionTSCInstance) } + + // Run the "evaluate" function for all + // ProjectionAndTSCInstanceNodeMetricProviders on the current projection and + // instance + metricProviders + .filterIsInstance< + ProjectionAndTSCInstanceNodeMetricProvider>() + .forEach { it.evaluate(projection, segmentProjectionTSCInstance) } } + logFine( + "The evaluation of projection '${projection.id}' for segment '$segment' took: $projectionEvaluationTime") } - logFine( - "The evaluation of all projections for segment '$segment' took: $projectionsEvaluationTime") } - logFine("The evaluation of segment '$segment' took: $segmentEvaluationTime") + logFine( + "The evaluation of all projections for segment '$segment' took: $projectionsEvaluationTime") } - .also { println() } - } - logInfo("The evaluation of all segments took: $segmentsEvaluationTime") + logFine("The evaluation of segment '$segment' took: $segmentEvaluationTime") + } + .also { println() } } - logInfo("The whole evaluation took: $totalEvaluationTime") + logInfo("The evaluation of all segments took: $segmentsEvaluationTime") + } + logInfo("The whole evaluation took: $totalEvaluationTime") - // Print the results of all Stateful metrics - metricProviders.filterIsInstance().forEach { it.printState() } + // Print the results of all Stateful metrics + metricProviders.filterIsInstance().forEach { it.printState() } - // Call the 'evaluate' and then the 'print' function for all PostEvaluationMetricProviders - println("Running post evaluation metrics") - metricProviders.filterIsInstance>().forEach { - it.postEvaluate() - it.printPostEvaluationResult() - } + // Call the 'evaluate' and then the 'print' function for all PostEvaluationMetricProviders + println("Running post evaluation metrics") + metricProviders.filterIsInstance>().forEach { + it.postEvaluate() + it.printPostEvaluationResult() + } - // Plot the results of all Plottable metrics - if (writePlots) { - println("Creating Plots") - metricProviders.filterIsInstance().forEach { it.writePlots() } - } + // Plot the results of all Plottable metrics + if (writePlots) { + println("Creating Plots") + metricProviders.filterIsInstance().forEach { it.writePlots() } + } - // Write CSV of the results of all Plottable metrics - if (writePlotDataCSV) { - println("Writing CSVs") - metricProviders.filterIsInstance().forEach { it.writePlotDataCSV() } - } - } finally { - // Close all logging handlers to prevent .lck files to remain - println("Closing Loggers") - metricProviders.filterIsInstance().forEach { it.closeLogger() } - closeLogger() + // Write CSV of the results of all Plottable metrics + if (writePlotDataCSV) { + println("Writing CSVs") + metricProviders.filterIsInstance().forEach { it.writePlotDataCSV() } } } } diff --git a/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/providers/Loggable.kt b/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/providers/Loggable.kt index 03b59c99..eb36c815 100644 --- a/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/providers/Loggable.kt +++ b/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/providers/Loggable.kt @@ -20,6 +20,7 @@ package tools.aqua.stars.core.metric.providers import java.io.File import java.util.logging.* import tools.aqua.stars.core.metric.utils.ApplicationConstantsHolder +import tools.aqua.stars.core.metric.utils.ApplicationConstantsHolder.activeLoggers /** This interface can be implemented to be able to log data into the stdout and log files. */ @Suppress("unused") @@ -116,17 +117,19 @@ interface Loggable { } val file = "$logFolderFile/$name-${currentTimeAndDate}" - return@run Logger.getAnonymousLogger().apply { - useParentHandlers = false - level = Level.FINEST - - addHandler(getLoggerHandler(file, Level.SEVERE)) - addHandler(getLoggerHandler(file, Level.WARNING)) - addHandler(getLoggerHandler(file, Level.INFO)) - addHandler(getLoggerHandler(file, Level.FINE)) - addHandler(getLoggerHandler(file, Level.FINER)) - addHandler(getLoggerHandler(file, Level.FINEST)) - } + return@run Logger.getAnonymousLogger() + .apply { + useParentHandlers = false + level = Level.FINEST + + addHandler(getLoggerHandler(file, Level.SEVERE)) + addHandler(getLoggerHandler(file, Level.WARNING)) + addHandler(getLoggerHandler(file, Level.INFO)) + addHandler(getLoggerHandler(file, Level.FINE)) + addHandler(getLoggerHandler(file, Level.FINER)) + addHandler(getLoggerHandler(file, Level.FINEST)) + } + .also { activeLoggers.add(it) } } private fun getLoggerHandler(file: String, level: Level) = diff --git a/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/utils/ApplicationConstantsHolder.kt b/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/utils/ApplicationConstantsHolder.kt index d7d72472..bc9569aa 100644 --- a/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/utils/ApplicationConstantsHolder.kt +++ b/stars-core/src/main/kotlin/tools/aqua/stars/core/metric/utils/ApplicationConstantsHolder.kt @@ -21,6 +21,7 @@ import java.io.File import java.time.LocalDateTime import java.time.format.DateTimeFormatter import java.util.logging.LogManager +import java.util.logging.Logger /** * This singleton holds the current date and time at the start of the application and the log @@ -44,11 +45,15 @@ object ApplicationConstantsHolder { val logFolder: String get() = if (isTestRun()) TEST_LOG_FOLDER else ANALYSIS_LOG_FOLDER + /** Holds the [MutableList] of all currently registered [Logger]s. */ + val activeLoggers: MutableList = mutableListOf() + init { Runtime.getRuntime() .addShutdownHook( Thread { LogManager.getLogManager().reset() + activeLoggers.forEach { it.handlers.forEach { handler -> handler.close() } } File(TEST_LOG_FOLDER).deleteRecursively() }) }