All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.activemq.tool.MemoryMonitoringTool Maven / Gradle / Ivy

There is a newer version: 5.7.0
Show newest version
/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.tool;

import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;


import java.io.DataOutputStream;
import java.util.Properties;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MemoryMonitoringTool implements Runnable {

    private long checkpointInterval = 5000;          // 5 sec sample checkpointInterval
    private long resultIndex = 0;

    private AtomicBoolean isRunning = new AtomicBoolean(false);
    private DataOutputStream dataDoutputStream = null;

    protected Properties testSettings = new Properties();
    protected ReportGenerator reportGenerator = new ReportGenerator();
    private MemoryMXBean memoryBean;

    public Properties getTestSettings() {
        return testSettings;
    }

    public void setTestSettings(Properties sysTestSettings) {
        this.testSettings = sysTestSettings;
    }

    public DataOutputStream getDataOutputStream() {
        return dataDoutputStream;
    }

    public void setDataOutputStream(DataOutputStream dataDoutputStream) {
        this.dataDoutputStream = dataDoutputStream;
    }


    public void stopMonitor() {
        isRunning.set(false);
    }


    public long getCheckpointInterval() {
        return checkpointInterval;
    }

    public void setCheckpointInterval(long checkpointInterval) {
        this.checkpointInterval = checkpointInterval;
    }


    public Thread startMonitor() {

        String intervalStr = this.getTestSettings().getProperty("checkpoint_interval");
        checkpointInterval = new Integer(intervalStr).intValue();
        this.getTestSettings().remove("checkpoint_interval");

        memoryBean = ManagementFactory.getMemoryMXBean();
        reportGenerator.setTestSettings(getTestSettings());
        addTestInformation();

        Thread t = new Thread(this);
        t.setName("Memory monitoring tool");
        isRunning.set(true);
        t.start();

        return t;

    }


    public void addTestInformation() {
        reportGenerator.setReportName(this.getTestSettings().getProperty("report_name"));
        reportGenerator.setReportDirectory(this.getTestSettings().getProperty("report_directory"));
        reportGenerator.startGenerateReport();

        reportGenerator.addTestInformation();
        reportGenerator.writeWithIndent(4, "");
        reportGenerator.writeWithIndent(6, "");
        reportGenerator.writeWithIndent(8, "" + memoryBean.getHeapMemoryUsage().getCommitted() + "");
        reportGenerator.writeWithIndent(8, "" + memoryBean.getHeapMemoryUsage().getMax() + "");
        reportGenerator.writeWithIndent(6, "");
        reportGenerator.writeWithIndent(6, "");
        reportGenerator.writeWithIndent(8, "" + memoryBean.getNonHeapMemoryUsage().getCommitted() + "");
        reportGenerator.writeWithIndent(8, "" + memoryBean.getNonHeapMemoryUsage().getMax() + "");
        reportGenerator.writeWithIndent(6, "");
        reportGenerator.writeWithIndent(4, "");

        reportGenerator.addClientSettings();
        reportGenerator.endTestInformation();
    }


    public void run() {

        long nonHeapMB = 0;
        long heapMB = 0;
        long oneMB = 1024 * 1024;

        reportGenerator.startTestResult(getCheckpointInterval());
        while (isRunning.get()) {

            try {
                //wait every check point before getting the next memory usage
                Thread.sleep(checkpointInterval);

                nonHeapMB = memoryBean.getNonHeapMemoryUsage().getUsed() / oneMB;
                heapMB = memoryBean.getHeapMemoryUsage().getUsed() / oneMB;

                reportGenerator.writeWithIndent(6, "");

                resultIndex++;

            } catch (Exception e) {
                e.printStackTrace();

            }


        }
        reportGenerator.endTestResult();
        reportGenerator.stopGenerateReport();

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy