Search in sources :

Example 16 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestClientCompressionFilter method provideRequestCompressionData.

@DataProvider(name = "requestCompressionData")
private Object[][] provideRequestCompressionData() {
    CompressionConfig smallThresholdConfig = new CompressionConfig(1);
    CompressionConfig largeThresholdConfig = new CompressionConfig(10000);
    return new Object[][] { { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_OFF, false }, { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_ON, true }, { new CompressionConfig(Integer.MAX_VALUE), null, false }, { new CompressionConfig(0), CompressionOption.FORCE_OFF, false }, { new CompressionConfig(0), CompressionOption.FORCE_ON, true }, { new CompressionConfig(0), null, true }, { smallThresholdConfig, CompressionOption.FORCE_OFF, false }, { smallThresholdConfig, CompressionOption.FORCE_ON, true }, { smallThresholdConfig, null, true }, { largeThresholdConfig, CompressionOption.FORCE_OFF, false }, { largeThresholdConfig, CompressionOption.FORCE_ON, true }, { largeThresholdConfig, null, false } };
}
Also used : CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 17 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestClientCompressionFilter method provideResponseCompressionData.

@DataProvider(name = "responseCompressionData")
private Object[][] provideResponseCompressionData() {
    CompressionConfig smallThresholdConfig = new CompressionConfig(1);
    CompressionConfig largeThresholdConfig = new CompressionConfig(10000);
    return new Object[][] { { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_OFF, null, null, "get" }, { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), "get" }, { new CompressionConfig(Integer.MAX_VALUE), null, ACCEPT_ENCODING_HEADER, Integer.toString(Integer.MAX_VALUE), "get" }, { new CompressionConfig(0), CompressionOption.FORCE_OFF, null, null, "get" }, { new CompressionConfig(0), CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), "get" }, { new CompressionConfig(0), null, ACCEPT_ENCODING_HEADER, Integer.toString(0), "get" }, { smallThresholdConfig, CompressionOption.FORCE_OFF, null, null, "get" }, { smallThresholdConfig, CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), "get" }, { smallThresholdConfig, null, ACCEPT_ENCODING_HEADER, Integer.toString(1), "get" }, { largeThresholdConfig, CompressionOption.FORCE_OFF, null, null, "get" }, { largeThresholdConfig, CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), "get" }, { largeThresholdConfig, null, ACCEPT_ENCODING_HEADER, Integer.toString(10000), "get" }, // All the same tests as above, but with a null operation
    { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_OFF, null, null, null }, { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), null }, { new CompressionConfig(Integer.MAX_VALUE), null, ACCEPT_ENCODING_HEADER, Integer.toString(Integer.MAX_VALUE), null }, { new CompressionConfig(0), CompressionOption.FORCE_OFF, null, null, null }, { new CompressionConfig(0), CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), null }, { new CompressionConfig(0), null, ACCEPT_ENCODING_HEADER, Integer.toString(0), null }, { smallThresholdConfig, CompressionOption.FORCE_OFF, null, null, null }, { smallThresholdConfig, CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), null }, { smallThresholdConfig, null, ACCEPT_ENCODING_HEADER, Integer.toString(1), null }, { largeThresholdConfig, CompressionOption.FORCE_OFF, null, null, null }, { largeThresholdConfig, CompressionOption.FORCE_ON, ACCEPT_ENCODING_HEADER, Integer.toString(0), null }, { largeThresholdConfig, null, ACCEPT_ENCODING_HEADER, Integer.toString(10000), null } };
}
Also used : CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 18 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestRequestCompression method noCompressionData.

@DataProvider
public Object[][] noCompressionData() {
    StreamEncodingType[] encodings = new StreamEncodingType[] { StreamEncodingType.GZIP, StreamEncodingType.DEFLATE, StreamEncodingType.SNAPPY_FRAMED, StreamEncodingType.BZIP2, StreamEncodingType.IDENTITY };
    String[] protocols = new String[] { HttpProtocolVersion.HTTP_1_1.name(), HttpProtocolVersion.HTTP_2.name() };
    Object[][] args = new Object[encodings.length * protocols.length][1];
    int cur = 0;
    for (StreamEncodingType requestEncoding : encodings) {
        for (String protocol : protocols) {
            StreamFilter clientCompressionFilter = new ClientStreamCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), null, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }), _executor);
            TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createStreamChain(clientCompressionFilter)).build();
            HashMap<String, String> properties = new HashMap<>();
            properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocol);
            Client client = new TransportClientAdapter(factory.getClient(Collections.<String, String>emptyMap()), true);
            args[cur][0] = client;
            //args[cur][1] = URI.create("/" + requestEncoding.getHttpName());
            cur++;
            _clientFactories.add(factory);
            _clients.add(client);
        }
    }
    return args;
}
Also used : HashMap(java.util.HashMap) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteString(com.linkedin.data.ByteString) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) ClientStreamCompressionFilter(com.linkedin.r2.filter.compression.ClientStreamCompressionFilter) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 19 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestClientStreamCompressionFilter method testAcceptEncodingHeader.

@Test(dataProvider = "requestData")
public void testAcceptEncodingHeader(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    Executor executor = Executors.newCachedThreadPool();
    ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
    int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.ACCEPT_ENCODING, true));
}
Also used : Executor(java.util.concurrent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(com.linkedin.r2.message.RequestContext) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 20 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestResponseCompression method testResponseCompression.

@Test(dataProvider = "requestData")
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
    Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<String, CompressionConfig>();
    if (responseCompressionConfig != null) {
        responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
    }
    HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(0, /* use default settings */
    new NamedThreadFactory("R2 Nio Event Loop")), true, executor, true, executor, false, AbstractJmxManager.NULL_JMX_MANAGER, Integer.MAX_VALUE, Collections.<String, CompressionConfig>emptyMap(), responseCompressionConfigs, true);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
    if (useResponseCompression != null) {
        properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
    }
    TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
    RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
    Long[] ids = new Long[idCount];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = (long) i;
    }
    BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
    if (expectedCompressionThreshold != null) {
        builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
    }
    Request<BatchResponse<Greeting>> request = builder.build();
    Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
    if (responseShouldBeCompressed) {
        Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
    } else {
        Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) BatchResponse(com.linkedin.restli.common.BatchResponse) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Aggregations

CompressionConfig (com.linkedin.r2.filter.CompressionConfig)20 DataProvider (org.testng.annotations.DataProvider)9 Test (org.testng.annotations.Test)8 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)6 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)6 RequestContext (com.linkedin.r2.message.RequestContext)5 URI (java.net.URI)5 HashMap (java.util.HashMap)5 ByteString (com.linkedin.data.ByteString)4 ClientStreamCompressionFilter (com.linkedin.r2.filter.compression.ClientStreamCompressionFilter)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4 Client (com.linkedin.r2.transport.common.Client)4 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)4 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)4 FilterChain (com.linkedin.r2.filter.FilterChain)3 StreamFilter (com.linkedin.r2.filter.message.stream.StreamFilter)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FutureCallback (com.linkedin.common.callback.FutureCallback)2