Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transform timing issue #347

Merged
merged 1 commit into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.io.File
import java.io.InputStream
import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
import java.time.Duration
import java.util.ServiceLoader
import java.util.jar.JarFile

Expand All @@ -26,7 +27,7 @@ class AsmTransformer : Transformer {

private val threadMxBean = ManagementFactory.getThreadMXBean()

private val durations = mutableMapOf<ClassTransformer, Long>()
private val durations = mutableMapOf<ClassTransformer, Duration>()

private val classLoader: ClassLoader

Expand Down Expand Up @@ -87,15 +88,17 @@ class AsmTransformer : Transformer {
it.javaClass.name.length
}.max() ?: 20
this.durations.forEach { (transformer, ns) ->
println("${transformer.javaClass.name.padEnd(w1 + 1)}: ${ns / 1000000} ms")
println("${transformer.javaClass.name.padEnd(w1 + 1)}: ${ns.toMillis()} ms")
}
}

private fun <R> ThreadMXBean.sumCpuTime(transformer: ClassTransformer, action: () -> R): R {
val ct0 = this.currentThreadCpuTime
val result = action()
val ct1 = this.currentThreadCpuTime
durations[transformer] = durations.getOrDefault(transformer, 0) + (ct1 - ct0)
durations[transformer] = durations.getOrPut(transformer) {
Duration.ofNanos(0)
} + Duration.ofNanos(ct1 - ct0)
return result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.io.DataOutputStream
import java.io.File
import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
import java.time.Duration
import java.util.ServiceLoader

/**
Expand All @@ -26,7 +27,7 @@ class JavassistTransformer : Transformer {

private val threadMxBean = ManagementFactory.getThreadMXBean()

private val durations = mutableMapOf<ClassTransformer, Long>()
private val durations = mutableMapOf<ClassTransformer, Duration>()

private val classLoader: ClassLoader

Expand Down Expand Up @@ -92,15 +93,17 @@ class JavassistTransformer : Transformer {
it.javaClass.name.length
}.max() ?: 20
this.durations.forEach { (transformer, ns) ->
println("${transformer.javaClass.name.padEnd(w1 + 1)}: ${ns / 1000000} ms")
println("${transformer.javaClass.name.padEnd(w1 + 1)}: ${ns.toMillis()} ms")
}
}

private fun <R> ThreadMXBean.sumCpuTime(transformer: ClassTransformer, action: () -> R): R {
val ct0 = this.currentThreadCpuTime
val result = action()
val ct1 = this.currentThreadCpuTime
durations[transformer] = durations.getOrDefault(transformer, 0) + (ct1 - ct0)
durations[transformer] = durations.getOrPut(transformer) {
Duration.ofNanos(0)
} + Duration.ofNanos(ct1 - ct0)
return result
}

Expand Down