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 } };
}
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 } };
}
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;
}
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));
}
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));
}
}
Aggregations