JAL-3477 Added 10k leeway to amount of memory reserved for OS test for float rounding...
[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
42     // smaller)
43     long mem1 = MemorySetting.getMemorySetting("1T", "100");
44     long fullmem = mem1 + 512 * MB; // mem1 gets 512MB removed for the OS
45     long mem1b = MemorySetting.getMemorySetting("1t", "100");
46     assertTrue(mem1 > 1 * GB);
47     assertEquals(mem1, mem1b);
48
49     // test 10% memory. Note 512MB is set as minimum, so adjust to 50% if less
50     // than
51     // 5GB RAM.
52     String pc;
53     Float pcf;
54     if (mem1 > 5 * GB)
55     {
56       pc = "10";
57       pcf = 0.1f;
58     }
59     else
60     {
61       pc = "50";
62       pcf = 0.5f;
63     }
64     long mem1c = MemorySetting.getMemorySetting("1T", pc);
65     assertTrue(mem1c > (pcf - 0.01) * fullmem
66             && mem1c < (pcf + 0.01) * fullmem); // allowing for floating point
67                                                 // errors
68
69     // should return 1GB (assuming host machine has more than 1GB RAM)
70     long mem2 = MemorySetting.getMemorySetting("1G", "100");
71     long mem2b = MemorySetting.getMemorySetting("1g", "100");
72     assertEquals(mem2, 1 * GB);
73     assertEquals(mem2, mem2b);
74
75     long mem3 = MemorySetting.getMemorySetting("1024M", "100");
76     long mem3b = MemorySetting.getMemorySetting("1024m", "100");
77     assertEquals(mem3, 1024 * MB);
78     assertEquals(mem3, mem3b);
79
80     long mem4 = MemorySetting.getMemorySetting("1048576K", "100");
81     long mem4b = MemorySetting.getMemorySetting("1048576k", "100");
82     assertEquals(mem4, 1048576 * KB);
83     assertEquals(mem4, mem4b);
84
85     long mem5 = MemorySetting.getMemorySetting("1073741824B", "100");
86     long mem5b = MemorySetting.getMemorySetting("1073741824b", "100");
87     long mem5c = MemorySetting.getMemorySetting("1073741824", "100");
88     assertEquals(mem5, 1073741824L);
89     assertEquals(mem5, mem5b);
90     assertEquals(mem5, mem5c);
91
92     // check g, m, k, b, "" acting as they should
93     assertEquals(mem2, mem3);
94     assertEquals(mem2, mem4);
95     assertEquals(mem2, mem5);
96
97     // default should not be more than 90% memory or 32GB
98     long mem6 = MemorySetting.getMemorySetting();
99     assertTrue(mem6 <= (long) (0.905 * fullmem));
100     assertTrue(mem6 <= 32 * GB);
101
102     // ensure enough memory for application
103     long mem7 = MemorySetting.getMemorySetting("1B", "0.000000001");
104     assertEquals(mem7, 512 * MB);
105
106     // ensure enough memory for OS
107     long mem8 = MemorySetting.getMemorySetting("2T", "100"); // this should be
108                                                              // short of 512MB
109     long mem8b = MemorySetting.getMemorySetting("2T", "50");
110     // allow 10k leeway
111     long diff = mem8b * 2 - mem8;
112     assertTrue(512 * MB - 10 * KB < diff && diff < 512 * MB + 10 * KB);
113     // assertEquals(mem8b * 2 - mem8, 512 * MB);
114   }
115
116 }