Skip to content

Commit

Permalink
ported tools/stats.gradle (#3219)
Browse files Browse the repository at this point in the history
* ported stats and removed redundant `project` receiver in release.gradle.kts

---------

Co-authored-by: Frank Liu <[email protected]>
  • Loading branch information
elect86 and frankfliu authored May 28, 2024
1 parent d241860 commit 5d951ba
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 29 deletions.
5 changes: 2 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/ai/djl/javaProject.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
`java-library`
id("ai.djl.javaFormatter")
id("ai.djl.check")
id("ai.djl.stats")
}

tasks {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/ai/djl/release.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import text

val libs = the<LibrariesForLibs>()

project.tasks {
tasks {
register("increaseBuildVersion") {
doLast {
if (!project.hasProperty("targetVersion")) {
Expand Down
66 changes: 66 additions & 0 deletions buildSrc/src/main/kotlin/ai/djl/stats.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<Duration, String>()

val demoListener = gradle.sharedServices.registerIfAbsent("demoListener ", StatisticsService::class) {
parameters.testsResults = testsResults
}

gradle.taskGraph.whenReady {
gradle.serviceOf<BuildEventsListenerRegistry>().onTaskCompletion(demoListener)
}

abstract class StatisticsService : BuildService<StatisticsService.Parameters>,
OperationCompletionListener, AutoCloseable {

interface Parameters : BuildServiceParameters {
var testsResults: MutableMap<Duration, String>
}

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<ExtraPropertiesExtension>("ext")
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -45,6 +45,7 @@ if (JavaVersion.current() < JavaVersion.VERSION_21) {
}

dependencyResolutionManagement {
@Suppress("UnstableApiUsage")
repositories {
mavenCentral()
maven("https://oss.sonatype.org/content/repositories/snapshots/")
Expand Down
24 changes: 0 additions & 24 deletions tools/gradle/stats.gradle

This file was deleted.

0 comments on commit 5d951ba

Please sign in to comment.