Created
August 1, 2013 20:25
-
-
Save mattmcmanus/6134931 to your computer and use it in GitHub Desktop.
A simple example of accepting post requests and streaming them directly to a log file. To test, load up the server > node index.js, the run the benchmark: node bench.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var async = require('async'); | |
var time = process.hrtime(); | |
var totalConnections = 0; | |
var simultaneousConnections = 500; | |
// Make the array async.each will loop over | |
var connections = [] | |
for (var i = 0; i < simultaneousConnections; i++) { | |
connections.push(i) | |
}; | |
async.each(connections, makeRequests, function(err){ | |
console.log('All Done') | |
var diff = process.hrtime(time); | |
var nanoseconds = diff[0] * 1e9 + diff[1]; | |
console.log('benchmark made %d connections and took %d nanoseconds (%d seconds)', totalConnections, nanoseconds, nanoseconds/1000000000); | |
}); | |
function makeRequests(n, next) { | |
for (var i = 0; i < 1000; i++) { | |
console.log(n + ':' + i) | |
totalConnections++; | |
request.post('http://localhost:3000/', {body:n + ':' + i + '============================================================================================================================================================================================================\n'}) | |
}; | |
next() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var fs = require('fs'); | |
setInterval(function(){ | |
console.log(process.memoryUsage().rss/1024/1024 + "mb"); | |
}, 1000) | |
var server = http.createServer(function (req, res) { | |
// console.log(req.method, req.url) | |
var log = fs.createWriteStream('data.log', { flags: 'a', encoding: 'utf-8'}) | |
if (req.method.toLowerCase() == 'post') { | |
// Assumptions: | |
// * The posted data has the new line in it | |
req.pipe(log); | |
res.writeHead(201, {'Content-Type': 'text/plain'}); | |
res.end(); | |
} else { | |
res.writeHead(401, {'Content-Type': 'text/plain'}); | |
res.end(); | |
} | |
}); | |
server.listen(3000, '127.0.0.1', function(){ | |
console.log("Server started") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment