|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.tools.launchers; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertNull; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | +import static org.junit.Assert.fail; |
| 32 | + |
| 33 | +public class JvmErgonomicsTests extends LaunchersTestCase { |
| 34 | + public void testExtractValidHeapSize() { |
| 35 | + assertEquals(Long.valueOf(1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx1024"))); |
| 36 | + assertEquals(Long.valueOf(2L * 1024 * 1024 * 1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx2g"))); |
| 37 | + assertEquals(Long.valueOf(32 * 1024 * 1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx32M"))); |
| 38 | + assertEquals(Long.valueOf(32 * 1024 * 1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-XX:MaxHeapSize=32M"))); |
| 39 | + } |
| 40 | + |
| 41 | + public void testExtractInvalidHeapSize() { |
| 42 | + try { |
| 43 | + JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx2T")); |
| 44 | + fail("Expected IllegalArgumentException to be raised"); |
| 45 | + } catch (IllegalArgumentException expected) { |
| 46 | + assertEquals("Unknown unit [T] for max heap size in [-Xmx2T]", expected.getMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void testExtractNoHeapSize() { |
| 51 | + assertNull("No spaces allowed", JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx 1024"))); |
| 52 | + assertNull("JVM option is not present", JvmErgonomics.extractHeapSize(Collections.singletonList(""))); |
| 53 | + assertNull("Multiple JVM options per line", JvmErgonomics.extractHeapSize(Collections.singletonList("-Xms2g -Xmx2g"))); |
| 54 | + } |
| 55 | + |
| 56 | + public void testExtractSystemProperties() { |
| 57 | + Map<String, String> expectedSystemProperties = new HashMap<>(); |
| 58 | + expectedSystemProperties.put("file.encoding", "UTF-8"); |
| 59 | + expectedSystemProperties.put("kv.setting", "ABC=DEF"); |
| 60 | + |
| 61 | + Map<String, String> parsedSystemProperties = JvmErgonomics.extractSystemProperties( |
| 62 | + Arrays.asList("-Dfile.encoding=UTF-8", "-Dkv.setting=ABC=DEF")); |
| 63 | + |
| 64 | + assertEquals(expectedSystemProperties, parsedSystemProperties); |
| 65 | + } |
| 66 | + |
| 67 | + public void testExtractNoSystemProperties() { |
| 68 | + Map<String, String> parsedSystemProperties = JvmErgonomics.extractSystemProperties(Arrays.asList("-Xms1024M", "-Xmx1024M")); |
| 69 | + assertTrue(parsedSystemProperties.isEmpty()); |
| 70 | + } |
| 71 | + |
| 72 | + public void testLittleMemoryErgonomicChoices() { |
| 73 | + String smallHeap = randomFrom(Arrays.asList("64M", "512M", "1024M", "1G")); |
| 74 | + List<String> expectedChoices = Collections.singletonList("-Dio.netty.allocator.type=unpooled"); |
| 75 | + assertEquals(expectedChoices, JvmErgonomics.choose(Arrays.asList("-Xms" + smallHeap, "-Xmx" + smallHeap))); |
| 76 | + } |
| 77 | + |
| 78 | + public void testPlentyMemoryErgonomicChoices() { |
| 79 | + String largeHeap = randomFrom(Arrays.asList("1025M", "2048M", "2G", "8G")); |
| 80 | + List<String> expectedChoices = Collections.singletonList("-Dio.netty.allocator.type=pooled"); |
| 81 | + assertEquals(expectedChoices, JvmErgonomics.choose(Arrays.asList("-Xms" + largeHeap, "-Xmx" + largeHeap))); |
| 82 | + } |
| 83 | +} |
0 commit comments