IndexingTest.kt
TLDR
This file contains a single class IndexingTest
which has a single method indexHprof
. The indexHprof
method indexes the records of an Hprof file.
Classes
IndexingTest
This class is responsible for indexing the records of an Hprof file. It contains a single method indexHprof
that performs the indexing operation.
Methods
indexHprof
This method performs the indexing of the records of an Hprof file. It does the following steps:
- Retrieves the instrumentation and context objects.
- Creates a file to store the Hprof dump.
- Copies the content of the Hprof dump file from the assets to the created file.
- Creates a
FileSourceProvider
using the created Hprof dump file. - Parses the header of the Hprof dump file.
- Starts indexing the records in the Hprof dump file using the
HprofIndex.indexRecordsOf
method. - Measures the duration of the indexing operation.
- Logs the duration of the indexing operation.
END
package leakcanary
import androidx.test.platform.app.InstrumentationRegistry
import java.io.File
import java.io.FileOutputStream
import leakcanary.internal.friendly.measureDurationMillis
import org.junit.Test
import shark.FileSourceProvider
import shark.HprofHeader
import shark.HprofIndex
import shark.SharkLog
class IndexingTest {
@Test fun indexHprof() {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val context = instrumentation.targetContext
val heapDumpFile = File(context.filesDir, "AnalysisDurationTest.hprof")
context.assets.open("large-dump.hprof").copyTo(FileOutputStream(heapDumpFile))
val sourceProvider = FileSourceProvider(heapDumpFile)
val parsedHeaders = HprofHeader.parseHeaderOf(heapDumpFile)
SharkLog.d { "Start indexing" }
val durationMs = measureDurationMillis {
HprofIndex.indexRecordsOf(
hprofSourceProvider = sourceProvider,
hprofHeader = parsedHeaders
)
}
SharkLog.d { "Indexing took $durationMs ms" }
}
}