@Test
  public void testCompactionTimeStat() throws Exception {
    // Setup off-heap memory for cache
    setSystemPropertiesOnVm(vm, true, getDistributedSystemProperties());

    // Create our off-heap region
    assertThat(createOffHeapRegionOnVm(vm, OFF_HEAP_REGION_NAME, DataPolicy.REPLICATE)).isNotNull();

    // Make sure our off-heap region has off-heap enabled.
    assertOffHeapRegionAttributesOnVm(vm);

    // Make sure our starting off heap stats are correct
    assertOffHeapMetricsOnVm(vm, TOTAL_MEMORY, 0, 0, 0);

    // After allocating large chunck we should still have no compaction time
    doPutOnVm(vm, KEY, new byte[HALF_TOTAL_MEMORY], OFF_HEAP_REGION_NAME, false);
    assertCompactionTimeStatOnVm(vm, 0, ASSERT_OP.EQUAL);

    // After freeing all memory we should have no compaction time
    doDestroyOnVm(vm, KEY, OFF_HEAP_REGION_NAME);
    assertCompactionTimeStatOnVm(vm, 0, ASSERT_OP.EQUAL);

    // Consume all off-heap memory using an allocation size
    int numAllocations = doConsumeOffHeapMemoryOnVm(vm, ALLOCATION_SIZE);
    assertThat(numAllocations > 0).isTrue();

    // Randomly free 3 allocations to produce off-heap gaps
    doFreeOffHeapMemoryOnVm(vm, numAllocations, 3);

    /*
     * Setup a compaction time attribute monitor
     */
    setupOffHeapMonitorOnVm(vm, "OffHeapCompactionTime", 0, 0);
    clearNotificationListenerOnVm(vm);

    // Allocate enough memory to force compaction which will update compaction time stat
    doPutOnVm(vm, KEY, new byte[NEW_ALLOCATION_SIZE], OFF_HEAP_REGION_NAME, true);

    // Make sure our compaction time monitor was triggered
    waitForNotificationListenerOnVm(vm, 5000);

    /*
     * Make sure we have some compaction time. In some environments the compaction time is reported
     * as 0 due to time sample granularity and compaction speed.
     */
    assertCompactionTimeStatOnVm(vm, 0, ASSERT_OP.GREATER_THAN_OR_EQUAL);
  }
Tests the compaction time statistic for off-heap memory.
geode-core/src/distributedTest/java/org/apache/geode/management/OffHeapManagementDUnitTest.java