diff --git a/build.gradle.kts b/build.gradle.kts index a4a6181e433..429a9b12f8e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,5 @@ plugins { - id ("com.github.spotbugs") apply false + id("com.github.spotbugs") apply false ai.djl.release + ai.djl.stats } - -//apply from: file("${rootProject.projectDir}/tools/gradle/stats.gradle") diff --git a/buildSrc/src/main/kotlin/ai/djl/javaProject.gradle.kts b/buildSrc/src/main/kotlin/ai/djl/javaProject.gradle.kts index 59d5c663aa0..fa234cb13da 100644 --- a/buildSrc/src/main/kotlin/ai/djl/javaProject.gradle.kts +++ b/buildSrc/src/main/kotlin/ai/djl/javaProject.gradle.kts @@ -11,6 +11,7 @@ plugins { `java-library` id("ai.djl.javaFormatter") id("ai.djl.check") + id("ai.djl.stats") } tasks { diff --git a/buildSrc/src/main/kotlin/ai/djl/release.gradle.kts b/buildSrc/src/main/kotlin/ai/djl/release.gradle.kts index aa1bceff73c..96eff83af69 100644 --- a/buildSrc/src/main/kotlin/ai/djl/release.gradle.kts +++ b/buildSrc/src/main/kotlin/ai/djl/release.gradle.kts @@ -7,7 +7,7 @@ import text val libs = the() -project.tasks { +tasks { register("increaseBuildVersion") { doLast { if (!project.hasProperty("targetVersion")) { diff --git a/buildSrc/src/main/kotlin/ai/djl/stats.gradle.kts b/buildSrc/src/main/kotlin/ai/djl/stats.gradle.kts new file mode 100644 index 00000000000..51523690208 --- /dev/null +++ b/buildSrc/src/main/kotlin/ai/djl/stats.gradle.kts @@ -0,0 +1,66 @@ +package ai.djl + +import org.gradle.api.plugins.ExtraPropertiesExtension +import org.gradle.kotlin.dsl.support.serviceOf +import org.gradle.tooling.events.FinishEvent +import org.gradle.tooling.events.OperationCompletionListener +import kotlin.time.Duration +import kotlin.time.Duration.Companion.seconds +import kotlin.time.ExperimentalTime +import kotlin.time.TimeSource + +plugins { + `java-library` +} + +@ExperimentalTime +val timeSource = TimeSource.Monotonic +val testsResults = mutableMapOf() + +val demoListener = gradle.sharedServices.registerIfAbsent("demoListener ", StatisticsService::class) { + parameters.testsResults = testsResults +} + +gradle.taskGraph.whenReady { + gradle.serviceOf().onTaskCompletion(demoListener) +} + +abstract class StatisticsService : BuildService, + OperationCompletionListener, AutoCloseable { + + interface Parameters : BuildServiceParameters { + var testsResults: MutableMap + } + + override fun onFinish(event: FinishEvent) {} + + override fun close() { + if (parameters.testsResults.isNotEmpty()) { + println("========== Test duration ========== ") + for ((key, value) in parameters.testsResults.entries.sortedByDescending { it.key }.take(6)) { + // `.inWholeSeconds.seconds` truncate to integer units, without decimals + println("\t$value:\t${key.inWholeSeconds.seconds}") + } + } + } +} + +tasks.test { + doFirst { + @OptIn(ExperimentalTime::class) + startTime = timeSource.markNow() + } + doLast { + @OptIn(ExperimentalTime::class) + if (state.didWork) + demoListener.get().parameters.testsResults[timeSource.markNow() - startTime] = project.name + } +} + +@ExperimentalTime +var Task.startTime: TimeSource.Monotonic.ValueTimeMark + get() = ext.get("startTime") as TimeSource.Monotonic.ValueTimeMark + set(value) = ext.set("startTime", value) + +val Task.ext: ExtraPropertiesExtension + get() = (this as ExtensionAware).extensions.getByName("ext") diff --git a/settings.gradle.kts b/settings.gradle.kts index e5ad56d8607..fd1c6bdd50b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,7 +1,7 @@ rootProject.name = "djl" plugins { - id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0") + id("org.gradle.toolchains.foojay-resolver-convention") version ("0.8.0") } include(":api") @@ -45,6 +45,7 @@ if (JavaVersion.current() < JavaVersion.VERSION_21) { } dependencyResolutionManagement { + @Suppress("UnstableApiUsage") repositories { mavenCentral() maven("https://oss.sonatype.org/content/repositories/snapshots/") diff --git a/tools/gradle/stats.gradle b/tools/gradle/stats.gradle deleted file mode 100644 index 92e9aa393ef..00000000000 --- a/tools/gradle/stats.gradle +++ /dev/null @@ -1,24 +0,0 @@ -def testsResults = new TreeMap<>(Comparator.reverseOrder()) -gradle.taskGraph.beforeTask { Task task -> - task.ext.setProperty("startTime", Instant.now()) -} - -gradle.taskGraph.afterTask { Task task, TaskState state -> - if (task.name.equals("test") && state.didWork) { - long duration = Duration.between(task.ext.startTime, Instant.now()).toSeconds() - testsResults.put(duration, task.project.name); - } -} - -gradle.buildFinished { - if (gradle.startParameter.taskNames.contains("build") && !testsResults.isEmpty()) { - int count = 0; - println "========== Test duration ==========" - for (Map.Entry entry : testsResults.entrySet()) { - if (count++ > 5) { - break; - } - println "\t${entry.value}:\t${entry.key}s" - } - } -}