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)