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) –