Interesting performance issue: The following test...
# konsist
s
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
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
👍 2
s
Right, that makes sense. Should then the Konsist APIs do a filtering first?
v
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.