https://kotlinlang.org logo
#konsist
Title
# konsist
s

stelios

03/28/2024, 11:53 PM
Interesting performance issue: The following test takes 648ms to run:
Copy code
Given("foo1") {
    val files = Konsist.scopeFromProduction().files

    Then("bar1") {
        files.assertFalse {
            it.text.contains("foo bar")
        }
    }
}
while the following takes 11ms:
Copy code
Given("foo2") {
    val files = Konsist.scopeFromProduction().files

    Then("bar2") {
        files.filter {
            it.text.contains("foo bar")
        }.assertEmpty()
    }
}
Any ideas why?
v

Vidmantas Kerbelis

03/29/2024, 6:52 AM
Because the first case relies on the assertFalse's predicate to pass. Meaning on every item, a lot of the
assert
code has to be run. If you look inside it it has several checks / getTestMethodName.. and so on. And the second one uses Kotlin collections to filter out results, running the predicate only in the scope of the "filter" function, thus slimming the results down and only then running some assertion. In any case it shows that Konsist assertions (at least some of them) are not very optimised at this point
👍 1
s

stelios

03/29/2024, 9:55 AM
Right, that makes sense. Should then the Konsist APIs do a filtering first?
v

Vidmantas Kerbelis

03/29/2024, 11:39 AM
Depends on the inner workings of the Konsist assertion codebase. Can't quite tell what the approach should be without looking into it more thoroughly. 😄 But changing
List<E>.assertFalse {}
into
List<E> -> filter -> assertEmpty
implementation might lead to some cases failing 🤔 So, hopefully someone spends some time on optimizations in the codebase. Personally I only use it for like 5 tests, so even if each test takes 1s, it's negligible for my project.
2 Views