src/jdk.internal.vm.compiler/share/classes/org.graalvm.util.test/src/org/graalvm/util/test/CollectionSizeTest.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     4
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     8
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    14
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    18
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    24
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    25
package org.graalvm.util.test;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    26
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    27
import static org.junit.Assert.assertEquals;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    28
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 48861
diff changeset
    29
import jdk.internal.vm.compiler.collections.EconomicMap;
26ebfe8ce852 8199755: Update Graal
dlong
parents: 48861
diff changeset
    30
import jdk.internal.vm.compiler.collections.Equivalence;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    31
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    32
import org.graalvm.util.ObjectSizeEstimate;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    33
import org.junit.Assume;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    34
import org.junit.Test;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    35
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    36
public class CollectionSizeTest {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    37
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    38
    /**
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    39
     * Tests the memory size of an empty map and a map with only one or two entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    40
     */
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    41
    @Test
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    42
    public void testSize() {
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    43
        Assume.assumeTrue("Not working in JDK9 due to module visibility.", JavaVersionUtil.JAVA_SPEC <= 8);
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    44
        EconomicMap<Object, Object> map = EconomicMap.create(Equivalence.IDENTITY);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    45
        assertEquals(49, ObjectSizeEstimate.forObject(map).getTotalBytes());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    46
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    47
        Integer value = 1;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    48
        map.put(value, value);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    49
        assertEquals(153, ObjectSizeEstimate.forObject(map).getTotalBytes());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    50
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    51
        Integer secondValue = 2;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    52
        map.put(secondValue, secondValue);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    53
        assertEquals(153 + 20, ObjectSizeEstimate.forObject(map).getTotalBytes());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    54
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    55
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    56
    /**
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    57
     * Tests whether the map actually compresses the entries array when a large number of entries
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    58
     * are deleted.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    59
     */
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    60
    @Test
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    61
    public void testCompress() {
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    62
        Assume.assumeTrue("Not working in JDK9 due to module visibility.", JavaVersionUtil.JAVA_SPEC <= 8);
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    63
        EconomicMap<Object, Object> map = EconomicMap.create();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    64
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    65
        // Measuring size of map with one entry.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    66
        Object firstValue = 0;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    67
        map.put(firstValue, firstValue);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    68
        ObjectSizeEstimate afterFirstValue = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    69
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    70
        // Add 999 more entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    71
        for (int i = 1; i < 1000; ++i) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    72
            Object value = i;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    73
            map.put(value, value);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    74
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    75
        ObjectSizeEstimate beforeRemove = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    76
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    77
        // Remove 999 first entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    78
        for (int i = 0; i < 999; ++i) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    79
            map.removeKey(i);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    80
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    81
        ObjectSizeEstimate afterRemove = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    82
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    83
        // Check that size is same size as with one entry.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    84
        assertEquals(afterFirstValue, afterRemove);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    85
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    86
        // Add 999 new entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    87
        for (int i = 0; i < 999; ++i) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    88
            Object value = i;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    89
            map.put(value, value);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    90
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    91
        ObjectSizeEstimate afterAdd = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    92
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    93
        // Check that entries array is same size again.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    94
        assertEquals(beforeRemove.getPointerCount(), afterAdd.getPointerCount());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    95
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    96
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    97
}