-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from adessoAG/structure_timestamps
Structure timestamps
- Loading branch information
Showing
15 changed files
with
295 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.gradle | ||
/build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
*.csv | ||
|
||
### Java ### | ||
.class | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
src/main/kotlin/de/adesso/junitinsights/SpringContextListeners.kt
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/de/adesso/junitinsights/annotations/JUnitInsights.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.adesso.junitinsights.annotations | ||
|
||
import de.adesso.junitinsights.tests.TestBenchmarkExtension | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
/** | ||
* Annotate your test classes with this method to activate JUnit-insights. | ||
*/ | ||
@Target(AnnotationTarget.TYPE, AnnotationTarget.CLASS) | ||
@Retention(value = AnnotationRetention.RUNTIME) | ||
@ExtendWith(TestBenchmarkExtension::class) | ||
annotation class JUnitInsights |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/de/adesso/junitinsights/annotations/NoJUnitInsights.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package de.adesso.junitinsights.annotations | ||
|
||
/** | ||
* Annotate the functions you don't want to have insights about with this and they won't be benchmarked. | ||
*/ | ||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(value = AnnotationRetention.RUNTIME) | ||
annotation class NoJUnitInsights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
src/main/kotlin/de/adesso/junitinsights/extensions/SpringInsightExtension.kt
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/de/adesso/junitinsights/listener/SpringContextListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package de.adesso.junitinsights.listener | ||
|
||
import de.adesso.junitinsights.tools.TimestampWriter | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.context.event.ContextClosedEvent | ||
import org.springframework.context.event.ContextRefreshedEvent | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class SpringContextListener { | ||
|
||
companion object { | ||
val log: Logger = LoggerFactory.getLogger(this::class.java) | ||
} | ||
|
||
private val timestampWriter = TimestampWriter | ||
|
||
@EventListener(ContextRefreshedEvent::class) | ||
fun catchContextStart(event: ContextRefreshedEvent) { | ||
//log.info("### AppContextId: ${event.applicationContext.id}") | ||
//TODO Check if first init before closing initial, so that its not a refresh | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"context refreshed", | ||
"", "") | ||
} | ||
|
||
@EventListener(ContextClosedEvent::class) | ||
fun catchContextEnd(event: ContextClosedEvent) { | ||
//log.info("### AppContextId: ${event.applicationContext.id}") | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"context closed", | ||
"", "") | ||
//TODO: Is this method really called only once at the end? | ||
timestampWriter.flush() | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/kotlin/de/adesso/junitinsights/tests/TestBenchmarkExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package de.adesso.junitinsights.tests | ||
|
||
import de.adesso.junitinsights.annotations.NoJUnitInsights | ||
import de.adesso.junitinsights.tools.TimestampWriter | ||
import org.junit.jupiter.api.extension.* | ||
import org.junit.platform.commons.support.AnnotationSupport.isAnnotated | ||
|
||
|
||
/** | ||
* Extension that measures the execution time of each test class and method | ||
* | ||
*/ | ||
class TestBenchmarkExtension : | ||
BeforeAllCallback, AfterAllCallback, | ||
BeforeEachCallback, AfterEachCallback, | ||
BeforeTestExecutionCallback, AfterTestExecutionCallback { | ||
|
||
private val timestampWriter = TimestampWriter | ||
|
||
override fun beforeAll(context: ExtensionContext) { | ||
if (shouldNotBeBenchmarked(context)) { | ||
return | ||
} | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"before all", | ||
trimClassName(context), | ||
trimMethodName(context)) | ||
} | ||
|
||
override fun afterAll(context: ExtensionContext) { | ||
if (shouldNotBeBenchmarked(context)) { | ||
return | ||
} | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"after all", | ||
trimClassName(context), | ||
trimMethodName(context)) | ||
timestampWriter.flush() | ||
} | ||
|
||
override fun beforeEach(context: ExtensionContext) { | ||
if (shouldNotBeBenchmarked(context)) { | ||
return | ||
} | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"before each", | ||
trimClassName(context), | ||
trimMethodName(context)) | ||
} | ||
|
||
override fun afterEach(context: ExtensionContext) { | ||
if (shouldNotBeBenchmarked(context)) { | ||
return | ||
} | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"after each", | ||
trimClassName(context), | ||
trimMethodName(context)) | ||
} | ||
|
||
@Throws(Exception::class) | ||
override fun beforeTestExecution(context: ExtensionContext) { | ||
if (shouldNotBeBenchmarked(context)) { | ||
return | ||
} | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"before test execution", | ||
trimClassName(context), | ||
trimMethodName(context)) | ||
} | ||
|
||
@Throws(Exception::class) | ||
override fun afterTestExecution(context: ExtensionContext) { | ||
if (shouldNotBeBenchmarked(context)) { | ||
return | ||
} | ||
timestampWriter.writeTimestamp(System.currentTimeMillis(), | ||
"after test execution", | ||
trimClassName(context), | ||
trimMethodName(context)) | ||
} | ||
|
||
private fun shouldNotBeBenchmarked(context: ExtensionContext): Boolean { | ||
return context.element | ||
.map<Boolean> { el -> isAnnotated(el, NoJUnitInsights::class.java) } | ||
.orElse(false) | ||
} | ||
|
||
private fun trimClassName(testContext: ExtensionContext): String { | ||
return testContext.testClass.toString().replace("class", "") | ||
} | ||
|
||
private fun trimMethodName(testContext: ExtensionContext): String { | ||
val splitName = testContext.testMethod.toString().split(".") | ||
return if (splitName.isEmpty()) "" else splitName.last() | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/de/adesso/junitinsights/tools/TimestampWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.adesso.junitinsights.tools | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.io.File | ||
|
||
var deltaMode = false | ||
var logOutput = false | ||
|
||
object TimestampWriter { | ||
private var file = File("timestamps.csv").bufferedWriter() | ||
private var lastTimestamp: Long = 0 | ||
|
||
private var logger: Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
init { | ||
file.write("timestamp;event;test class;test function") | ||
file.newLine() | ||
} | ||
|
||
fun writeTimestamp(timestamp: Long, event: String, testClass: String, testFunction: String) { | ||
var tstamp: Long = timestamp | ||
if (deltaMode) { | ||
if (lastTimestamp == 0.toLong()) { | ||
lastTimestamp = timestamp | ||
} else { | ||
tstamp = timestamp - lastTimestamp | ||
lastTimestamp = timestamp | ||
} | ||
} | ||
file.write(tstamp.toString() + ";" + event + ";" + trimObjectString(testClass) + ";" + trimObjectString(testFunction)) | ||
file.newLine() | ||
if (logOutput) | ||
logger.info("########" + tstamp.toString() + ";" + event + ";" + trimObjectString(testClass) + ";" + trimObjectString(testFunction) + "\n") | ||
} | ||
|
||
fun flush() { | ||
file.flush() | ||
} | ||
|
||
private fun trimObjectString(string: String): String { | ||
return string.replace("Optional.empty", "") | ||
.replace("Optional", "") | ||
.replace("[", "") | ||
.replace("]", "") | ||
} | ||
} |
Oops, something went wrong.