2015-05-19 19:00:06 +08:00
|
|
|
'use strict';
|
2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
2010-12-06 06:33:52 +08:00
|
|
|
var assert = require('assert');
|
2010-12-05 08:11:57 +08:00
|
|
|
var net = require('net');
|
|
|
|
|
2009-07-15 23:00:15 +08:00
|
|
|
// settings
|
2010-12-03 09:03:18 +08:00
|
|
|
var bytes = 1024 * 40;
|
2009-07-15 23:00:15 +08:00
|
|
|
var concurrency = 100;
|
|
|
|
var connections_per_client = 5;
|
|
|
|
|
|
|
|
// measured
|
|
|
|
var total_connections = 0;
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
var body = '';
|
2009-07-15 23:00:15 +08:00
|
|
|
for (var i = 0; i < bytes; i++) {
|
2010-12-03 09:03:18 +08:00
|
|
|
body += 'C';
|
2009-07-15 23:00:15 +08:00
|
|
|
}
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
var server = net.createServer(function(c) {
|
2013-05-07 07:37:03 +08:00
|
|
|
console.log('connected');
|
|
|
|
total_connections++;
|
|
|
|
common.print('#');
|
|
|
|
c.write(body);
|
|
|
|
c.end();
|
2009-07-15 23:00:15 +08:00
|
|
|
});
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
function runClient(callback) {
|
2010-07-16 02:47:25 +08:00
|
|
|
var client = net.createConnection(common.PORT);
|
2010-03-10 08:27:49 +08:00
|
|
|
|
2009-07-15 23:00:15 +08:00
|
|
|
client.connections = 0;
|
2010-03-10 08:27:49 +08:00
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
client.setEncoding('utf8');
|
2009-07-15 23:00:15 +08:00
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
client.on('connect', function() {
|
2010-12-03 09:03:18 +08:00
|
|
|
common.print('c');
|
|
|
|
client.recved = '';
|
2009-07-15 23:00:15 +08:00
|
|
|
client.connections += 1;
|
|
|
|
});
|
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
client.on('data', function(chunk) {
|
2009-07-15 23:00:15 +08:00
|
|
|
this.recved += chunk;
|
|
|
|
});
|
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
client.on('end', function() {
|
2010-04-09 01:44:22 +08:00
|
|
|
client.end();
|
2009-07-15 23:00:15 +08:00
|
|
|
});
|
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
client.on('error', function(e) {
|
2010-12-03 09:03:18 +08:00
|
|
|
console.log('\n\nERROOOOOr');
|
2010-03-10 08:27:49 +08:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
client.on('close', function(had_error) {
|
2010-12-03 09:03:18 +08:00
|
|
|
common.print('.');
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, had_error);
|
|
|
|
assert.equal(bytes, client.recved.length);
|
2010-03-10 08:27:49 +08:00
|
|
|
|
|
|
|
if (client.fd) {
|
2010-06-24 08:40:51 +08:00
|
|
|
console.log(client.fd);
|
2010-03-10 08:27:49 +08:00
|
|
|
}
|
|
|
|
assert.ok(!client.fd);
|
|
|
|
|
2009-07-15 23:00:15 +08:00
|
|
|
if (this.connections < connections_per_client) {
|
2010-07-16 02:47:25 +08:00
|
|
|
this.connect(common.PORT);
|
2009-07-15 23:00:15 +08:00
|
|
|
} else {
|
2009-08-27 04:03:19 +08:00
|
|
|
callback();
|
2009-07-15 23:00:15 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
server.listen(common.PORT, function() {
|
2010-03-10 08:27:49 +08:00
|
|
|
var finished_clients = 0;
|
|
|
|
for (var i = 0; i < concurrency; i++) {
|
2010-12-03 09:03:18 +08:00
|
|
|
runClient(function() {
|
2010-03-10 08:27:49 +08:00
|
|
|
if (++finished_clients == concurrency) server.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2009-07-15 23:00:15 +08:00
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
process.on('exit', function() {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(connections_per_client * concurrency, total_connections);
|
2010-12-03 09:03:18 +08:00
|
|
|
console.log('\nokay!');
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|