簡體   English   中英

使用JMeter Java API的Perfmon樣本集合

[英]Perfmon sample collection with JMeter Java API

是否可以使用Perfmon( jmeter-plugins-perfmon )通過JMeter Java API收集服務器性能?

我知道如何使用這種方法收集基本摘要結果,但是找不到與使用Java API進行服務器性能監視有關的任何內容

對此任何幫助表示贊賞。

謝謝

您可以按照以下步驟從Java代碼初始化PerfMon Metrics Collector偵聽

PerfMonCollector perfMonCollector = new PerfMonCollector();
perfMonCollector.setName("PerfMon Metrics Collector");
perfMonCollector.setProperty("filename","perfmon.jtl");
CollectionProperty metricConnections = new CollectionProperty();
metricConnections.setName("metricConnections");
CollectionProperty cpu = new CollectionProperty();
cpu.setName("cpu");
cpu.addProperty(new StringProperty("host","localhost"));
cpu.addProperty(new StringProperty("port","4444"));
cpu.addProperty(new StringProperty("metric","CPU"));
cpu.addProperty(new StringProperty("metricParam",""));
metricConnections.addProperty(cpu);
perfMonCollector.setProperty(metricConnections);
perfMonCollector.setProperty(TestElement.TEST_CLASS, PerfMonCollector.class.getName());
perfMonCollector.setProperty(TestElement.GUI_CLASS, PerfMonGui.class.getName());

完整的代碼,以防萬一:

import kg.apc.jmeter.perfmon.PerfMonCollector;
import kg.apc.jmeter.vizualizers.PerfMonGui;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.FileOutputStream;

public class JMeterWithPerfMon {

    public static void main(String[] args) throws Exception {

        String jmeterHome = "/path/to/your/jmeter/installation";

        StandardJMeterEngine jmeter = new StandardJMeterEngine();

        JMeterUtils.setJMeterHome(jmeterHome);
        JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
        JMeterUtils.initLocale();

        HashTree testPlanTree = new HashTree();

        HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
        examplecomSampler.setDomain("example.com");
        examplecomSampler.setPort(80);
        examplecomSampler.setPath("/");
        examplecomSampler.setMethod("GET");
        examplecomSampler.setName("Open example.com");
        examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
        examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

        PerfMonCollector perfMonCollector = new PerfMonCollector();
        perfMonCollector.setName("PerfMon Metrics Collector");
        perfMonCollector.setProperty("filename", "perfmon.jtl");
        CollectionProperty metricConnections = new CollectionProperty();
        metricConnections.setName("metricConnections");
        CollectionProperty cpu = new CollectionProperty();
        cpu.setName("cpu");
        cpu.addProperty(new StringProperty("host", "localhost"));
        cpu.addProperty(new StringProperty("port", "4444"));
        cpu.addProperty(new StringProperty("metric", "CPU"));
        cpu.addProperty(new StringProperty("metricParam", ""));
        metricConnections.addProperty(cpu);
        perfMonCollector.setProperty(metricConnections);
        perfMonCollector.setProperty(TestElement.TEST_CLASS, PerfMonCollector.class.getName());
        perfMonCollector.setProperty(TestElement.GUI_CLASS, PerfMonGui.class.getName());

        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.setFirst(true);
        loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
        loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
        loopController.initialize();

        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("Example Thread Group");
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);
        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

        TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
        testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

        testPlanTree.add(testPlan);
        HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(perfMonCollector);
        threadGroupHashTree.add(examplecomSampler);

        SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/test.jmx"));

        Summariser summer = null;
        String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
        if (summariserName.length() > 0) {
            summer = new Summariser(summariserName);
        }

        String logFile = jmeterHome + "/bin/result.jtl";
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);
        testPlanTree.add(testPlanTree.getArray()[0], logger);

        jmeter.configure(testPlanTree);
        jmeter.run();

        System.exit(0);
    }
}

更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM