Allow custom HTTP Sampler
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.3k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 5
Description
**Ryan** ([Bug 58279](https://bz.apache.org/bugzilla//show_bug.cgi?id=58279&redirect=false)):
It would be convenient to allow a custom HTTP sampler to be specified in the properties configuration.
Leaving the sampler empty already uses the default specified in the properties file. The attached patch uses the class specified in jmeter.properties as the sampler such as:
jmeter.httpsampler=com.example.MySampler
The custom sampler just needs to be in the classpath and it will be instantiated.
Existing functionality which utilizes the jmeter.httpsampler property should continue to work as-is.
Created attachment [CustomSampler.patch](https://apache.github.io/jmeter-bugzilla-attachments/79/58279/33033/CustomSampler.patch): Patch allowing user to specify http sampler
CustomSampler.patch
````diff
Index: test/src/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactoryTest.java
===================================================================
--- test/src/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactoryTest.java (revision 0)
+++ test/src/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactoryTest.java (revision 0)
@@ -0,0 +1,15 @@
+package org.apache.jmeter.protocol.http.sampler;
+
+import junit.framework.TestCase;
+
+public class HTTPSamplerFactoryTest extends TestCase {
+
+ public void testGetImplementation() throws Exception {
+
+ HTTPAbstractImpl implementation = HTTPSamplerFactory.getImplementation(
+ StubHTTPSamplerBase.class.getName(),
+ HTTPSamplerFactory.newInstance());
+
+ assertEquals(StubHTTPSamplerBase.class.getName(), implementation.getClass().getName());
+ }
+}
Index: src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java
===================================================================
--- src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java (revision 1696806)
+++ src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java (working copy)
@@ -65,7 +65,7 @@
*
* @param alias HTTP_SAMPLER or HTTP_SAMPLER_APACHE or IMPL_HTTP_CLIENT3_1 or IMPL_HTTP_CLIENT4
* @return the appropriate sampler
- * @throws UnsupportedOperationException if alias is not recognised
+ * @throws UnsupportedOperationException if alias is not recognized
*/
public static HTTPSamplerBase newInstance(String alias) {
if (alias ==null || alias.length() == 0) {
@@ -101,8 +101,14 @@
} else if (IMPL_HTTP_CLIENT4.equals(impl)) {
return new HTTPHC4Impl(base);
} else {
- throw new IllegalArgumentException("Unknown implementation type: '"+impl+"'");
+ try {
+ Class implClass = Class.forName(impl);
+ return (HTTPAbstractImpl)implClass.getConstructor(HTTPSamplerBase.class).newInstance(base);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
}
+ throw new IllegalArgumentException("Unknown implementation type: '"+impl+"'");
}
}
Index: test/src/org/apache/jmeter/protocol/http/sampler/StubHTTPSamplerBase.java
===================================================================
--- test/src/org/apache/jmeter/protocol/http/sampler/StubHTTPSamplerBase.java (revision 0)
+++ test/src/org/apache/jmeter/protocol/http/sampler/StubHTTPSamplerBase.java (revision 0)
@@ -0,0 +1,22 @@
+package org.apache.jmeter.protocol.http.sampler;
+
+import java.net.URL;
+
+public class StubHTTPSamplerBase extends HTTPAbstractImpl {
+
+ public StubHTTPSamplerBase(HTTPSamplerBase testElement) {
+ super(testElement);
+ }
+
+ @Override
+ public boolean interrupt() {
+ return false;
+ }
+
+ @Override
+ protected HTTPSampleResult sample(URL url, String method,
+ boolean areFollowingRedirect, int frameDepth) {
+ return null;
+ }
+
+}
\ No newline at end of file
````
OS: Linux
Contributor guide
Research direction
Start with src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerFactory.java and the attached HTTPSamplerFactoryTest.java and StubHTTPSamplerBase.java. Check how the jmeter.httpsampler property is resolved and how existing sampler aliases behave. Done means a sampler class supplied by that property can be instantiated from the classpath without breaking existing values, with test coverage for the custom implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100