forked from adamfisk/LittleProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ThrottlingTest. Removed AdaptiveRecvByteBufAllocator.
- Loading branch information
Showing
4 changed files
with
299 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
src/test/java/org/littleshoot/proxy/ThrottlingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
package org.littleshoot.proxy; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.conn.params.ConnRoutePNames; | ||
import org.apache.http.entity.ByteArrayEntity; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.apache.http.params.CoreConnectionPNames; | ||
import org.apache.http.util.EntityUtils; | ||
import org.eclipse.jetty.server.Server; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.littleshoot.proxy.impl.DefaultHttpProxyServer; | ||
|
||
public class ThrottlingTest { | ||
|
||
private static final int WRITE_WEB_SERVER_PORT = 8926; | ||
private static final int READ_WEB_SERVER_PORT = 8927; | ||
private static final long THROTTLED_READ_BYTES_PER_SECOND = 25000L; | ||
private static final long THROTTLED_WRITE_BYTES_PER_SECOND = 25000L; | ||
|
||
// throttling is not guaranteed to be exact, so allow some variation in the amount of time the call takes. since we want | ||
// these tests to take just a few seconds, allow significant variation. even with this large variation, if throttling | ||
// is broken it should take much less time than expected. | ||
private static final double ALLOWABLE_VARIATION = 0.30; | ||
|
||
private Server writeWebServer; | ||
private Server readWebServer; | ||
|
||
private byte[] largeData; | ||
|
||
private int msToWriteThrottled; | ||
private int msToReadThrottled; | ||
|
||
// time to allow for an unthrottled local request | ||
private static final int UNTRHOTTLED_REQUEST_TIME_MS = 1000; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
// Set up some large data | ||
largeData = new byte[100000]; | ||
for (int i = 0; i < largeData.length; i++) { | ||
largeData[i] = 1 % 256; | ||
} | ||
|
||
msToWriteThrottled = largeData.length * 1000 / (int)THROTTLED_WRITE_BYTES_PER_SECOND; | ||
msToReadThrottled = largeData.length * 1000 / (int)THROTTLED_READ_BYTES_PER_SECOND; | ||
|
||
writeWebServer = TestUtils.startWebServer(WRITE_WEB_SERVER_PORT); | ||
readWebServer = TestUtils.startWebServerWithResponse(READ_WEB_SERVER_PORT, largeData); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
writeWebServer.stop(); | ||
readWebServer.stop(); | ||
} | ||
|
||
@Test | ||
public void testThrottledWrite() throws Exception { | ||
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap() | ||
.withPort(0) | ||
.withThrottling(0, THROTTLED_WRITE_BYTES_PER_SECOND) | ||
.start(); | ||
|
||
int proxyPort = proxyServer.getListenAddress().getPort(); | ||
|
||
final HttpPost request = new HttpPost("/"); | ||
request.getParams().setParameter( | ||
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); | ||
final ByteArrayEntity entity = new ByteArrayEntity(largeData); | ||
entity.setChunked(true); | ||
request.setEntity(entity); | ||
|
||
DefaultHttpClient httpClient = new DefaultHttpClient(); | ||
final HttpHost proxy = new HttpHost("127.0.0.1", proxyPort, "http"); | ||
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, | ||
proxy); | ||
|
||
long start = System.currentTimeMillis(); | ||
final org.apache.http.HttpResponse response = httpClient.execute( | ||
new HttpHost("127.0.0.1", | ||
WRITE_WEB_SERVER_PORT), request); | ||
long finish = System.currentTimeMillis(); | ||
|
||
Assert.assertEquals("Received " + largeData.length + " bytes\n", | ||
EntityUtils.toString(response.getEntity())); | ||
|
||
Assert.assertTrue("Expected throttled write to complete in approximately " + msToWriteThrottled + "ms" + " but took " + (finish - start) + "ms", | ||
finish - start > msToWriteThrottled * (1 - ALLOWABLE_VARIATION) | ||
&& (finish - start < msToWriteThrottled * (1 + ALLOWABLE_VARIATION))); | ||
|
||
proxyServer.stop(); | ||
} | ||
|
||
@Test | ||
public void testUnthrottledWrite() throws Exception { | ||
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap() | ||
.withPort(0) | ||
.start(); | ||
|
||
int proxyPort = proxyServer.getListenAddress().getPort(); | ||
|
||
final HttpPost request = new HttpPost("/"); | ||
request.getParams().setParameter( | ||
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); | ||
final ByteArrayEntity entity = new ByteArrayEntity(largeData); | ||
entity.setChunked(true); | ||
request.setEntity(entity); | ||
|
||
DefaultHttpClient httpClient = new DefaultHttpClient(); | ||
final HttpHost proxy = new HttpHost("127.0.0.1", proxyPort, "http"); | ||
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, | ||
proxy); | ||
|
||
long start = System.currentTimeMillis(); | ||
final org.apache.http.HttpResponse response = httpClient.execute( | ||
new HttpHost("127.0.0.1", | ||
WRITE_WEB_SERVER_PORT), request); | ||
long finish = System.currentTimeMillis(); | ||
|
||
Assert.assertEquals("Received " + largeData.length + " bytes\n", | ||
EntityUtils.toString(response.getEntity())); | ||
|
||
Assert.assertTrue("Unthrottled write took " + (finish - start) + "ms, but expected to complete in " + UNTRHOTTLED_REQUEST_TIME_MS + "ms", finish - start < UNTRHOTTLED_REQUEST_TIME_MS); | ||
|
||
proxyServer.stop(); | ||
} | ||
|
||
@Test | ||
public void testThrottledRead() throws Exception { | ||
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap() | ||
.withPort(0) | ||
.withThrottling(THROTTLED_READ_BYTES_PER_SECOND, 0) | ||
.start(); | ||
|
||
int proxyPort = proxyServer.getListenAddress().getPort(); | ||
|
||
final HttpGet request = new HttpGet("/"); | ||
request.getParams().setParameter( | ||
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); | ||
|
||
DefaultHttpClient httpClient = new DefaultHttpClient(); | ||
final HttpHost proxy = new HttpHost("127.0.0.1", proxyPort, "http"); | ||
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, | ||
proxy); | ||
|
||
long start = System.currentTimeMillis(); | ||
final org.apache.http.HttpResponse response = httpClient.execute( | ||
new HttpHost("127.0.0.1", | ||
READ_WEB_SERVER_PORT), request); | ||
byte[] readContent = new byte[100000]; | ||
|
||
int bytesRead = 0; | ||
while (bytesRead < largeData.length) { | ||
int read = response.getEntity().getContent().read(readContent, bytesRead, largeData.length - bytesRead); | ||
bytesRead += read; | ||
} | ||
|
||
long finish = System.currentTimeMillis(); | ||
|
||
Assert.assertTrue("Expected throttled read to complete in approximately " + msToReadThrottled + "ms" + " but took " + (finish - start) + "ms", | ||
finish - start > msToReadThrottled * (1 - ALLOWABLE_VARIATION) | ||
&& (finish - start < msToReadThrottled * (1 + ALLOWABLE_VARIATION))); | ||
|
||
proxyServer.stop(); | ||
} | ||
|
||
@Test | ||
public void testUnthrottledRead() throws Exception { | ||
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap() | ||
.withPort(0) | ||
.start(); | ||
|
||
int proxyPort = proxyServer.getListenAddress().getPort(); | ||
|
||
final HttpGet request = new HttpGet("/"); | ||
request.getParams().setParameter( | ||
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); | ||
|
||
DefaultHttpClient httpClient = new DefaultHttpClient(); | ||
final HttpHost proxy = new HttpHost("127.0.0.1", proxyPort, "http"); | ||
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, | ||
proxy); | ||
|
||
long start = System.currentTimeMillis(); | ||
final org.apache.http.HttpResponse response = httpClient.execute( | ||
new HttpHost("127.0.0.1", | ||
READ_WEB_SERVER_PORT), request); | ||
|
||
byte[] readContent = new byte[100000]; | ||
int bytesRead = 0; | ||
while (bytesRead < largeData.length) { | ||
int read = response.getEntity().getContent().read(readContent, bytesRead, largeData.length - bytesRead); | ||
bytesRead += read; | ||
} | ||
|
||
long finish = System.currentTimeMillis(); | ||
|
||
Assert.assertTrue("Unthrottled read took " + (finish - start) + "ms, but expected to complete in " + UNTRHOTTLED_REQUEST_TIME_MS + "ms", finish - start < UNTRHOTTLED_REQUEST_TIME_MS); | ||
|
||
proxyServer.stop(); | ||
} | ||
} |