apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
8.39k stars 2.1k forks source link

Add ability to send compressed request #4580

Open asfimport opened 6 years ago

asfimport commented 6 years ago

@pmouawad (Bug 61748): We should provide a way to send compressed content.

We could detect if a header is set :

And compress the body as a consequence

OS: All

asfimport commented 6 years ago

UbikLoadPack support (migrated from Bugzilla): As per dev mailing list discussion:


Hello, In some applications, the request body needs to be gzipped.

It is currently possible to do it by developping a JSR223 PreProcessor but it requires some development knowledge.

We would like to contribute a patch to provide this but have some questions on what best options would be:

Option 1 :Should it be automatic, ie, whenever a Header Content-Encoding=gzip , we do it:
    Easy
    To disable it, remove Content-Encoding header
    Is there a case where it would be annoying ?
    We would add a property to disable registration of this Interceptor to disable globally the feature if really needed
Option 2 :Should it be activated by a checkbox in HTTP Request Advanced Tab ?
    Yet Another option in a complex UI ?
Option 3 : Should it be a PreProcessor ?:
    More flexible
    but  requires insertions in many places

Unless there is a case we would go for Option 1.

Any thoughts ?

Thanks Regards UbikLoadPack Team

Created attachment BUG_61748.patch: Patch implementing option 1

BUG_61748.patch ````diff Index: src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java =================================================================== --- src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (revision 1831018) +++ src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (working copy) @@ -42,6 +42,7 @@ import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import javax.security.auth.Subject; @@ -51,6 +52,7 @@ import org.apache.http.HttpClientConnection; import org.apache.http.HttpConnectionMetrics; import org.apache.http.HttpEntity; +import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -95,6 +97,7 @@ import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.cookie.CookieSpecProvider; +import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.FileEntity; import org.apache.http.entity.StringEntity; @@ -213,8 +216,25 @@ } }; + private static final class GzipHttpRequestInterceptor implements HttpRequestInterceptor { + @Override + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { + if(request instanceof HttpEntityEnclosingRequest) { + Header header = request.getFirstHeader("Content-Encoding"); + if(header != null && "gzip".equals(header.getValue())) { + HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request; + HttpEntity entity = enclosingRequest.getEntity(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (GZIPOutputStream gzipOS = new GZIPOutputStream(out)) { + entity.writeTo(gzipOS); + } + enclosingRequest.setEntity(new ByteArrayEntity(out.toByteArray())); + } + } + } + } private static final class PreemptiveAuthRequestInterceptor implements HttpRequestInterceptor { - //@Override + @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext localContext = HttpClientContext.adapt(context); AuthManager authManager = (AuthManager) localContext.getAttribute(CONTEXT_ATTRIBUTE_AUTH_MANAGER); @@ -375,7 +395,7 @@ private static final String DIGEST_PARAMETERS = DigestParameters.VARIABLE_NAME; - + private static final HttpRequestInterceptor GZIP_REQUEST_INTERCEPTOR = new GzipHttpRequestInterceptor(); private static final HttpRequestInterceptor PREEMPTIVE_AUTH_INTERCEPTOR = new PreemptiveAuthRequestInterceptor(); // see https://stackoverflow.com/questions/26166469/measure-bandwidth-usage-with-apache-httpcomponents-httpclient @@ -1051,6 +1071,7 @@ } builder.setDefaultCredentialsProvider(credsProvider); } + builder.addInterceptorFirst(GZIP_REQUEST_INTERCEPTOR); builder.disableContentCompression().addInterceptorLast(RESPONSE_CONTENT_ENCODING); if(BASIC_AUTH_PREEMPTIVE) { builder.addInterceptorFirst(PREEMPTIVE_AUTH_INTERCEPTOR); ````