Skip to content

Instantly share code, notes, and snippets.

@esafirm
Created April 20, 2020 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esafirm/11ddff48914cf8f4e2841a1a4c930963 to your computer and use it in GitHub Desktop.
Save esafirm/11ddff48914cf8f4e2841a1a4c930963 to your computer and use it in GitHub Desktop.
class MemoryProfilerRule : TestRule {
override fun apply(base: Statement?, description: Description?): Statement {
return MemoryProfilerWatcher().apply(base, description)
}
private inner class MemoryProfilerWatcher : TestWatcher() {
private val runtime = Runtime.getRuntime()
private var before: Long = 0
private var after: Long = 0
override fun starting(description: Description?) {
before = getCurrentlyAllocatedMemory()
}
override fun finished(description: Description?) {
after = getCurrentlyAllocatedMemory()
println("""
Before: $before
After: $after
""".trimIndent())
}
fun getCurrentlyAllocatedMemory(): Long {
return (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment