Skip to content

Commit ae1de47

Browse files
committed
yet more refactoring
1 parent 72c7706 commit ae1de47

6 files changed

Lines changed: 69 additions & 58 deletions

File tree

core/src/main/kotlin/org/evomaster/core/problem/graphql/IntrospectiveQuery.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@ import com.fasterxml.jackson.databind.ObjectMapper
77
import org.evomaster.core.logging.LoggingUtil
88
import org.evomaster.core.remote.HttpClientFactory
99
import org.evomaster.core.remote.SutProblemException
10-
import org.evomaster.core.search.service.time.SearchTimeController
11-
import org.glassfish.jersey.client.ClientConfig
12-
import org.glassfish.jersey.client.ClientProperties
13-
import org.glassfish.jersey.client.HttpUrlConnectorProvider
10+
import org.evomaster.core.utils.TimeUtils
1411
import org.slf4j.LoggerFactory
15-
import javax.net.ssl.SSLContext
1612
import javax.ws.rs.client.Client
17-
import javax.ws.rs.client.ClientBuilder
1813
import javax.ws.rs.client.Entity
1914
import javax.ws.rs.core.MediaType
2015

@@ -58,8 +53,8 @@ class IntrospectiveQuery {
5853
""".trimIndent(), MediaType.APPLICATION_JSON_TYPE)
5954

6055
//TODO check if TCP problems
61-
val response = SearchTimeController.measureTimeMillis({ ms, res ->
62-
LoggingUtil.getInfoLogger().info("Fetched GraphQL schema in ${ms}ms")
56+
val response = TimeUtils.measureTimeMillis({ ms, res ->
57+
LoggingUtil.getInfoLogger().info("Fetched GraphQL schema in ${ms}ms")
6358
}, {
6459
try {
6560
var request = client.target(graphQlEndpoint)
@@ -72,8 +67,10 @@ class IntrospectiveQuery {
7267
.invoke()
7368
} catch (e: Exception) {
7469
log.error("Failed query to '$graphQlEndpoint' : $query")
75-
throw SutProblemException("Failed introspection query to '$graphQlEndpoint'." +
76-
" Please check connection and URL format. Error: ${e.message}")
70+
throw SutProblemException(
71+
"Failed introspection query to '$graphQlEndpoint'." +
72+
" Please check connection and URL format. Error: ${e.message}"
73+
)
7774
}
7875
})
7976

@@ -109,4 +106,4 @@ class IntrospectiveQuery {
109106

110107
return body
111108
}
112-
}
109+
}

core/src/main/kotlin/org/evomaster/core/problem/rest/service/fitness/AbstractRestFitness.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.evomaster.core.problem.rest.service.fitness
22

33
import com.webfuzzing.commons.faults.DefinedFaultCategory
4-
import com.webfuzzing.commons.faults.FaultCategory
54
import org.evomaster.test.utils.EMTestUtils
65
import org.evomaster.client.java.controller.api.dto.ActionDto
76
import org.evomaster.client.java.controller.api.dto.AdditionalInfoDto
@@ -12,13 +11,11 @@ import org.evomaster.core.logging.LoggingUtil
1211
import org.evomaster.core.problem.enterprise.DetectedFault
1312
import org.evomaster.core.problem.enterprise.ExperimentalFaultCategory
1413
import org.evomaster.core.problem.enterprise.SampleType
15-
import org.evomaster.core.problem.enterprise.auth.NoAuth
1614
import org.evomaster.core.problem.externalservice.HostnameResolutionAction
1715
import org.evomaster.core.problem.externalservice.HostnameResolutionInfo
1816
import org.evomaster.core.problem.externalservice.httpws.service.HarvestActualHttpWsResponseHandler
1917
import org.evomaster.core.problem.externalservice.httpws.service.HttpWsExternalServiceHandler
2018
import org.evomaster.core.problem.externalservice.httpws.HttpExternalServiceInfo
21-
import org.evomaster.core.problem.httpws.HttpWsCallResult
2219
import org.evomaster.core.problem.httpws.auth.AuthUtils
2320
import org.evomaster.core.problem.httpws.service.HttpWsFitness
2421
import org.evomaster.core.problem.rest.*
@@ -59,9 +56,8 @@ import org.evomaster.core.search.gene.string.StringGene
5956
import org.evomaster.core.search.gene.utils.GeneUtils
6057
import org.evomaster.core.search.service.DataPool
6158
import org.evomaster.core.search.service.ExecutionStats
62-
import org.evomaster.core.search.service.time.SearchTimeController
6359
import org.evomaster.core.taint.TaintAnalysis
64-
import org.evomaster.core.utils.StackTraceUtils
60+
import org.evomaster.core.utils.TimeUtils
6561
import org.slf4j.Logger
6662
import org.slf4j.LoggerFactory
6763
import java.net.URI
@@ -610,7 +606,7 @@ abstract class AbstractRestFitness : HttpWsFitness<RestIndividual>() {
610606
val response = try {
611607
val call = createInvocation(a, chainState, cookies, tokens)
612608

613-
SearchTimeController.measureTimeMillis(
609+
TimeUtils.measureTimeMillis(
614610
{ t, res ->
615611
rcr.setResponseTimeMs(t)
616612
executionStats.record(a.id, t)

core/src/main/kotlin/org/evomaster/core/search/service/FitnessFunction.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.evomaster.core.search.service
22

33
import com.google.inject.Inject
4-
import com.webfuzzing.commons.faults.FaultCategory
54
import org.evomaster.core.EMConfig
65
import org.evomaster.core.Lazy
76
import org.evomaster.core.problem.enterprise.DetectedFaultUtils
@@ -11,6 +10,7 @@ import org.evomaster.core.search.service.monitor.SearchProcessMonitor
1110
import org.evomaster.core.search.service.mutator.MutatedGeneSpecification
1211
import org.evomaster.core.search.service.time.ExecutionPhaseController
1312
import org.evomaster.core.search.service.time.SearchTimeController
13+
import org.evomaster.core.utils.TimeUtils
1414
import org.slf4j.Logger
1515
import org.slf4j.LoggerFactory
1616

@@ -149,12 +149,20 @@ abstract class FitnessFunction<T> where T : Individual {
149149
// By default, we optimize for performance in collecting coverage values, but for special cases, we want to collect full info
150150
val allTargetsWithDescriptive = config.processFormat == EMConfig.ProcessDataFormat.TARGET_HEURISTIC
151151

152-
val ei = SearchTimeController.measureTimeMillis(
153-
{ t, ind ->
154-
time.reportExecutedIndividualTime(t, actionsSize)
155-
ind?.executionTimeMs = t
156-
},
157-
{doCalculateCoverage(individual, targets, allTargets = allTargetsWithDescriptive, fullyCovered = false, descriptiveIds = allTargetsWithDescriptive)}
152+
val ei = TimeUtils.measureTimeMillis(
153+
{ t, ind ->
154+
time.reportExecutedIndividualTime(t, actionsSize)
155+
ind?.executionTimeMs = t
156+
},
157+
{
158+
doCalculateCoverage(
159+
individual,
160+
targets,
161+
allTargets = allTargetsWithDescriptive,
162+
fullyCovered = false,
163+
descriptiveIds = allTargetsWithDescriptive
164+
)
165+
}
158166
)
159167
// plugin execution info reporter here, to avoid the time spent by execution reporter
160168
handleExecutionInfo(ei)
@@ -179,4 +187,4 @@ abstract class FitnessFunction<T> where T : Individual {
179187
executionInfoReporter.sqlExecutionInfo(ei.individual.seeAllActions(), ei.fitness.databaseExecutions)
180188
executionInfoReporter.actionExecutionInfo(ei.individual, ei.executionTimeMs, time.evaluatedIndividuals)
181189
}
182-
}
190+
}

core/src/main/kotlin/org/evomaster/core/search/service/time/ExecutionPhaseController.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.evomaster.core.search.service.time
33
import org.evomaster.core.EMConfig
44
import org.evomaster.core.logging.LoggingUtil
55
import org.evomaster.core.search.service.time.SearchTimeController
6+
import org.evomaster.core.utils.TimeUtils
67
import javax.inject.Inject
78

89
class ExecutionPhaseController {
@@ -93,12 +94,12 @@ class ExecutionPhaseController {
9394
durationInSeconds[phase] = seconds
9495

9596
if(!config.avoidNonDeterministicLogs) {
96-
val time = SearchTimeController.getElapsedTime(seconds)
97+
val time = TimeUtils.getElapsedTime(seconds)
9798
LoggingUtil.getInfoLogger().info("Phase $phase lasted: $time")
9899
}
99100
}
100101

101102
phase = newPhase
102103
lastPhaseStartMs = System.currentTimeMillis()
103104
}
104-
}
105+
}

core/src/main/kotlin/org/evomaster/core/search/service/time/SearchTimeController.kt

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package org.evomaster.core.search.service.time
33
import com.google.inject.Inject
44
import org.evomaster.core.EMConfig
55
import org.evomaster.core.logging.LoggingUtil
6-
import org.evomaster.core.search.service.time.SearchListener
76
import org.evomaster.core.utils.IncrementalAverage
7+
import org.evomaster.core.utils.TimeUtils
88
import org.slf4j.LoggerFactory
99
import java.util.ArrayDeque
1010
import java.util.Queue
@@ -23,36 +23,7 @@ class SearchTimeController {
2323
companion object{
2424
private val log = LoggerFactory.getLogger(SearchTimeController::class.java)
2525

26-
/**
27-
* Invoke the [function] lambda, which will return some result of generic type [T].
28-
* Once this is completed, the [loggingFunction] will be automatically called with,
29-
* as input, the execution time expressed in milliseconds, as well as the [function]'s result
30-
* of type [T].
31-
*
32-
* From https://proandroiddev.com/measuring-execution-times-in-kotlin-460a0285e5ea
33-
*/
34-
inline fun <T> measureTimeMillis(loggingFunction: (Long, T) -> Unit,
35-
function: () -> T): T {
36-
37-
val startTime = System.currentTimeMillis()
38-
val result: T = function.invoke()
39-
loggingFunction.invoke(System.currentTimeMillis() - startTime, result)
40-
41-
return result
42-
}
43-
44-
fun getElapsedTime(totalInSeconds: Long) : String{
45-
46-
val seconds = totalInSeconds
47-
val minutes = seconds / 60.0
48-
val hours = minutes / 60.0
4926

50-
val ps = "%d".format(seconds % 60)
51-
val pm = "%d".format(minutes.toInt() % 60)
52-
val ph = "%d".format(hours.toInt())
53-
54-
return "${ph}h ${pm}m ${ps}s"
55-
}
5627
}
5728

5829

@@ -258,7 +229,7 @@ class SearchTimeController {
258229
}
259230

260231
fun getElapsedTime() : String{
261-
return getElapsedTime(getElapsedSeconds().toLong())
232+
return TimeUtils.getElapsedTime(getElapsedSeconds().toLong())
262233
}
263234

264235
fun shouldContinueSearch(): Boolean{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.evomaster.core.utils
2+
3+
object TimeUtils {
4+
5+
6+
/**
7+
* Invoke the [function] lambda, which will return some result of generic type [T].
8+
* Once this is completed, the [loggingFunction] will be automatically called with,
9+
* as input, the execution time expressed in milliseconds, as well as the [function]'s result
10+
* of type [T].
11+
*
12+
* From https://proandroiddev.com/measuring-execution-times-in-kotlin-460a0285e5ea
13+
*/
14+
inline fun <T> measureTimeMillis(
15+
loggingFunction: (Long, T) -> Unit,
16+
function: () -> T
17+
): T {
18+
19+
val startTime = System.currentTimeMillis()
20+
val result: T = function.invoke()
21+
loggingFunction.invoke(System.currentTimeMillis() - startTime, result)
22+
23+
return result
24+
}
25+
26+
fun getElapsedTime(totalInSeconds: Long) : String{
27+
28+
val seconds = totalInSeconds
29+
val minutes = seconds / 60.0
30+
val hours = minutes / 60.0
31+
32+
val ps = "%d".format(seconds % 60)
33+
val pm = "%d".format(minutes.toInt() % 60)
34+
val ph = "%d".format(hours.toInt())
35+
36+
return "${ph}h ${pm}m ${ps}s"
37+
}
38+
}

0 commit comments

Comments
 (0)