main

square/leakcanary

Last updated at: 29/12/2023 09:37

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:

  1. Retrieves the instrumentation and context objects.
  2. Creates a file to store the Hprof dump.
  3. Copies the content of the Hprof dump file from the assets to the created file.
  4. Creates a FileSourceProvider using the created Hprof dump file.
  5. Parses the header of the Hprof dump file.
  6. Starts indexing the records in the Hprof dump file using the HprofIndex.indexRecordsOf method.
  7. Measures the duration of the indexing operation.
  8. 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" }
  }
}