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'),
13 var useDomains = process.env.NODE_USE_DOMAINS;
15 // set up one global domain.
17 var domain = require('domain');
18 var gdom = domain.create();
19 gdom.on('error', function(er) {
20 console.log('Error on global domain', er);
26 var server = http.createServer(function (req, res) {
28 var dom = domain.create();
33 var commands = req.url.split('/');
34 var command = commands[1];
36 var arg = commands[2];
37 var n_chunks = parseInt(commands[3], 10);
40 if (command == 'bytes') {
43 throw new Error('bytes called with n <= 0')
44 if (storedBytes[n] === undefined) {
45 console.log('create storedBytes[n]');
46 storedBytes[n] = makeString(n, 'C');
48 body = storedBytes[n];
50 } else if (command == 'buffer') {
53 throw new Error('buffer called with n <= 0');
54 if (storedBuffer[n] === undefined) {
55 console.log('create storedBuffer[n]');
56 storedBuffer[n] = new Buffer(n);
57 for (var i = 0; i < n; i++) {
58 storedBuffer[n][i] = 'C'.charCodeAt(0);
61 body = storedBuffer[n];
63 } else if (command == 'quit') {
64 res.connection.server.close();
67 } else if (command == 'fixed') {
70 } else if (command == 'echo') {
71 res.writeHead(200, { 'Content-Type': 'text/plain',
72 'Transfer-Encoding': 'chunked' });
81 // example: http://localhost:port/bytes/512/4
82 // sends a 512 byte body in 4 chunks of 128 bytes
84 res.writeHead(status, { 'Content-Type': 'text/plain',
85 'Transfer-Encoding': 'chunked' });
86 // send body in chunks
87 var len = body.length;
88 var step = ~~(len / n_chunks) || len;
90 for (var i = 0; i < len; i += step) {
91 res.write(body.slice(i, i + step));
96 var content_length = body.length.toString();
98 res.writeHead(status, { 'Content-Type': 'text/plain',
99 'Content-Length': content_length });
104 function makeString(size, c) {
106 while (s.length < size) {
112 server.listen(port, function () {
113 console.log('Listening at http://127.0.0.1:'+port+'/');
116 process.on('exit', function() {
117 console.error('libuv counters', process.uvCounters());