我使用nodejs構建一個http服務器。每個請求收入,http服務器連接到memcache,檢查並返回數據或做其他事情。當我使用Apache Benchmark測試性能時,如:-n 1000.輸出顯示它剛剛完成了約900個請求,然後http服務器不能再服務。 我在做什麼錯?你能解釋發生了什麼?Nodejs HTTP服務器連接memcache
這裏我的代碼:
http.createServer(function(req, res) {
var client = new mc.Client(ip, mc.Adapter.binary);
client.connect(function() {
client.get(key, function(err, response) {
if(err) { //not exist
..
res.end(result)
..
}
else { //exist in memcache
var result = response[key];
res.end(result);
}
});
}); }); 這裏是benchmarch後的結果是:
This is ApacheBench, Version 2.4.1 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
apr_poll: The timeout specified has expired (70007)
Total of 896 requests completed
更新:我想通了,我也不要斷開memcached的每個請求。問題解決了!!!
我認爲你的if-else邏輯是不正確的。錯誤並不意味着密鑰不存在。當key不存在時,我相信nil會返回。錯誤是由於處理給定的命令失敗。 – user568109
是的,我知道,但是當我使用MC模塊和密鑰不存在它打印錯誤和響應也是空的。如果無法連接memcache,我在mc模塊中找不到任何api,例如timeout。所以我必須先啓動memcache。 – Windranger