Skip to content

Commit 1b3d4be

Browse files
author
Rustam Sadykov
committed
optimize coverage storage
1 parent 4414255 commit 1b3d4be

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -483,29 +483,26 @@ internal val Method.isVisibleFromGeneratedTest: Boolean
483483
&& (this.modifiers and Modifier.NATIVE) == 0
484484

485485
private fun StatsForClass.updateCoverage(newCoverage: Coverage, isNewClass: Boolean, fromFuzzing: Boolean) {
486-
coverage = coverage.update(newCoverage, isNewClass)
486+
coverage.update(newCoverage, isNewClass)
487487
// other coverage type updates by empty coverage to respect new class
488488
val emptyCoverage = newCoverage.copy(
489489
coveredInstructions = emptyList()
490490
)
491491
if (fromFuzzing) {
492-
fuzzedCoverage = fuzzedCoverage.update(newCoverage, isNewClass)
493-
concolicCoverage = concolicCoverage.update(emptyCoverage, isNewClass)
492+
fuzzedCoverage to concolicCoverage
494493
} else {
495-
fuzzedCoverage = fuzzedCoverage.update(emptyCoverage, isNewClass)
496-
concolicCoverage = concolicCoverage.update(newCoverage, isNewClass)
494+
concolicCoverage to fuzzedCoverage
495+
}.let { (targetSource, otherSource) ->
496+
targetSource.update(newCoverage, isNewClass)
497+
otherSource.update(emptyCoverage, isNewClass)
497498
}
498499
}
499500

500-
private fun Coverage?.update(newCoverage: Coverage, isNewClass: Boolean) =
501-
this?.let { oldCoverage ->
502-
val instructionsCount = if (isNewClass && newCoverage.instructionsCount != null) {
503-
newCoverage.instructionsCount!! + (oldCoverage.instructionsCount ?: 0)
504-
} else {
505-
oldCoverage.instructionsCount
501+
private fun CoverageInstructionsSet.update(newCoverage: Coverage, isNewClass: Boolean) {
502+
if (isNewClass) {
503+
newCoverage.instructionsCount?.let {
504+
totalInstructions += it
506505
}
507-
Coverage(
508-
coveredInstructions = oldCoverage.coveredInstructions + newCoverage.coveredInstructions,
509-
instructionsCount = instructionsCount
510-
)
511-
} ?: newCoverage
506+
}
507+
coveredInstructions.addAll(newCoverage.coveredInstructions)
508+
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Statistics.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.utbot.contest
22

3+
import java.io.File
34
import org.utbot.common.MutableMultiset
45
import org.utbot.common.mutableMultisetOf
6+
import org.utbot.framework.plugin.api.Instruction
57
import org.utbot.framework.plugin.api.UtError
6-
import org.utbot.framework.plugin.api.Coverage
7-
import java.io.File
88

99
private fun Double.format(digits: Int) = "%.${digits}f".format(this)
1010

@@ -55,11 +55,11 @@ class GlobalStats {
5555
get() = statsForClasses.sumBy { it.concolicCoverage.getCoverageInfo(it.className).covered }
5656

5757
val totalInstructionsCount: Int
58-
get() = statsForClasses.sumBy { it.coverage?.instructionsCount?.toInt() ?: 0 }
58+
get() = statsForClasses.sumBy { it.coverage.totalInstructions.toInt() }
5959

6060
val avgCoverage: Double
6161
get() = statsForClasses
62-
.filter { it.coverage?.instructionsCount?.let { cnt -> cnt != 0L } ?: false }
62+
.filter { it.coverage.totalInstructions != 0L }
6363
.map { it.coverage.getCoverageInfo(it.className).run { if (total == 0) 0.0 else 100.0 * covered / total } }
6464
.average()
6565

@@ -114,11 +114,11 @@ class StatsForClass(val className: String) {
114114
val methodsWithAtLeastOneException: Int get() = statsForMethods.count { it.failReasons.isNotEmpty() }
115115
val testcasesGenerated: Int get() = statsForMethods.sumBy { it.testsGeneratedCount }
116116

117-
var coverage: Coverage? = null
118-
var fuzzedCoverage: Coverage? = null
119-
var concolicCoverage: Coverage? = null
117+
var coverage = CoverageInstructionsSet()
118+
var fuzzedCoverage = CoverageInstructionsSet()
119+
var concolicCoverage = CoverageInstructionsSet()
120120

121-
private fun Coverage?.prettyInfo(): String =
121+
private fun CoverageInstructionsSet.prettyInfo(): String =
122122
getCoverageInfo(className).run { "$covered/$total" }
123123

124124
override fun toString(): String = "\n<StatsForClass> :" +
@@ -178,13 +178,18 @@ class FailReason(private val throwable: Throwable) {
178178

179179
}
180180

181+
data class CoverageInstructionsSet(
182+
val coveredInstructions: MutableSet<Instruction> = mutableSetOf(),
183+
var totalInstructions: Long = 0
184+
)
185+
181186
data class CoverageStatistic(val covered: Int, val total: Int)
182187

183-
private fun Coverage?.getCoverageInfo(className: String): CoverageStatistic = this?.run {
188+
private fun CoverageInstructionsSet?.getCoverageInfo(className: String): CoverageStatistic = this?.run {
184189
CoverageStatistic(
185190
coveredInstructions.filter {
186191
instr -> instr.className.startsWith(className)
187192
}.toSet().size,
188-
instructionsCount?.toInt() ?: 0
193+
totalInstructions.toInt()
189194
)
190195
} ?: CoverageStatistic(covered = 0, total = 0)

0 commit comments

Comments
 (0)