class echoHttp {
constructor() {
this.app = new Koa();
this.port = 3000;
this.router = new Router();
this.app.use(bodyParser());
this.app.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
//console.log('dataLength:', ctx.request.body.test.length);
yield next();
}));
this.app.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
ctx.body = {
echo: `echo at ${Date()}`, ts: Date.now(),
rbody: ctx.request.body, rquery: ctx.request.query
};
}));
}
launch(port = this.port) {
this.app.listen(port);
console.log(`Server running on port ${port}`);
return Promise.resolve(port);
}
}
function randomString(len = 4) {
var text = "";
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < len; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function summary(data) {
return {
avg: ss.mean(data),
max: ss.max(data),
min: ss.min(data),
stddev: ss.standardDeviation(data),
median: ss.median(data)
};
}
class nsFetch extends events_1.EventEmitter {
constructor(dataSize = 100, sampleSize = 3) {
super();
this.samples = [];
this.dataSize = dataSize;
this.sampleSize = sampleSize;
}
sample(target = "http://localhost:3000", dataSize = this.dataSize, sampleSize = this.sampleSize, delay = 100) {
var body = JSON.stringify({ test: randomString(dataSize) });
this.samples = [];
this.doSample(target, body, sampleSize, delay);
}
doSample(target = "http://localhost:3000", body, sampleSize, delay = 100) {
var tStart = Date.now();
return node_fetch_1.default(target, {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: body
}).then(res => res.json())
.then(json => {
this.samples.push((Date.now() - tStart));
if (sampleSize > this.samples.length)
setTimeout(() => { this.doSample(target, body, sampleSize, delay); }, delay);
else
this.emit("result", target, this.samples);
return this.samples;
})
.catch(console.error);
}
}
let dataServer = process.argv[2] || 'http://localhost:3001';
const netinfo = require('os').networkInterfaces();
const echoPort = parseInt(process.argv[3]) || 3002;
const hostname = process.argv[4] || require('os').hostname();
var adds = [];
Object.keys(netinfo).forEach(key => {
netinfo[key].forEach(nn => {
if ((nn.family == 'IPv4') && (nn.internal == false)) {
adds.push(nn.address);
}
});
});