0

I have the following code in JSR223 Sampler and I get following errors when I run this jmeter command. Can anyone please advise if something is wrong with my code? I'm basically reading a image and changing it little bit and sending a multipart/form-data POST request.

jmeter -n -Jthreads=20 -Jrampup=30 -Jduration=60 -Jiterations=-1 -t script.jmx

javax.script.ScriptException: java.net.SocketTimeoutException: Read timed out Uncaught Exception java.lang.OutOfMemoryError: Java heap space in thread Thread[Thread Group 1-10,5,main]

import org.apache.http.HttpHeaders
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpUriRequest
import org.apache.http.client.methods.RequestBuilder
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.conn.ssl.TrustStrategy
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.ssl.SSLContextBuilder
import org.apache.http.util.EntityUtils
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.HttpMultipartMode;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.io.ByteArrayOutputStream;
import org.apache.http.entity.ContentType;

import java.security.cert.CertificateException
import java.security.cert.X509Certificate

List<String> sendRequest(String url, String method, String body) {


RequestConfig requestConfig = RequestConfig.custom()
        .setConnectTimeout(2000)
        .setSocketTimeout(3000)
        .build();

BufferedImage image = ImageIO.read(new File("C:/Users/bd3249/Pictures/5.JPG"));
Graphics graphics = image.getGraphics();
graphics.setFont(graphics.getFont().deriveFont(16f));
graphics.drawString("User " + ctx.getThreadNum() + '-' + Math.random()  +"; iteration: " + ctx.getVariables().getIteration(), 50, 50);
graphics.dispose();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", bytes);

final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.STRICT);
multipartEntity.addBinaryBody("File", bytes.toByteArray(),ContentType.IMAGE_JPEG, "5.JPG");


HttpUriRequest request = RequestBuilder.create(method)
        .setConfig(requestConfig)
        .setUri(url)
        .setHeader("x-ibm-client-id","248a20f3-c39b-45d0-b26a-9019c26e9be8")
        .setHeader("X-Client-Id","2861D410-773B-4DD9-AE74-882116B08856")
        .setEntity(multipartEntity.build())
        .build();

String req = "REQUEST:" + "User " + ctx.getThreadNum() + "; iteration: " + ctx.getVariables().getIteration() + " " + request.getRequestLine() + "\n";

def builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
    @Override
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
    }
});
def trustAllFactory = new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());

HttpClients.custom().setSSLSocketFactory(trustAllFactory).build().withCloseable { httpClient ->

    httpClient.execute(request).withCloseable { response ->

        String res = "RESPONSE:" + "User " + ctx.getThreadNum() + "; iteration: " + ctx.getVariables().getIteration() + " " + response.getStatusLine()  + (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

        log.info(req + "\n" + res);

        return Arrays.asList(req, res);
    }
  }
}

List test1 = sendRequest("https://test.com/upload", "POST", "");
user1829449
  • 59
  • 11

1 Answers1

0

The error you're getting clearly indicates that you don't have sufficient Java Heap space, by default JMeter 5.3 comes with 1 GB of heap and depending on your image size and response size it might be not enough for your test.

Use i.e. Active Threads Over Time listener to see how many virtual users were online when the error occurs and increase the heap size proportionally.

More information: 9 Easy Solutions for a JMeter Load Test “Out of Memory” Failure

Dmitri T
  • 159,985
  • 5
  • 83
  • 133