001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.test;
016    
017    import com.liferay.portal.kernel.process.ProcessUtil;
018    import com.liferay.portal.kernel.util.HeapUtil;
019    
020    import java.util.Date;
021    import java.util.concurrent.Future;
022    
023    import org.junit.rules.TestRule;
024    import org.junit.runner.Description;
025    import org.junit.runners.model.Statement;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class HeapDumpTestRule implements TestRule {
031    
032            public static final HeapDumpTestRule INSTANCE = new HeapDumpTestRule(true);
033    
034            public HeapDumpTestRule(boolean live) {
035                    _live = live;
036            }
037    
038            @Override
039            public Statement apply(
040                    final Statement statement, final Description description) {
041    
042                    return new Statement() {
043    
044                            @Override
045                            public void evaluate() throws Throwable {
046                                    Date date = new Date();
047    
048                                    GCUtil.fullGC(_live);
049    
050                                    Future<?> future = HeapUtil.heapDump(
051                                            _live, true,
052                                            description.toString() + "-" + date + "-before.bin",
053                                            ProcessUtil.ECHO_OUTPUT_PROCESSOR);
054    
055                                    future.get();
056    
057                                    try {
058                                            statement.evaluate();
059                                    }
060                                    finally {
061                                            GCUtil.fullGC(_live);
062    
063                                            future = HeapUtil.heapDump(
064                                                    _live, true,
065                                                    description.toString() + "-" + date + "-after.bin",
066                                                    ProcessUtil.ECHO_OUTPUT_PROCESSOR);
067    
068                                            future.get();
069                                    }
070                            }
071    
072                    };
073            }
074    
075            private final boolean _live;
076    
077    }