0
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,

1 Answers1

0

As per SSL Encryption chapter of JMeter Documentation:

The JMeter HTTP samplers are configured to accept all certificates, whether trusted or not, regardless of validity periods, etc. This is to allow the maximum flexibility in testing servers.

It means that if you use HTTP Request sampler it will be successfully executed even if there are problems with server-side certificates.

If you wrote your own java code and trying to use it in JSR223 Test Element or trying to create a JMeter plugin you can create a special SSLSocketFactory which will trust all certificates like it's described in Trusting all certificates using HttpClient over HTTPS thread

In any case we cannot comprehensively assist without seeing your full code.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • I added my full code – Subramanian I Mar 02 '23 at 11:21
  • Probably there is something wrong with your project dependencies. You can take i.e. [jmeter-from-code](https://bitbucket.org/blazemeter/jmeter-from-code/src/master/) example project as a basis. See [JMeter Command Line Overview: 5 Ways To Launch a Test](https://www.blazemeter.com/blog/jmeter-command-line) article for general information on various ways of launching a JMeter test in non-GUI mode. – Dmitri T Mar 02 '23 at 11:24