我很难确定我的网络服务器(Cherokee)和 expressjs 网站之间的性能瓶颈。
由于 POST 流量的增加,我们开始时不时地看到性能问题。所以我设计了一个测试,看看网站的 1、2、3 和 4 个实例的断点是什么,并对糟糕的性能感到震惊(每秒 5 个运行良好,每秒 10 个运行非常差)。
根据我的性能记录,一个完整的请求从开始到结束需要在 1000-4000 毫秒之间开始,并且在短时间内开始以线性趋势增加,因此大约 5 分钟后请求需要 40,000-60,0000 毫秒。expressjs 网站记录中从开始到结束请求的性能日志记录需要 100-400 毫秒(其中大部分时间 90-390 毫秒用于等待 Java API 响应)。
我已经研究了连接池/最大套接字,但是不明白如何使用 express 配置它,并且随着流量的不断增长,我开始感受到网站每次性能下降带来的热量。
现在我有两个理论: 1.压力测试代码确实有问题;2. 压测app或者expressjs发送请求时,连接池很小。
如果以上任何一个都是真的,我不知道如何解决这些问题。
-- 压力测试应用 --
/*
* POST stress testing
*/
var rest = require( "restler");
var u = require( "../underscore")._;
var uuid = require( "node-guid" );
var fs = require( "fs" );
var quoteContent = fs.readFileSync( __dirname + "/data/quoteList.json" );
var quotes = JSON.parse( quoteContent ).quotes;
var feedListContent = fs.readFileSync( __dirname + "/data/feedList.json" );
var feeds = JSON.parse( feedListContent ).feeds;
var feedLength = feeds.length;
var experiment = { "users": 2, "frequency": 1000 };
// Clear memory out bit
quoteContent = null;
feedListContent = null;
console.log("Concurrent users:", experiment.users);
console.log("Every :", (experiment.frequency / 1000) + " seconds" );
setInterval(
function()
{
var rndQuote;
for(i=0;i<experiment.users;i++)
{
delayPost( (experiment.frequency) / (experiment.users + 1) );
}
}
, experiment.frequency);
function delayPost( delay )
{
setTimeout(
function()
{
rndQuote = getRandomQuote();
postToRandomFeed( rndQuote );
}
, delay );
}
function getRandomQuote()
{
var n = Math.floor( Math.random() * feedLength );
return quotes[n];
}
function postToRandomFeed( theQuote )
{
var n = Math.floor( Math.random() * feedLength );
var feed = feeds[n];
var kloudId = feed.kloudId
, feedId = feed._id
, uid = feed.owner["$oid"]
, url = "quote://" + uuid.new() // Generate random URL
, quote = theQuote.quote
, author = theQuote.author;
var data = { "url" : url, "title": quote, "text": quote, "tags": [author], "feeds": [ feedId ] }
, postTo = [ "http://127.0.0.1:4100/api/content/post/" + uid + "/wintermead" /*, "http://127.0.0.1:4101/api/content/post/" + uid + "/wintermead"*/ ]
, ports = [ 4100 ];
var st = gt();
var postUrl = postTo[ Math.floor(Math.random()*postTo.length) ];
console.log( postUrl );
rest.post( postUrl,
{
"data": data
, "headers":
{ "kloudid": kloudId, 'Connection':'close', 'Connection-Timeout': 10 }
}
).on('complete',
function( data, response )
{
gt( "end: ",st );
if (response.statusCode == 202 ||response.statusCode == 201 || response.statusCode == 200)
{
try {
data =JSON.parse( data );
} catch(e) {}
if( data && data.error )
console.log( "1", data.message );
else
console.log( "2", data._id );
// you can get at the raw response like this...
}
else
{
console.log( "! Error ", response.statusCode, data );
}
}
);
}
function gt( name, lastTime )
{
if( !name || name=="start" )
{
var d = new Date().getTime();
var uu = uuid.new();
return (d + "|" + uu);
}
else if( lastTime )
{
var props = lastTime.split("|");
var time = parseInt(props[0]);
var uu = props[1];
console.log( uu, name, ( new Date().getTime() - time ));
}
else
{
console.log( "logging: invalid start time " + name);
}
}