2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.common
;
11 import org
.apache
.logging
.log4j
.LogManager
;
12 import org
.apache
.logging
.log4j
.Logger
;
13 import org
.junit
.Assert
;
14 import org
.junit
.Test
;
17 * @author a.kohlbecker
21 public class JvmMonitorTest
extends Assert
{
24 public static final Logger logger
= LogManager
.getLogger(JvmMonitorTest
.class);
27 public void testGcTime() {
28 JvmMonitor jvmMonitor
= new JvmMonitor();
29 assertNotEquals(-1l, jvmMonitor
.gcTime());
31 Runtime
.getRuntime().gc();
32 Runtime
.getRuntime().gc();
33 Runtime
.getRuntime().gc();
34 long gcTimeLast_1
= jvmMonitor
.getGCtimeSiceLastCheck();
35 assertTrue(gcTimeLast_1
> 0);
36 Runtime
.getRuntime().gc();
37 Runtime
.getRuntime().gc();
38 Runtime
.getRuntime().gc();
39 long gcTimeLast_2
= jvmMonitor
.getGCtimeSiceLastCheck();
40 assertTrue(gcTimeLast_1
> 0);
41 assertTrue(jvmMonitor
.gcTime() > gcTimeLast_2
);
46 public void testHeapUsage() {
48 int failWithMB
= 300 * MB
;
49 JvmMonitor jvmMonitor
= new JvmMonitor();
51 long baseline
= jvmMonitor
.getHeapMemoryUsage().getUsed();
52 logger
.debug("before: " + baseline
);
54 assertTrue(jvmMonitor.hasFreeHeap(0.9));
56 logger.setLevel(Level.DEBUG);
58 Object[] measure = new Object[MB]; // 1MB
59 double bytePerObject = (jvmMonitor.getHeapMemoryUsage().getUsed() - baseline) / MB;
60 long maxHeap = jvmMonitor.getHeapMemoryUsage().getMax();
61 logger.debug("max: " + maxHeap);
62 Object[] heapEater = new Object[(int)Math.round((failWithMB / bytePerObject))];
63 logger.debug("after: " + jvmMonitor.getHeapMemoryUsage().getUsed());
65 assertFalse(jvmMonitor.hasFreeHeap((failWithMB * 2) / (double)maxHeap));