Representational State Transfer (REST) has gained widespread acceptance across the Web as a simpler alternative to SOAP- and Web Services Description Language (WSDL)-based Web services.
Key evidence of this shift in interface design is the adoption of REST by mainstream Web 2.0 service providers—including Yahoo, Google, and Facebook—who have deprecated or passed on SOAP and WSDL-based interfaces in favor of an easier-to-use, resource-oriented model to expose their services. Sometimes you have to test your RESTFul web service. You may have couple of options:
- Hit your WebService using single URL
- Use
JMeter
and hit WebService in loop. This test also performs some SimpleLoad Test
.
Here in this example I’ll explain you on how Create Simple Hello World WebService
and hit the same WebService using JMeter.
If you have any of below questions the you are at right place:
- Testing SOAP/REST Web Services Using JMeter
- Performance Testing of a RESTful API using JMeter
- Rest API Automation in JMeter – Performance Testing
- How to test REST API using JMeter
- Using JMeter to test a RESTful web service (Jersey)
Action Summary:
Pre-Requisite:
Implement this RESTFul service completely.- Create simple Java class: CrunchifyJMeterTest.java (Service which we will hit using JMeter).
- Redeploy the Same Application on Apache Tomcat Web Server.
- Run JMeter and open below provided
.jmx
file - Run and Analyze your test
Another must read:
Step-1
Pre-Requisite:
Implement this RESTFul service completely. Deploy and Run Application.
Let me know if you see any issue with it.
Step-2
Create CrunchifyJMeterTest.java
File
package com.crunchify.restjersey; import java.io.FileNotFoundException; import java.io.IOException; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; /** * @author Crunchify * */ @Path("/index") public class CrunchifyJMeterTest { @GET @Produces("text/html") public String checkECV() throws InterruptedException, FileNotFoundException, IOException { String result = "<br><div align='center'><h2>Hey This is Crunchify's JMeter Test...</h2></div>"; System.out.println(result); Thread.sleep(1000); return result; } }
You should see below Eclipse Directory Structure:
Step-3
Redeploy the same CrunchifyRESTJerseyExample Project on Tomcat. Test it out your REST Service.
URL:
http://localhost:8080/CrunchifyRESTJerseyExample/crunchify/index/
Step-4
Copy below Code to file and save it as Crunchify-JMeter-Test.jmx
.
<?xml version="1.0" encoding="UTF-8"?> <jmeterTestPlan version="1.2" properties="2.4" jmeter="2.9 r1437961"> <hashTree> <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="App Shah Desktop Test" enabled="true"> <stringProp name="TestPlan.comments"></stringProp> <boolProp name="TestPlan.functional_mode">false</boolProp> <boolProp name="TestPlan.serialize_threadgroups">false</boolProp> <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="TestPlan.user_define_classpath"></stringProp> </TestPlan> <hashTree> <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Crunchify's REST Service JMeter Test" enabled="true"> <stringProp name="ThreadGroup.on_sample_error">continue</stringProp> <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true"> <boolProp name="LoopController.continue_forever">false</boolProp> <intProp name="LoopController.loops">-1</intProp> </elementProp> <stringProp name="ThreadGroup.num_threads">5</stringProp> <stringProp name="ThreadGroup.ramp_time">2</stringProp> <longProp name="ThreadGroup.start_time">1367432020000</longProp> <longProp name="ThreadGroup.end_time">1367432020000</longProp> <boolProp name="ThreadGroup.scheduler">false</boolProp> <stringProp name="ThreadGroup.duration"></stringProp> <stringProp name="ThreadGroup.delay"></stringProp> </ThreadGroup> <hashTree> <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true"> <boolProp name="HTTPSampler.postBodyRaw">true</boolProp> <elementProp name="HTTPsampler.Arguments" elementType="Arguments"> <collectionProp name="Arguments.arguments"> <elementProp name="" elementType="HTTPArgument"> <boolProp name="HTTPArgument.always_encode">false</boolProp> <stringProp name="Argument.value"></stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> </collectionProp> </elementProp> <stringProp name="HTTPSampler.domain">localhost</stringProp> <stringProp name="HTTPSampler.port">8080</stringProp> <stringProp name="HTTPSampler.connect_timeout">10000</stringProp> <stringProp name="HTTPSampler.response_timeout">10000</stringProp> <stringProp name="HTTPSampler.protocol"></stringProp> <stringProp name="HTTPSampler.contentEncoding"></stringProp> <stringProp name="HTTPSampler.path">/CrunchifyRESTJerseyExample/crunchify/index</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> <boolProp name="HTTPSampler.follow_redirects">true</boolProp> <boolProp name="HTTPSampler.auto_redirects">false</boolProp> <boolProp name="HTTPSampler.use_keepalive">true</boolProp> <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> <boolProp name="HTTPSampler.monitor">false</boolProp> <stringProp name="HTTPSampler.embedded_url_re"></stringProp> </HTTPSamplerProxy> <hashTree/> <ResultCollector guiclass="StatGraphVisualizer" testclass="ResultCollector" testname="Aggregate Graph" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="SampleSaveConfiguration"> <time>true</time> <latency>true</latency> <timestamp>true</timestamp> <success>true</success> <label>true</label> <code>true</code> <message>true</message> <threadName>true</threadName> <dataType>true</dataType> <encoding>false</encoding> <assertions>true</assertions> <subresults>true</subresults> <responseData>false</responseData> <samplerData>false</samplerData> <xml>true</xml> <fieldNames>false</fieldNames> <responseHeaders>false</responseHeaders> <requestHeaders>false</requestHeaders> <responseDataOnError>false</responseDataOnError> <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage> <assertionsResultsToSave>0</assertionsResultsToSave> <bytes>true</bytes> </value> </objProp> <stringProp name="filename"></stringProp> </ResultCollector> <hashTree/> <ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="Aggregate Report" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="SampleSaveConfiguration"> <time>true</time> <latency>true</latency> <timestamp>true</timestamp> <success>true</success> <label>true</label> <code>true</code> <message>true</message> <threadName>true</threadName> <dataType>true</dataType> <encoding>false</encoding> <assertions>true</assertions> <subresults>true</subresults> <responseData>false</responseData> <samplerData>false</samplerData> <xml>false</xml> <fieldNames>false</fieldNames> <responseHeaders>false</responseHeaders> <requestHeaders>false</requestHeaders> <responseDataOnError>false</responseDataOnError> <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage> <assertionsResultsToSave>0</assertionsResultsToSave> <bytes>true</bytes> </value> </objProp> <stringProp name="filename"></stringProp> </ResultCollector> <hashTree/> </hashTree> </hashTree> </hashTree> </jmeterTestPlan>
Step-5
- Download Apache JMeter.
- Run JMeter using jmeter.bat or jmeter.sh file
- Click on
File -> Open
- Go to the folder where you saved file Crunchify-JMeter-Test.jmx and select file
Step-6
- Now expand Crunchify’s Rest Service JMeter Test as you see in below image
- Click on HTTP Request
- Make sure below parameters are correct
Step-7
Analyze your Test Result.
Rest API Automation in JMeter?
Well, after each development cycle, you could run test which you have already defined in JMeter file at specific interval.
There are number of other ways you can change your test parameters. Play with it and let me know how it goes or if you see any issue with this.