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.rule;
016    
017    import com.liferay.portal.kernel.process.ProcessUtil;
018    import com.liferay.portal.kernel.test.GCUtil;
019    import com.liferay.portal.kernel.util.HeapUtil;
020    
021    import java.util.Date;
022    import java.util.concurrent.Future;
023    
024    import org.junit.rules.TestRule;
025    import org.junit.runner.Description;
026    import org.junit.runners.model.Statement;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class HeapDumpTestRule implements TestRule {
032    
033            public static final HeapDumpTestRule INSTANCE = new HeapDumpTestRule(true);
034    
035            public HeapDumpTestRule(boolean live) {
036                    _live = live;
037            }
038    
039            @Override
040            public Statement apply(
041                    final Statement statement, final Description description) {
042    
043                    return new Statement() {
044    
045                            @Override
046                            public void evaluate() throws Throwable {
047                                    Date date = new Date();
048    
049                                    GCUtil.fullGC(_live);
050    
051                                    Future<?> future = HeapUtil.heapDump(
052                                            _live, true,
053                                            description.toString() + "-" + date + "-before.bin",
054                                            ProcessUtil.ECHO_OUTPUT_PROCESSOR);
055    
056                                    future.get();
057    
058                                    try {
059                                            statement.evaluate();
060                                    }
061                                    finally {
062                                            GCUtil.fullGC(_live);
063    
064                                            future = HeapUtil.heapDump(
065                                                    _live, true,
066                                                    description.toString() + "-" + date + "-after.bin",
067                                                    ProcessUtil.ECHO_OUTPUT_PROCESSOR);
068    
069                                            future.get();
070                                    }
071                            }
072    
073                    };
074            }
075    
076            private final boolean _live;
077    
078    }