当前位置: 首页 > 知识库问答 >
问题:

JSR223采样器优化

夔学智
2023-03-14

我有下面的JSR223采样器,它读取图像,稍微修改它,并发送一个POST multipart/form-data请求。与HTTP采样器相比,我发现它广泛使用了CPU,但我不能使用HTTP采样器,因为它不支持在不保存到文件系统的情况下更改映像。

如果任何人有任何输入来优化JSR223采样器中的脚本,这样它就不会占用大量的CPU,我将很感激。

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(40000)
        .setSocketTimeout(50000)
        .build();

BufferedImage image = ImageIO.read(new File("/home/4567/loadtest/Bank.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, "Bank.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")
        .setHeader("ResponseMinLatencyMs","45")
        .setHeader("ResponseMaxLatencyMs","55")
        .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);
       response.close()
    }
    httpClient.close()
}
image.flush()
bytes.flush()
bytes.close()

}

sendRequest("https://test-server.com/upload", "POST", "");

共有1个答案

白云
2023-03-14
  1. StackOverflow不是一个代码编写服务,您应该要求别人对您甚至没有编写的代码进行优化,例如在Toptal这样的网站上
  2. 我相信普通的HTTP请求采样器不会在发送请求之前更改映像,这就是为什么它没有那么占用CPU的原因。
  3. 研究Beanshell与JSR223和Java JMeter脚本:您一直在等待的性能下降!就性能和资源使用而言,Java it似乎仍然是最佳选择,因此您可能希望用Java重新实现代码,并使用JUnit或Java请求采样器
  4. 如果您在JMeter机器上运行超过85%的CPU,那么由于JMeter无法足够快地发送请求,您很可能无法得到准确的结果,请考虑进行分布式测试
 类似资料: