2010-07-16 02:47:25 +08:00
|
|
|
common = require("../common");
|
|
|
|
assert = common.assert
|
2010-03-10 08:27:49 +08:00
|
|
|
net = require("net");
|
2009-07-15 23:00:15 +08:00
|
|
|
// settings
|
|
|
|
var bytes = 1024*40;
|
|
|
|
var concurrency = 100;
|
|
|
|
var connections_per_client = 5;
|
|
|
|
|
|
|
|
// measured
|
|
|
|
var total_connections = 0;
|
|
|
|
|
|
|
|
var body = "";
|
|
|
|
for (var i = 0; i < bytes; i++) {
|
|
|
|
body += "C";
|
|
|
|
}
|
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
var server = net.createServer(function (c) {
|
2009-07-15 23:00:15 +08:00
|
|
|
c.addListener("connect", function () {
|
|
|
|
total_connections++;
|
2010-08-17 23:21:43 +08:00
|
|
|
common.print("#");
|
2010-02-17 05:15:30 +08:00
|
|
|
c.write(body);
|
2010-04-09 01:44:22 +08:00
|
|
|
c.end();
|
2009-07-15 23:00:15 +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
|
|
|
|
2009-07-15 23:00:15 +08:00
|
|
|
client.setEncoding("utf8");
|
|
|
|
|
|
|
|
client.addListener("connect", function () {
|
2010-08-17 23:21:43 +08:00
|
|
|
common.print("c");
|
2009-07-15 23:00:15 +08:00
|
|
|
client.recved = "";
|
|
|
|
client.connections += 1;
|
|
|
|
});
|
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
client.addListener("data", function (chunk) {
|
2009-07-15 23:00:15 +08:00
|
|
|
this.recved += chunk;
|
|
|
|
});
|
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
client.addListener("end", function () {
|
2010-04-09 01:44:22 +08:00
|
|
|
client.end();
|
2009-07-15 23:00:15 +08:00
|
|
|
});
|
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
client.addListener("error", function (e) {
|
2010-06-24 08:40:51 +08:00
|
|
|
console.log("\n\nERROOOOOr");
|
2010-03-10 08:27:49 +08:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
client.addListener("close", function (had_error) {
|
2010-08-17 23:21:43 +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-07-16 02:47:25 +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++) {
|
|
|
|
runClient(function () {
|
|
|
|
if (++finished_clients == concurrency) server.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2009-07-15 23:00:15 +08:00
|
|
|
|
2009-08-27 00:51:04 +08:00
|
|
|
process.addListener("exit", function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(connections_per_client * concurrency, total_connections);
|
2010-06-24 08:40:51 +08:00
|
|
|
console.log("\nokay!");
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|