1 var path = require('path'),
2 exec = require('child_process').exec,
3 http = require('http');
5 var port = parseInt(process.env.PORT || 8000);
7 console.log('pid ' + process.pid);
9 var fixed = makeString(20 * 1024, 'C'),
14 var useDomains = process.env.NODE_USE_DOMAINS;
16 // set up one global domain.
18 var domain = require('domain');
19 var gdom = domain.create();
20 gdom.on('error', function(er) {
21 console.log('Error on global domain', er);
27 var server = http.createServer(function (req, res) {
29 var dom = domain.create();
34 var commands = req.url.split('/');
35 var command = commands[1];
37 var arg = commands[2];
38 var n_chunks = parseInt(commands[3], 10);
41 if (command == 'bytes') {
44 throw new Error('bytes called with n <= 0')
45 if (storedBytes[n] === undefined) {
46 console.log('create storedBytes[n]');
47 storedBytes[n] = makeString(n, 'C');
49 body = storedBytes[n];
51 } else if (command == 'buffer') {
54 throw new Error('buffer called with n <= 0');
55 if (storedBuffer[n] === undefined) {
56 console.log('create storedBuffer[n]');
57 storedBuffer[n] = new Buffer(n);
58 for (var i = 0; i < n; i++) {
59 storedBuffer[n][i] = 'C'.charCodeAt(0);
62 body = storedBuffer[n];
64 } else if (command == 'unicode') {
67 throw new Error('unicode called with n <= 0');
68 if (storedUnicode[n] === undefined) {
69 console.log('create storedUnicode[n]');
70 storedUnicode[n] = makeString(n, '\u263A');
72 body = storedUnicode[n];
74 } else if (command == 'quit') {
75 res.connection.server.close();
78 } else if (command == 'fixed') {
81 } else if (command == 'echo') {
82 res.writeHead(200, { 'Content-Type': 'text/plain',
83 'Transfer-Encoding': 'chunked' });
92 // example: http://localhost:port/bytes/512/4
93 // sends a 512 byte body in 4 chunks of 128 bytes
95 res.writeHead(status, { 'Content-Type': 'text/plain',
96 'Transfer-Encoding': 'chunked' });
97 // send body in chunks
98 var len = body.length;
99 var step = Math.floor(len / n_chunks) || 1;
101 for (var i = 0, n = (n_chunks - 1); i < n; ++i) {
102 res.write(body.slice(i * step, i * step + step));
104 res.end(body.slice((n_chunks - 1) * step));
106 var content_length = body.length.toString();
108 res.writeHead(status, { 'Content-Type': 'text/plain',
109 'Content-Length': content_length });
114 function makeString(size, c) {
116 while (s.length < size) {
122 server.listen(port, function () {
123 console.log('Listening at http://127.0.0.1:'+port+'/');
126 process.on('exit', function() {
127 console.error('libuv counters', process.uvCounters());