Make your own sampler with JMeter

background

Step on the URL that comes to GMail with Jmeter at random. I'm making a tool called. I don't understand the behavior of MailReaderSampler ... imap doesn't work as referenced. pop3 is connected. But how do you extract the text? Something like that. In particular, image.png

I'm nested like this, and I don't know how to take this text by post-processing regular expression extraction. (I don't know how to focus on the first result in the bottom layer)

After all, I used to make my own sampler, so make your own sampler. In that.

I forgot how to make my own sampler, so I wrote an article anyway.

version

apache-jmeter : 5.2.1 JRE : 1.8

Sampler code

DemoSampler.java


package jmeter.extend.sampler;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class DemoSampler extends AbstractJavaSamplerClient {

	//Define parameter keys
	private static final String MAIL_ADDRESS = "mailAddress";
	private static final String PASSWORD = "password";

	/* (non-Javadoc)
	 *Set default parameters
	 */
	@Override
	public Arguments getDefaultParameters() {
		//Setting default parameters
		Arguments defaultParameters = new Arguments();
		defaultParameters.addArgument(MAIL_ADDRESS, "[email protected]");
		defaultParameters.addArgument(PASSWORD, "input your password");
		return defaultParameters;
	}

	@Override
	public SampleResult runTest(JavaSamplerContext context) {
		SampleResult result = new SampleResult();
		try {
			//Get the parameters entered in JMeter
			String mail = context.getParameter(MAIL_ADDRESS);
			String password = context.getParameter(PASSWORD);

			//Start time measurement(1)
			result.sampleStart();

			//Arbitrary processing you want to measure
			Thread.sleep(500);

			StringBuilder bulder = new StringBuilder();
			bulder.append("this is ResponseData\n");
			bulder.append("mail : ").append(mail).append("\n");
			bulder.append("password : ").append(password).append("\n");
			bulder.append("Japanese string character string\n");
			bulder.append("URL : https://github.com/");

			//Time measurement finished(2)
			// (2) - (1)Time can be obtained as the response time of JMeter
			result.sampleEnd();

			//Successful acquisition of JMeter
			result.setSuccessful(true);
			//OK JMeter's response code(200)Set to
			result.setResponseCodeOK();
			result.setRequestHeaders("this is RequestHeaders");
			result.setResponseData(bulder.toString(), "utf-8");

			//This guy doesn't seem to appear anywhere
			result.setResponseMessage("this is ResponseMessage");

		} catch (Exception e) {
			//Time measurement finished(2)
			// (2) - (1)Time can be obtained as the response time of JMeter
			result.sampleEnd();
			//JMeter acquisition result fails
			result.setSuccessful(false);
			//OK JMeter's response code(200)Set to a number other than
			result.setResponseCode("500");
			//Fill in the contents of the response data displayed in JMeter
			result.setResponseMessage("Error!! " + e.getMessage());
		}
		return result;
	}
}

All the required libraries are under bin in jmeter.

Import to JMeter

Make the above package into a jar with the export function of Eclipse etc. Just place it under libs / ext of jmeter and restart jmeter. If your code depends on a jar other than the one that comes with jmeterlib from the beginning, Place that jar in the same folder as well.

Execution image

image.png

Use Java requests. Classes that inherit AbstractJavaSamplerClient are listed in the list of class names.

image.png

The character specified in result.setResponseData is displayed in ResponseBody.

reference

https://qiita.com/kiida/items/6af3e46e5bae14c38242 It's almost a round pakuri for reference. I'm sorry.

PS

You can get it normally from MailReaderSampler! Please let me know if you have any questions.

Recommended Posts

Make your own sampler with JMeter
Make your own pomodoro
Make your own Rails validate
Make your own Elasticsearch plugin
Make your own persistence FW (Java)
Create your own validator with Bean Validation
Add your own authentication items with Spring Security
Create your own Utility with Thymeleaf with Spring Boot
Call your own class created under lib with Rails
Load test with JMeter
Call your own method with PreAuthorize in Spring Security
Register your own Docker image with ECR using AWS CLI
(PC operation at home) Collecting events with your own batch
Make your own simple server in Java and understand HTTP
Java: Start WAS with Docker and deploy your own application
Use your own classes in the lib directory with Rails6
Automate your work with Rake
Microservices with DevOps Make Changes
Create your own Java annotations
Tune your query with EXPLAIN
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
Make your own keyboard QMK in Docker. Volume unique to Windows
How to create your own Controller corresponding to / error with Spring Boot
Redirect to your own domain with rack-rewrite on Heroku x Rails