src/jdk.internal.vm.compiler/share/classes/org.graalvm.util.test/src/org/graalvm/util/test/CollectionSizeTest.java
author iveresov
Fri, 02 Feb 2018 17:28:17 -0800
changeset 48861 47f19ff9903c
child 49873 26ebfe8ce852
permissions -rw-r--r--
8194819: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     1
/*
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
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
 */
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    23
package org.graalvm.util.test;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    24
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    25
import static org.junit.Assert.assertEquals;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    26
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    27
import org.graalvm.collections.EconomicMap;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    28
import org.graalvm.collections.Equivalence;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    29
import org.graalvm.compiler.test.GraalTest;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    30
import org.graalvm.util.ObjectSizeEstimate;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    31
import org.junit.Assume;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    32
import org.junit.Test;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    33
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    34
public class CollectionSizeTest {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    35
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    36
    /**
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    37
     * 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
    38
     */
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    39
    @Test
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    40
    public void testSize() {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    41
        Assume.assumeTrue("Not working in JDK9 due to module visibility.", GraalTest.Java8OrEarlier);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    42
        EconomicMap<Object, Object> map = EconomicMap.create(Equivalence.IDENTITY);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    43
        assertEquals(49, ObjectSizeEstimate.forObject(map).getTotalBytes());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    44
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    45
        Integer value = 1;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    46
        map.put(value, value);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    47
        assertEquals(153, ObjectSizeEstimate.forObject(map).getTotalBytes());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    48
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    49
        Integer secondValue = 2;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    50
        map.put(secondValue, secondValue);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    51
        assertEquals(153 + 20, ObjectSizeEstimate.forObject(map).getTotalBytes());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    52
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    53
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    54
    /**
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    55
     * Tests whether the map actually compresses the entries array when a large number of entries
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    56
     * are deleted.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    57
     */
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    58
    @Test
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    59
    public void testCompress() {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    60
        Assume.assumeTrue("Not working in JDK9 due to module visibility.", GraalTest.Java8OrEarlier);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    61
        EconomicMap<Object, Object> map = EconomicMap.create();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    62
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    63
        // Measuring size of map with one entry.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    64
        Object firstValue = 0;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    65
        map.put(firstValue, firstValue);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    66
        ObjectSizeEstimate afterFirstValue = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    67
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    68
        // Add 999 more entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    69
        for (int i = 1; i < 1000; ++i) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    70
            Object value = i;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    71
            map.put(value, value);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    72
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    73
        ObjectSizeEstimate beforeRemove = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    74
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    75
        // Remove 999 first entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    76
        for (int i = 0; i < 999; ++i) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    77
            map.removeKey(i);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    78
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    79
        ObjectSizeEstimate afterRemove = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    80
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    81
        // Check that size is same size as with one entry.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    82
        assertEquals(afterFirstValue, afterRemove);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    83
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    84
        // Add 999 new entries.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    85
        for (int i = 0; i < 999; ++i) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    86
            Object value = i;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    87
            map.put(value, value);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    88
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    89
        ObjectSizeEstimate afterAdd = ObjectSizeEstimate.forObject(map);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    90
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    91
        // Check that entries array is same size again.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    92
        assertEquals(beforeRemove.getPointerCount(), afterAdd.getPointerCount());
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    93
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    94
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    95
}