From 4a75abc6f283c51f3f5f98c0e78362e2df217d64 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Thu, 24 Aug 2023 16:18:34 +0300 Subject: [PATCH] Use some time compensation for dynamic classes transformation (#2551) Use some time compensation in dynamic classes transformation --- .../main/kotlin/org/utbot/common/StopWatch.kt | 17 ++++++++++++----- .../agent/DynamicClassTransformer.kt | 4 +++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/utbot-core/src/main/kotlin/org/utbot/common/StopWatch.kt b/utbot-core/src/main/kotlin/org/utbot/common/StopWatch.kt index 1c355317f9..1ff1b149fa 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/StopWatch.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/StopWatch.kt @@ -28,12 +28,19 @@ class StopWatch { startTime = System.currentTimeMillis() } } - - fun stop() { + + /** + * @param compensationMillis the duration in millis that should be subtracted from [elapsedMillis] to compensate + * for stopping and restarting [StopWatch] taking some time, can also be used to compensate for some activities, + * that are hard to directly detect (e.g. class loading). + * + * NOTE: [compensationMillis] will never cause [elapsedMillis] become negative. + */ + fun stop(compensationMillis: Long = 0) { lock.withLockInterruptibly { - startTime?.let { - elapsedMillis += (System.currentTimeMillis() - it) - startTime = null + startTime?.let { startTime -> + elapsedMillis += ((System.currentTimeMillis() - startTime) - compensationMillis).coerceAtLeast(0) + this.startTime = null } } } diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/agent/DynamicClassTransformer.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/agent/DynamicClassTransformer.kt index c3e83d4694..e9ba0651ff 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/agent/DynamicClassTransformer.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/agent/DynamicClassTransformer.kt @@ -35,7 +35,9 @@ class DynamicClassTransformer : ClassFileTransformer { classfileBuffer: ByteArray ): ByteArray? { try { - UtContext.currentContext()?.stopWatch?.stop() + // since we got here we have loaded a new class, meaning program is not stuck and some "meaningful" + // non-repeating actions are performed, so we assume that we should not time out for then next 65 ms + UtContext.currentContext()?.stopWatch?.stop(compensationMillis = 65) val pathToClassfile = protectionDomain.codeSource?.location?.toURI()?.let(Paths::get)?.absolutePathString() return if (pathToClassfile in pathsToUserClasses || packsToAlwaysTransform.any(className::startsWith)