-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspeedtest.js
53 lines (39 loc) · 1.36 KB
/
speedtest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser')
var app = express();
app.use(bodyParser.raw({inflate:false,type:"*/*",limit:1024*1024*1024*10}));
app.use(function(err, req, res, next){
console.log("error: "+err.stack);
res.status(500).send('Something broke!');
});
app.get('/',function(req,res) {
res.sendFile(__dirname+"/index.html");
console.log("Incoming Request from "+req.connection.remoteAddress);
});
app.get('/blob/:size',function (req,res) {
var size=req.params.size;
if (!size || size<=0) size=1;
for (var i=0;i<size;i++) res.write(blob);
res.end();
});
app.post('/blob/',function (req,res) {
res.send("OK: "+req.body.length);
});
var blob=new Buffer(1024*1024);
// ---------- initialize random blob
fs.open("/dev/urandom","r",function (err,fd) {
fs.readSync(fd,blob,0,1024*1024,0);
});
var listen="0.0.0.0";
var port="8081";
opt=require('node-getopt').create([
["l","bindaddress=ARG","Listen to this IP (any)"],
["p","port=ARG","Listen to this TCP Port (8081)"],
['h','help','display this help']
]).bindHelp().parseSystem();
if (opt.options.bindaddress) listen=opt.options.bindaddress;
if (opt.options.port) port=opt.options.port;
var server = app.listen(port,listen, function() {
console.log('Listening on %s:%d', listen, server.address().port);
});