Created
December 25, 2015 05:39
-
-
Save petermikitsh/ebbf5ea6aa85100bfa57 to your computer and use it in GitHub Desktop.
simple nodejs server for cpu, i/o benchmarking
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
#!/usr/bin/env node | |
// to run: `node server.js` or `./server.js` | |
var crypto = require("crypto"), | |
fs = require('fs'), | |
http = require("http"); | |
function fibonacci (n) { | |
if (n < 2) { | |
return 1; | |
} else { | |
return fibonacci(n-2) + fibonacci(n-1); | |
} | |
}; | |
var server = http.createServer(function(req, res) { | |
if (req.url == "/cpu") { | |
res.writeHead(200, {"Content-Type": "text/plain"}); | |
res.write(String(fibonacci(30))); | |
res.end(); | |
} else if (req.url == "/io") { | |
res.writeHead(200, {"Content-Type": "text/plain"}); | |
fs.writeFile("./.tmp", crypto.randomBytes(1048576).toString('hex'), function (err) { | |
if (err) { | |
return console.log(err); | |
} else { | |
fs.readFile("./.tmp", 'utf8', function (err, data) { | |
if (err) { | |
return console.log(err); | |
} else { | |
res.writeHead(200); | |
res.write(data) | |
res.end(); | |
} | |
}); | |
} | |
}); | |
} else { | |
res.writeHead(200); | |
res.end(); | |
} | |
}); | |
server.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment