为什么在尝试执行Jmeter java代码时会出现异常?

qlvxas9a  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(144)
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
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.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class Load Performance {
    
    static Scanner s = new Scanner(System.in);            

    public static void main(String[] arg) throws Exception {
        
        // Prompt user to enter URL
        System.out.print("Enter URL: ");
        String url = System.console().readLine();
        System.out.println("Enter the No.of USER: ");
        int user = s.nextInt();
        System.out.println("Enter the No.of LOOP: ");
        int loop = s.nextInt();
        System.out.println("Enter the ramp up: ");
        int rampup = s.nextInt();

        String jmeter = "~/Downloads/Apache_Jars/apache-jmeter-5.5";
        File jmeterHome=new File(jmeter);
        String slash = System.getProperty("file.separator");

        if (jmeterHome.exists()) {
            File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
            if (jmeterProperties.exists()) {
                //JMeter Engine
                StandardJMeterEngine jmeter1 = new StandardJMeterEngine();

                //JMeter initialization (properties, log levels, locale, etc)
                JMeterUtils.setJMeterHome(jmeterHome.getPath());
                JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
                JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
                JMeterUtils.initLocale();

                // JMeter Test Plan, basically JOrphan HashTree
                HashTree testPlanTree = new HashTree();

                //HTTP Sampler - open client
                HTTPSamplerProxy client = new HTTPSamplerProxy();
                client.setDomain(url);          
                client.setPath("/");
                client.setMethod("GET");
                client.setName("Open Client");
                client.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
                client.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

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

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

                // Test Plan
                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());

                // Construct Test Plan from previously initialized elements
                testPlanTree.add(testPlan);
                HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
                threadGroupHashTree.add(client);

                // save generated test plan to JMeter's .jmx file format
                SaveService.saveTree(testPlanTree, Files.newOutputStream(Paths.get(jmeterHome + slash + "example.jmx")));

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

                // Store execution results into a .jtl file
                String logFile = jmeterHome + slash + "example.jtl";
                ResultCollector logger = new ResultCollector(summer);
                logger.setFilename(logFile);
                testPlanTree.add(testPlanTree.getArray()[0], logger);

                // Run Test Plan
                jmeter1.configure(testPlanTree);
                jmeter1.run();

                System.out.println("Test completed. See " + jmeterHome + slash + "example.jtl file for results");
                System.out.println("JMeter .jmx script is available at " + jmeterHome + slash + "example.jmx");
                System.exit(0);
            }
        }
        System.err.println("jmeter.home property is not set or pointing to incorrect location");
        System.exit(1);
    }
}

Exception in thread "Example Thread Group 1-1" java.lang.NoSuchMethodError: org.apache.jmeter.util.HttpSSLProtocolSocketFactory.(Lorg/apache/jmeter/util/JsseSSLManager;I)V at org.apache.jmeter.protocol.http.util.HC4TrustAllSSLSocketFactory.(HC4TrustAllSSLSocketFactory.java:55) at org.apache.jmeter.protocol.http.sampler.LazySchemeSocketFactory$AdapteeHolder.checkAndInit(LazySchemeSocketFactory.java:52) at org.apache.jmeter.protocol.http.sampler.LazySchemeSocketFactory$AdapteeHolder.(LazySchemeSocketFactory.java:44) at org.apache.jmeter.protocol.http.sampler.LazySchemeSocketFactory.createSocket(LazySchemeSocketFactory.java:79) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:168) at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326) at org.apache.jmeter.protocol.http.sampler.MeasuringConnectionManager$MeasuredConnection.open(MeasuringConnectionManager.java:114) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:654) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:413) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:76) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1166) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1155) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:651) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:570) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) at java.lang.Thread.run(Thread.java:748)
I written a java code for load performance and i compile that with google url its execute successfully but i enter my url which has no ssl certificate, The code is doesn't execute and its display this exception,

q3aa0525

q3aa05251#

根据JMeter文档的SSL加密章节:

**JMeter HTTP采样器被配置为接受所有证书,无论是否可信,也无论有效期长短等。**这是为了在测试服务器时提供最大的灵活性。

这意味着,如果您使用HTTP请求采样器,即使服务器端证书有问题,它也会成功执行。
如果您编写了自己的java代码并尝试在JSR223 Test Element中使用它或尝试create a JMeter plugin,则可以创建一个特殊的SSLSocketFactory,它将信任所有证书,就像Trusting all certificates using HttpClient over HTTPS线程中所描述的那样
在任何情况下,我们不能全面协助没有看到您的完整代码.

相关问题