Created
May 14, 2019 19:03
-
-
Save ygrenzinger/d4e02949e0075f0e579e940d80bdbd57 to your computer and use it in GitHub Desktop.
Mockk slowness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kata.tetris | |
import io.mockk.every | |
import io.mockk.mockk | |
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.jupiter.api.Test | |
/* | |
<dependency> | |
<groupId>io.mockk</groupId> | |
<artifactId>mockk</artifactId> | |
<version>1.9</version> | |
<scope>test</scope> | |
</dependency> | |
*/ | |
typealias Foo = String | |
class FooLoader { | |
fun allFoos() : List<Foo> { | |
return listOf("com", "kata", "tetris") | |
} | |
} | |
class RandomFoo(private val fooLoader: FooLoader) { | |
fun giveFoo() : Foo { | |
val foos = fooLoader.allFoos() | |
return foos[(0 until foos.size).random()] | |
} | |
} | |
class MockkSlownessTest { | |
@Test | |
fun tooSlow() { | |
val time = System.currentTimeMillis() | |
val fooLoader = mockk<FooLoader>() | |
every { fooLoader.allFoos() } returns listOf("mock","slowness", "test") | |
println("Time elapsed : ${System.currentTimeMillis() - time} ms") | |
// Almost 3.5 seconds to init mock | |
val randomFoo = RandomFoo(fooLoader) | |
assertThat(randomFoo.giveFoo()).isIn("mock","slowness", "test") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check the following code:
Gives me following answer:
The first number - is the time of subclassing operation in ByteBuddy with all intialization.
Second - is transformation cost + 100000 mocks
And 3, 4 is just 100000 mocks.
So 0,005 ms per mock after class was transformed. But alike 1200 ms to transform.