0
请看下面我写的测试代码。 使用纯java我设置了一个Authenticator并进行URI调用以获取一些xml数据并将其转换为对象。客户端的Netty HTTP验证
我写了下面的代码来测试hotpotato(netty)与纯java(无流水线)的性能。
问题是,我无法弄清楚如何使用hotpotato或netty验证我的请求,两者的代码都可以接受,我只想测试性能差异(即查看将在5秒内执行多少次请求)。
public static void main(String[] args) throws Exception {
Authenticator.setDefault(new MyAuthenticator("DummyUser", "DummyPassword"));
int timeToTestFor = 5000; //5 seconds;
int count = 0;
System.out.println("Start time");
long starttime = System.currentTimeMillis();
do {
URL url = new URL(
"http://example.com/rest/GetData.ashx?what=pizza&where=new%20york&visitorId=12345&sessionId=123456");
SearchResultsDocument doc = SearchResultsDocument.Factory.parse(url);
count++;
} while (System.currentTimeMillis() - starttime < timeToTestFor);
System.out.println("DONE Total count=" + count);
System.out.println("Netty/Hotpotatoe Start time");
count = 0;
starttime = System.currentTimeMillis();
do {
// Create & initialise the client
HttpClient client = new DefaultHttpClient();
client.init();
// Setup the request
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0,
HttpMethod.GET, "/rest/GetData.ashx?what=pizza&where=new%20york&visitorId=12345&sessionId=123456");
// Execute the request, turning the result into a String
HttpRequestFuture future = client.execute("example.com", 80, request,
new BodyAsStringProcessor());
future.awaitUninterruptibly();
// Print some details about the request
System.out.println("A >> " + future);
// If response was >= 200 and <= 299, print the body
if (future.isSuccessfulResponse()) {
System.out.println("B >> "+future.getProcessedResult());
}
// Cleanup
client.terminate();
count++;
} while (System.currentTimeMillis() - starttime < timeToTestFor);
System.out.println("DONE Total count=" + count);
}
谢谢,与管道的例子是非常赞赏,但有没有办法做到这一点,而不使用流水线? – Ali
哦,等等,你使用'awaitUninterruptibly()'我猜这意味着这个例子没有使用管道? – Ali
管道被用于原因。如果你的意思是HttpClientCodec在其中的管道。这个例子非常糟糕,甚至没有停止执行。这仅仅是事实的例证,基本认证只是HttpRequest中的一个头文件。 (http://en.wikipedia.org/wiki/Basic_access_authentication) –