nodejs写入webdis非常慢,请问是为什么呢?
发布于 2年前 作者 jtyjty99999 951 次浏览

代码

var http = require("http");
//http://192.168.1.3:7379/HGET/123:cellphonenumber/
function start(handle) {
    function onRequest(request, response) {
        var options = {
            host:'192.168.1.6',
            port:7379,
            path:'/SADD/' + 'niaAOVU2lg' + ':config/' + '2013-03-09'+Math.random(),
            method:'get'
        };
        var req = http.get(options, function (res) {
            //    console.log("Got response: " + res.statusCode);
            res.on('error',function (e) {
                //     console.log("Got error: " + e.message);
            }).on('data', function (chunk) {
                    //         console.log('BODY: ' + chunk);
                });
        });
        req.on('error', function(e) {
            //  console.log("Got error: " + e.message)
        })
        response.writeHead(200,{'Content-Type':'text/html'});
        response.end()
    }
    var server = http.createServer(onRequest).listen(8888);
}
    exports.start = start;

启动服务器后,利用 setsid webbench -c 1000 -t 60 '192.168.1.2:8888/’进行压测

之后发现 负载服务器.2的cpu几乎跑满,成功150000多个,失败300+

但是192.168.1.6 webdis端,cpu几乎没有负载,访问 http://192.168.1.6:7379/SCARD/niaAOVU2lg:config 可以发现,记录数不断增长,但是非常慢(大约一秒增长300个左右) 请问一下这是怎么回事啊,是我的代码写的有问题么?

1 回复

补充,开了个8核的机器

function start(handle) {
    var http = require("http");
    var cluster = require('cluster');
    var http = require('http');
    var numCPUs = require('os').cpus().length;
    if (cluster.isMaster) {
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }
        cluster.on('death', function (worker) {
            console.log('worker ' + worker.pid + ' died');
            cluster.fork();
        });
    } else {
        function onRequest(request, response) {
            var options = {
                host:'192.168.1.6',
                port:7379,
                path:'/SADD/' + 'niaAOVU2lg' + ':config/' + '2013-03-09' + Math.random(),
                method:'get'
            };
            var req = http.get(options, function (res) {
                //    console.log("Got response: " + res.statusCode);
                res.on('error',function (e) {
                    //     console.log("Got error: " + e.message);
                }).on('data', function (chunk) {
                        //         console.log('BODY: ' + chunk);
                    });
            });
            req.on('error', function (e) {
                //  console.log("Got error: " + e.message)
            })
            response.writeHead(200, {'Content-Type':'text/html'});
            response.end()
        }

        var server = http.createServer(onRequest).listen(8888);
    }
}

exports.start = start;

写入速度提高到3600个每秒了,而压测webdis的极限写入速度是17000个每秒,请问我的代码还有什么问题?

回到顶部