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.log4j.Logger;
|
12
|
import org.junit.Assert;
|
13
|
import org.junit.Test;
|
14
|
|
15
|
/**
|
16
|
* @author a.kohlbecker
|
17
|
* @since Jul 1, 2016
|
18
|
*
|
19
|
*/
|
20
|
public class JvmMonitorTest extends Assert{
|
21
|
|
22
|
|
23
|
public static final Logger logger = Logger.getLogger(JvmMonitorTest.class);
|
24
|
|
25
|
@Test
|
26
|
public void testGcTime() {
|
27
|
JvmMonitor jvmMonitor = new JvmMonitor();
|
28
|
assertNotEquals(-1l, jvmMonitor.gcTime());
|
29
|
|
30
|
Runtime.getRuntime().gc();
|
31
|
Runtime.getRuntime().gc();
|
32
|
Runtime.getRuntime().gc();
|
33
|
long gcTimeLast_1 = jvmMonitor.getGCtimeSiceLastCheck();
|
34
|
assertTrue(gcTimeLast_1 > 0);
|
35
|
Runtime.getRuntime().gc();
|
36
|
Runtime.getRuntime().gc();
|
37
|
Runtime.getRuntime().gc();
|
38
|
long gcTimeLast_2 = jvmMonitor.getGCtimeSiceLastCheck();
|
39
|
assertTrue(gcTimeLast_1 > 0);
|
40
|
assertTrue(jvmMonitor.gcTime() > gcTimeLast_2);
|
41
|
|
42
|
}
|
43
|
|
44
|
@Test
|
45
|
public void testHeapUsage() {
|
46
|
int MB = 1024 * 1024;
|
47
|
int failWithMB = 300 * MB;
|
48
|
JvmMonitor jvmMonitor = new JvmMonitor();
|
49
|
|
50
|
long baseline = jvmMonitor.getHeapMemoryUsage().getUsed();
|
51
|
logger.debug("before: " + baseline);
|
52
|
/*
|
53
|
assertTrue(jvmMonitor.hasFreeHeap(0.9));
|
54
|
|
55
|
logger.setLevel(Level.DEBUG);
|
56
|
|
57
|
Object[] measure = new Object[MB]; // 1MB
|
58
|
double bytePerObject = (jvmMonitor.getHeapMemoryUsage().getUsed() - baseline) / MB;
|
59
|
long maxHeap = jvmMonitor.getHeapMemoryUsage().getMax();
|
60
|
logger.debug("max: " + maxHeap);
|
61
|
Object[] heapEater = new Object[(int)Math.round((failWithMB / bytePerObject))];
|
62
|
logger.debug("after: " + jvmMonitor.getHeapMemoryUsage().getUsed());
|
63
|
|
64
|
assertFalse(jvmMonitor.hasFreeHeap((failWithMB * 2) / (double)maxHeap));
|
65
|
*/
|
66
|
|
67
|
}
|
68
|
|
69
|
}
|