ref #9359 upgrade cdmlib to log4j 2
[cdmlib.git] / cdmlib-commons / src / test / java / eu / etaxonomy / cdm / common / JvmMonitorTest.java
1 /**
2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.common;
10
11 import org.apache.logging.log4j.LogManager;
12 import org.apache.logging.log4j.Logger;
13 import org.junit.Assert;
14 import org.junit.Test;
15
16 /**
17 * @author a.kohlbecker
18 * @since Jul 1, 2016
19 *
20 */
21 public class JvmMonitorTest extends Assert{
22
23
24 public static final Logger logger = LogManager.getLogger(JvmMonitorTest.class);
25
26 @Test
27 public void testGcTime() {
28 JvmMonitor jvmMonitor = new JvmMonitor();
29 assertNotEquals(-1l, jvmMonitor.gcTime());
30
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);
42
43 }
44
45 @Test
46 public void testHeapUsage() {
47 int MB = 1024 * 1024;
48 int failWithMB = 300 * MB;
49 JvmMonitor jvmMonitor = new JvmMonitor();
50
51 long baseline = jvmMonitor.getHeapMemoryUsage().getUsed();
52 logger.debug("before: " + baseline);
53 /*
54 assertTrue(jvmMonitor.hasFreeHeap(0.9));
55
56 logger.setLevel(Level.DEBUG);
57
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());
64
65 assertFalse(jvmMonitor.hasFreeHeap((failWithMB * 2) / (double)maxHeap));
66 */
67
68 }
69
70 }