~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to memory-tests/src/test/java/com/gs/collections/impl/memory/MemoryTestBench.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2000-2013 Heinz Max Kabutz
 
3
 *
 
4
 * See the NOTICE file distributed with this work for additional
 
5
 * information regarding copyright ownership.  Heinz Max Kabutz licenses
 
6
 * this file to you under the Apache License, Version 2.0 (the "License");
 
7
 * you may not use this file except in compliance with the License. You may
 
8
 * 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, software
 
13
 * distributed under the License is distributed on an "AS IS" BASIS,
 
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
 * See the License for the specific language governing permissions and
 
16
 * limitations under the License.
 
17
 */
 
18
 
 
19
package com.gs.collections.impl.memory;
 
20
 
 
21
import java.text.NumberFormat;
 
22
 
 
23
import com.gs.collections.api.block.function.Function0;
 
24
import com.gs.collections.api.block.procedure.primitive.IntProcedure;
 
25
import com.gs.collections.impl.list.Interval;
 
26
import org.slf4j.Logger;
 
27
import org.slf4j.LoggerFactory;
 
28
 
 
29
public final class MemoryTestBench
 
30
{
 
31
    private static final Logger LOGGER = LoggerFactory.getLogger(MemoryTestBench.class);
 
32
 
 
33
    private static final GCAndSleepProcedure GC_AND_SLEEP_PROCEDURE = new GCAndSleepProcedure();
 
34
    private static final Interval GC_INTERVAL = Interval.oneTo(20);
 
35
    private final Class<?> clazz;
 
36
 
 
37
    private MemoryTestBench(Class<?> clazz)
 
38
    {
 
39
        this.clazz = clazz;
 
40
    }
 
41
 
 
42
    public static MemoryTestBench on(Class<?> clazz)
 
43
    {
 
44
        return new MemoryTestBench(clazz);
 
45
    }
 
46
 
 
47
    /**
 
48
     * From newsletter 193
 
49
     * (http://www.javaspecialists.eu/archive/Issue193.html).  Used
 
50
     * to estimate memory usage by objects.
 
51
     */
 
52
    public long calculateMemoryUsage(Function0<?> factory)
 
53
    {
 
54
        // Clean the slate and prep
 
55
        this.forceGCAndSleepMultipleTimes();
 
56
        Object container = factory.value();
 
57
        if (!this.clazz.isInstance(container))
 
58
        {
 
59
            throw new RuntimeException();
 
60
        }
 
61
        long memory = this.currentUsedMemory();
 
62
        //noinspection UnusedAssignment,ReuseOfLocalVariable
 
63
        container = null;
 
64
        this.forceGCAndSleepMultipleTimes();
 
65
 
 
66
        // Calculate memory before creation
 
67
        long memory2 = this.currentUsedMemory();
 
68
        //noinspection UnusedAssignment,ReuseOfLocalVariable
 
69
        container = factory.value();
 
70
        // Get rid of transient garbage
 
71
        this.forceGCAndSleepMultipleTimes();
 
72
        // Calculate new used memory
 
73
        return this.currentUsedMemory() - memory2;
 
74
    }
 
75
 
 
76
    private long currentUsedMemory()
 
77
    {
 
78
        Runtime runtime = Runtime.getRuntime();
 
79
        return runtime.totalMemory() - runtime.freeMemory();
 
80
    }
 
81
 
 
82
    private void forceGCAndSleepMultipleTimes()
 
83
    {
 
84
        GC_INTERVAL.forEach(GC_AND_SLEEP_PROCEDURE);
 
85
    }
 
86
 
 
87
    public void printContainerMemoryUsage(String category, int size, Function0<?> factory)
 
88
    {
 
89
        String memoryUsedInBytes = NumberFormat.getInstance().format(this.calculateMemoryUsage(factory));
 
90
        String sizeFormatted = NumberFormat.getInstance().format(size);
 
91
        LOGGER.info("{} {} size {} bytes {}", category, this.clazz.getName(), sizeFormatted, memoryUsedInBytes);
 
92
    }
 
93
 
 
94
    private static class GCAndSleepProcedure implements IntProcedure
 
95
    {
 
96
        @Override
 
97
        public void value(int each)
 
98
        {
 
99
            System.gc();
 
100
            try
 
101
            {
 
102
                Thread.sleep(100);
 
103
            }
 
104
            catch (InterruptedException e)
 
105
            {
 
106
                throw new RuntimeException(e);
 
107
            }
 
108
        }
 
109
    }
 
110
}