iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 1
0
自我挑戰組

霍普菜鳥的資訊工作雜記系列 第 21

D21-使用koa測試網路1

  • 分享至 

  • xImage
  •  
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);
        }
    });
});

上一篇
D20-字串轉換
下一篇
D22-使用koa測試網路2
系列文
霍普菜鳥的資訊工作雜記31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言