Skip to content

Commit df75f26

Browse files
committed
very primitive global timeoutfor synthesis
1 parent c6cac8d commit df75f26

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/examples/synthesis/SynthesisExamplesTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class SynthesisExamplesTest : UtValueTestCaseChecker(
3737
fun enableSynthesizer() {
3838
UtSettings.enableSynthesis = true
3939
UtSettings.enableSynthesisCache = true
40-
UtSettings.synthesisTimeoutInMillis = 60_000
40+
UtSettings.synthesisTimeoutInMillis = 100_000
4141
UtSettings.synthesisMaxDepth = 10
4242
}
4343

@@ -262,4 +262,4 @@ class SynthesisExamplesTest : UtValueTestCaseChecker(
262262
)
263263
assertTrue(Synthesizer.successRate > 0.9)
264264
}
265-
}
265+
}

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.utbot.framework.plugin.api.util.utContext
3636
import org.utbot.framework.plugin.api.util.withUtContext
3737
import org.utbot.framework.plugin.services.JdkInfo
3838
import org.utbot.framework.synthesis.Synthesizer
39+
import org.utbot.framework.synthesis.SynthesizerController
3940
import org.utbot.framework.synthesis.postcondition.constructors.EmptyPostCondition
4041
import org.utbot.framework.synthesis.postcondition.constructors.PostConditionConstructor
4142
import org.utbot.framework.util.SootUtils
@@ -52,6 +53,7 @@ import java.util.*
5253
import kotlin.coroutines.cancellation.CancellationException
5354
import kotlin.math.min
5455
import kotlin.reflect.KCallable
56+
import kotlin.system.measureTimeMillis
5557

5658
/**
5759
* Generates test cases: one by one or a whole set for the method under test.
@@ -74,6 +76,7 @@ open class TestCaseGenerator(
7476
) {
7577
private val logger: KLogger = KotlinLogging.logger {}
7678
private val timeoutLogger: KLogger = KotlinLogging.logger(logger.name + ".timeout")
79+
protected var synthesizerController = SynthesizerController(UtSettings.synthesisTimeoutInMillis)
7780

7881
private val classpathForEngine: String
7982
get() = buildDir.toString() + (classpath?.let { File.pathSeparator + it } ?: "")
@@ -387,20 +390,26 @@ open class TestCaseGenerator(
387390

388391
protected fun List<UtExecution>.toAssemble(method: ExecutableId): List<UtExecution> =
389392
map { execution ->
390-
val symbolicExecution = (execution as? UtSymbolicExecution)
391-
?: return@map execution
392-
393-
val newBeforeState = mapEnvironmentModels(method, symbolicExecution, symbolicExecution.stateBefore) {
394-
it.modelsBefore
395-
} ?: return@map execution
396-
val newAfterState = getConcreteAfterState(method, newBeforeState) ?: return@map execution
397-
398-
symbolicExecution.copy(
399-
newBeforeState,
400-
newAfterState,
401-
symbolicExecution.result,
402-
symbolicExecution.coverage
403-
)
393+
var result = execution
394+
synthesizerController.spentTime += measureTimeMillis {
395+
if (!synthesizerController.hasTimeLimit()) return@measureTimeMillis
396+
397+
val symbolicExecution = (execution as? UtSymbolicExecution)
398+
?: return@measureTimeMillis
399+
400+
val newBeforeState = mapEnvironmentModels(method, symbolicExecution, symbolicExecution.stateBefore) {
401+
it.modelsBefore
402+
} ?: return@measureTimeMillis
403+
val newAfterState = getConcreteAfterState(method, newBeforeState) ?: return@measureTimeMillis
404+
405+
result = symbolicExecution.copy(
406+
newBeforeState,
407+
newAfterState,
408+
symbolicExecution.result,
409+
symbolicExecution.coverage
410+
)
411+
}
412+
result
404413
}
405414

406415
private fun mapEnvironmentModels(
@@ -411,7 +420,7 @@ open class TestCaseGenerator(
411420
): EnvironmentModels? {
412421
val constrainedExecution = symbolicExecution.constrainedExecution ?: return null
413422
val aa = Synthesizer(this@TestCaseGenerator, method, selector(constrainedExecution))
414-
val synthesizedModels = aa.synthesize()
423+
val synthesizedModels = aa.synthesize(synthesizerController.localTimeLimit)
415424

416425
val (synthesizedThis, synthesizedParameters) = models.thisInstance?.let {
417426
synthesizedModels.first() to synthesizedModels.drop(1)

utbot-framework/src/main/kotlin/org/utbot/framework/synthesis/Synthesizer.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ private object SynthesisCache {
2525
.add(synthesisUnitContext)
2626
}
2727

28+
29+
data class SynthesizerController(
30+
val globalTimeLimit: Long,
31+
val localTimeLimit: Long = globalTimeLimit / 10
32+
) {
33+
var spentTime = 0L
34+
35+
fun hasTimeLimit() = spentTime < globalTimeLimit
36+
}
37+
2838
class Synthesizer(
2939
val testCaseGenerator: TestCaseGenerator,
3040
val method: ExecutableId,

utbot-framework/src/main/kotlin/org/utbot/tests/infrastructure/TestSpecificTestCaseGenerator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.utbot.framework.plugin.api.UtExecution
2020
import org.utbot.framework.plugin.api.UtMethodTestSet
2121
import org.utbot.framework.plugin.api.util.id
2222
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider
23+
import org.utbot.framework.synthesis.SynthesizerController
2324
import org.utbot.framework.synthesis.postcondition.constructors.EmptyPostCondition
2425
import org.utbot.framework.util.jimpleBody
2526
import java.nio.file.Path
@@ -93,6 +94,7 @@ class TestSpecificTestCaseGenerator(
9394
forceMockListener?.detach(this, forceMockListener)
9495
forceStaticMockListener?.detach(this, forceStaticMockListener)
9596

97+
synthesizerController = SynthesizerController(UtSettings.synthesisTimeoutInMillis)
9698
val minimizedExecutions = super.minimizeExecutions(executions.toAssemble(method))
9799
return UtMethodTestSet(method, minimizedExecutions, jimpleBody(method), errors)
98100
}

0 commit comments

Comments
 (0)