From f2907d4e6d63c8ff1ae36068eb9ba6aa265d1a77 Mon Sep 17 00:00:00 2001 From: johnsonlee Date: Mon, 27 Jun 2022 00:15:53 +0800 Subject: [PATCH] Fix transform timing issue --- .../didiglobal/booster/transform/asm/AsmTransformer.kt | 9 ++++++--- .../booster/transform/javassist/JavassistTransformer.kt | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/booster-transform-asm/src/main/kotlin/com/didiglobal/booster/transform/asm/AsmTransformer.kt b/booster-transform-asm/src/main/kotlin/com/didiglobal/booster/transform/asm/AsmTransformer.kt index dfe073ebf..1beaca5b4 100644 --- a/booster-transform-asm/src/main/kotlin/com/didiglobal/booster/transform/asm/AsmTransformer.kt +++ b/booster-transform-asm/src/main/kotlin/com/didiglobal/booster/transform/asm/AsmTransformer.kt @@ -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 @@ -26,7 +27,7 @@ class AsmTransformer : Transformer { private val threadMxBean = ManagementFactory.getThreadMXBean() - private val durations = mutableMapOf() + private val durations = mutableMapOf() private val classLoader: ClassLoader @@ -87,7 +88,7 @@ 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") } } @@ -95,7 +96,9 @@ class AsmTransformer : Transformer { 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 } diff --git a/booster-transform-javassist/src/main/kotlin/com/didiglobal/booster/transform/javassist/JavassistTransformer.kt b/booster-transform-javassist/src/main/kotlin/com/didiglobal/booster/transform/javassist/JavassistTransformer.kt index 9dafffcb5..3b9c5263f 100644 --- a/booster-transform-javassist/src/main/kotlin/com/didiglobal/booster/transform/javassist/JavassistTransformer.kt +++ b/booster-transform-javassist/src/main/kotlin/com/didiglobal/booster/transform/javassist/JavassistTransformer.kt @@ -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 /** @@ -26,7 +27,7 @@ class JavassistTransformer : Transformer { private val threadMxBean = ManagementFactory.getThreadMXBean() - private val durations = mutableMapOf() + private val durations = mutableMapOf() private val classLoader: ClassLoader @@ -92,7 +93,7 @@ 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") } } @@ -100,7 +101,9 @@ class JavassistTransformer : Transformer { 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 }