JAL-3477 New sensible default decisions on memory, and new jvmmemmax setting for...
[jalview.git] / test / jalview / bin / MemorySettingTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import org.testng.annotations.Test;
27
28 public class MemorySettingTest
29 {
30
31   @Test(groups = "Functional")
32   public void testGetMemorySetting()
33   {
34     long KB = 1024;
35     long MB = KB * KB;
36     long GB = MB * KB;
37     // long TB = GB * KB;
38     
39     /* some of these tests assume a host machine with RAM somewhere between 1GB and 1TB */
40
41     // should return 100% of physical memory available (or 1TB whichever is smaller)
42     long mem1 = MemorySetting.getMemorySetting("1T", "100");
43     long fullmem = mem1 + 512 * MB; // mem1 gets 512MB removed for the OS
44     long mem1b = MemorySetting.getMemorySetting("1t", "100");
45     assertTrue(mem1 > 1 * GB);
46     assertEquals(mem1, mem1b);
47     
48     // test 10% memory. Note 512MB is set as minimum, so adjust to 50% if less than
49     // 5GB RAM.
50     String pc;
51     Float pcf;
52     if (mem1 > 5 * GB)
53     {
54       pc = "10";
55       pcf = 0.1f;
56     }
57     else
58     {
59       pc = "50";
60       pcf = 0.5f;
61     }
62     long mem1c = MemorySetting.getMemorySetting("1T", pc);
63     assertTrue(mem1c > (pcf - 0.01) * fullmem && mem1c < (pcf + 0.01) * fullmem); // allowing for floating point errors
64     
65     // should return 1GB (assuming host machine has more than 1GB RAM)
66     long mem2 = MemorySetting.getMemorySetting("1G", "100");
67     long mem2b = MemorySetting.getMemorySetting("1g", "100");
68     assertEquals(mem2, 1 * GB);
69     assertEquals(mem2, mem2b);
70     
71     long mem3 = MemorySetting.getMemorySetting("1024M", "100");
72     long mem3b = MemorySetting.getMemorySetting("1024m", "100");
73     assertEquals(mem3, 1024 * MB);
74     assertEquals(mem3, mem3b);
75     
76     long mem4 = MemorySetting.getMemorySetting("1048576K", "100");
77     long mem4b = MemorySetting.getMemorySetting("1048576k", "100");
78     assertEquals(mem4, 1048576 * KB);
79     assertEquals(mem4, mem4b);
80
81     long mem5 = MemorySetting.getMemorySetting("1073741824B", "100");
82     long mem5b = MemorySetting.getMemorySetting("1073741824b", "100");
83     long mem5c = MemorySetting.getMemorySetting("1073741824", "100");
84     assertEquals(mem5, 1073741824L);
85     assertEquals(mem5, mem5b);
86     assertEquals(mem5, mem5c);
87
88     // check g, m, k, b, "" acting as they should
89     assertEquals(mem2, mem3);
90     assertEquals(mem2, mem4);
91     assertEquals(mem2, mem5);
92
93     // default should not be more than 90% memory or 32GB
94     long mem6 = MemorySetting.getMemorySetting();
95     assertTrue(mem6 <= (long) (0.905 * fullmem));
96     assertTrue(mem6 <= 32 * GB);
97
98     // ensure enough memory for application
99     long mem7 = MemorySetting.getMemorySetting("1B", "0.000000001");
100     assertEquals(mem7, 512 * MB);
101
102     // ensure enough memory for OS
103     long mem8 = MemorySetting.getMemorySetting("2TB", "100"); // this should be short of 512MB
104     long mem8b = MemorySetting.getMemorySetting("2TB", "50");
105     assertEquals(mem8b * 2 - mem8, 512 * MB);
106   }
107
108 }