main

square/leakcanary

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

ShallowSizeCalculatorTest.kt

TLDR

This file contains a test class called ShallowSizeCalculatorTest that tests the functionality of the ShallowSizeCalculator class. It verifies the calculations of the shallow size of different types of Java classes using a provided heap dump file.

Methods

setUp()

This method is a setup method annotated with @Before. It initializes the hprofFile variable with a temporary file created by the testFolder rule.

empty class has instance size 0

This test method verifies that an empty class has an instance size of 0. It creates a heap dump file with an instance of the class "SomeClass" and then calculates the shallow size of the instance using the ShallowSizeCalculator class. The expected result is that the instance size is 0.

class with static field has instance size 0

This test method verifies that a class with a static field has an instance size of 0. It creates a heap dump file with an instance of the class "SomeClass" that has a static field called "someStaticField" of type LongHolder. The test calculates the shallow size of the instance using the ShallowSizeCalculator class. The expected result is that the instance size is 0.

class with int field has instance size 4

This test method verifies that a class with an int field has an instance size of 4. It creates a heap dump file with an instance of the class "SomeClass" that has a field called "someIntField" of type IntHolder. The test calculates the shallow size of the instance using the ShallowSizeCalculator class. The expected result is that the instance size is 4.

empty class has size EMPTY_CLASS_SIZE

This test method verifies that an empty class has a size equal to the constant EMPTY_CLASS_SIZE. It creates a heap dump file with a class called "SomeClass" and then calculates the shallow size of the class using the ShallowSizeCalculator class. The expected result is that the class size is equal to the value of EMPTY_CLASS_SIZE.

class with static int field has class size that includes field

This test method verifies that a class with a static int field has a class size that includes the field. It creates a heap dump file with a class called "SomeClass" that has a static field called "someStaticField" of type IntHolder. The test calculates the shallow size of the class using the ShallowSizeCalculator class. The expected result is that the class size is equal to the sum of EMPTY_CLASS_SIZE and the size occupied by the field (field ID, field type, and field value).

Classes

ShallowSizeCalculatorTest

This class contains test methods that verify the functionality of the ShallowSizeCalculator class. It creates heap dump files with different instances and classes and calculates the shallow sizes using the ShallowSizeCalculator class.

package shark.internal

import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import shark.HprofHeapGraph.Companion.openHeapGraph
import shark.ValueHolder.IntHolder
import shark.ValueHolder.LongHolder
import shark.dump
import java.io.File

private const val EMPTY_CLASS_SIZE = 42

class ShallowSizeCalculatorTest {

  @get:Rule
  var testFolder = TemporaryFolder()
  private lateinit var hprofFile: File

  @Before
  fun setUp() {
    hprofFile = testFolder.newFile("temp.hprof")
  }

  @Test fun `empty class has instance size 0`() {
    hprofFile.dump {
      "SomeClass" instance {}
    }

    val instanceSize = hprofFile.openHeapGraph().use { graph ->
      val calculator = ShallowSizeCalculator(graph)
      calculator.computeShallowSize(
        graph.findClassByName("SomeClass")!!.instances.single().objectId
      )
    }

    assertThat(instanceSize).isEqualTo(0)
  }

  @Test fun `class with static field has instance size 0`() {
    hprofFile.dump {
      "SomeClass" instance {
        staticField["someStaticField"] = LongHolder(42)
      }
    }

    val instanceSize = hprofFile.openHeapGraph().use { graph ->
      val calculator = ShallowSizeCalculator(graph)
      calculator.computeShallowSize(
        graph.findClassByName("SomeClass")!!.instances.single().objectId
      )
    }

    assertThat(instanceSize).isEqualTo(0)
  }

  @Test fun `class with int field has instance size 4`() {
    hprofFile.dump {
      "SomeClass" instance {
        field["someIntField"] = IntHolder(42)
      }
    }

    val instanceSize = hprofFile.openHeapGraph().use { graph ->
      val calculator = ShallowSizeCalculator(graph)
      calculator.computeShallowSize(
        graph.findClassByName("SomeClass")!!.instances.single().objectId
      )
    }
    assertThat(instanceSize).isEqualTo(4)
  }

  @Test fun `empty class has size EMPTY_CLASS_SIZE`() {
    hprofFile.dump {
      "SomeClass" clazz {}
    }

    val classSize = hprofFile.openHeapGraph().use { graph ->
      val calculator = ShallowSizeCalculator(graph)
      calculator.computeShallowSize(graph.findClassByName("SomeClass")!!.objectId)
    }
    assertThat(classSize).isEqualTo(EMPTY_CLASS_SIZE)
  }

  @Test fun `class with static int field has class size that includes field`() {
    hprofFile.dump {
      "SomeClass" clazz {
        staticField["someStaticField"] = IntHolder(42)
      }
    }

    val classSize = hprofFile.openHeapGraph().use { graph ->
      val calculator = ShallowSizeCalculator(graph)
      calculator.computeShallowSize(graph.findClassByName("SomeClass")!!.objectId)
    }

    val bytesForFieldId = 4
    val bytesForFieldType = 1
    val bytesForFieldValue = 4

    assertThat(classSize).isEqualTo(
      EMPTY_CLASS_SIZE + bytesForFieldId + bytesForFieldType + bytesForFieldValue
    )
  }
}