Skip to content

Instantly share code, notes, and snippets.

@luchenqun
Created March 19, 2018 10:28
Show Gist options
  • Select an option

  • Save luchenqun/bc5e1530a08b0cde8f0a015beda95ab3 to your computer and use it in GitHub Desktop.

Select an option

Save luchenqun/bc5e1530a08b0cde8f0a015beda95ab3 to your computer and use it in GitHub Desktop.
ws与http分别调用10万次压力测试

通讯协议简单压力测试

websocket 或者 http分别调用eth_accounts,接收到后台数据之后立马再次调用此接口。调用10万次,结果大概如下: websocket: 17287ms
http: 37491ms

Websocket压测代码

const WebSocket = require('ws');
const ws = new WebSocket('ws://127.0.0.1:6788');
var id = 0;
const COUNT = 100000;

var eth_accounts = {
    "method": "eth_accounts",
    "jsonrpc": "2.0",
    "id": id,
    "params": []
}

ws.on('open', () => {
    connect = true;
    try {
        // 第一次发送数据地方
        console.log('==========================================')
        console.log('Send Data:', postData);
        ws.send(postData);
    } catch (error) {
        console.log(`Send error:${error}`);
        connect = false;
    }
});

var postData = JSON.stringify(eth_accounts);
var pre = new Date().getTime();
ws.on('message', (replyData) => {
    if(eth_accounts.id - id >= COUNT){
        ws.close(1000, "i love you too...")
        console.log("speed time:" + ((new Date().getTime()) - pre));
        console.log("last:", replyData);
    } else {
        ws.send(postData);
    }
    eth_accounts.id++;
});

ws.on('close', () => {
    connect = false;
    console.log('\nWebsocket disconnected!');
});

打印输出如下:

==========================================
Send Data: {"method":"eth_accounts","jsonrpc":"2.0","id":0,"params":[]}
speed time:17287
last: {"id":0,"jsonrpc":"2.0","result":["0x00ef0a55fa7872a9dbc53bc3ec84ee70a1b4fb19"]}
Websocket disconnected!

HTTP压测代码

const http = require('http');
var id = 0;
const COUNT = 100000;

const hostname = '127.0.0.1';
const port = 6789;
var pre = new Date().getTime();

var eth_accounts = {
    "method": "eth_accounts",
    "jsonrpc": "2.0",
    "id": id,
    "params": []
}

var request = (postData) => {
    var options = {
        hostname: hostname,
        port: port,
        path: '',
        method: 'POST',
        agent: false,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': postData.length
        }
    };

    var replyData = '';
    var error = false;
    var req = http.request(options, (res) => {
        if (res.statusCode == 200) {
            res.on('data', (data) => { replyData += data; }).on('end', function () {
                if(eth_accounts.id - id >= COUNT){
                    console.log("speed time:" + ((new Date().getTime()) - pre));
                    console.log("last:", replyData);
                } else {
                    request(postData);
                }
                eth_accounts.id++;
            });
        } else {
            console.log("error", (eth_accounts.id - id));
        }
    });

    req.on('error', (e) => {
        console.error('error', e);
    });

    req.write(postData);
    req.end();
}

var postData = JSON.stringify(eth_accounts);
console.log('==========================================')
console.log('Send Data:', postData);
request(postData);

打印输出如下:

Send Data: {"method":"eth_accounts","jsonrpc":"2.0","id":0,"params":[]}
speed time:37491
last: {"id":0,"jsonrpc":"2.0","result":["0x00ef0a55fa7872a9dbc53bc3ec84ee70a1b4fb19"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment