2

我注意到普通 node.js 与 node-webkit 之间的速度存在显着差异

我所做的只是使用 HTTP 模块连接到 localhost couchdb。

相同的代码需要大约 520 毫秒来建立连接并在 node-webkit 中返回响应,而在普通 node.js 中需要大约 10 毫秒

对node-webkit有更深入了解的人可以帮助解释造成这种情况的原因吗?

代码如下

Class = new function() {};
Class.prototype.info = function(s) {console.log(console,s);}
Class.prototype.err = function(s) {console.log(console,s);}
Class.prototype.warning = function(s) {console.log(console,s);}
Class.prototype.debug = function(s) {console.log(console,s);}
Class.prototype.postMessage = function(oterm, msg) {
    __cb.shift().call(this, msg);
}
Class.prototype.onMsgFor = {};


Class.prototype.__agentkeepalive = require('agentkeepalive');    
Class.prototype.__http = require("http");
Class.prototype.__follow = require("follow");
Class.prototype.__http.globalAgent.maxSockets = 1;
Class.prototype._onInit = function(props) {
    this._host = props["host"];
    this._port = props["port"];
    this._db_name = props["name"];

    this._agent = new this.__agentkeepalive({
    minSockets: 1
    ,maxSockets: 1
    , maxKeepAliveRequests: 0
    , maxKeepAliveTime: 300000
    });
};

Class.prototype.onMsgFor["connect_request_in"] = function(msg) {
    var err;
    var self = this;

    self._connect(this._host, this._port, this._db_name);
};


/*************************************************************************/
Class.prototype._connect = function(host, port, namespace) {
    var self = this;
    var err;

    function _onConnect(code, body, hdrs) {
    function _onCreate(code, body, hdrs) {
        if (code == 201) { // created
        //self._registerChangeNotification();
        self.postMessage("connect_response_out", {name: namespace});
        } else {
        alert(code);
        err = "Unable to create namespace: " + namespace;
        self.error(err);
        self.postMessage("error_response_out", {text_content:err});
        }
    }

    if (code == undefined) { // no code means error connecting
        self.error("Error connecting to DB: " + body);

        self.postMessage("error_response_out", {text_content:body 
                            ,question: "Is CouchDB up?"});
    } else if (code == 200) { // ok
        if (body.indexOf(namespace) != -1) {
        //self._registerChangeNotification();
        self.postMessage("connect_response_out", {name: namespace});
        } else {
        self.warning("No such namespace: " + namespace);
        self._request(host, port, namespace, "PUT", _onCreate);
        }
    } else {
        alert("Unexpected code: " + code);
    }

    return;
    }

    self._request( host, port, "_all_dbs", "GET", _onConnect);
};

Class.prototype._request = function(host, port, namespace, method, cb, uuid, arg, opts) {
    var t = (new Date()).getTime(); 
    var self = this;
    var path = "/" + escape(namespace) + (uuid ? ("/" + escape(uuid)) : "");
    var req, k, buf = "", headers = {};

    if (method == "POST" || method == "PUT") {
    headers["content-type"] = "application/json";
    buf = JSON.stringify(arg);
    }

    if (opts) {
    path += "?";

    for (k in opts) {
        path += k + "=" + opts[k];
    }
    }

    self.info("http://" + host + ":" + port + path);
    req = this.__http.request({hostname: host
                   , port: port
                   , path: path
                   , method: method
                   , headers : headers
                   , agent: this._agent
                  });

    req.setNoDelay(true)
    req.setSocketKeepAlive(true, 0);


    function _onError(err) {
    cb(undefined, err.message, {});
    }

    function _onSocket(socket) {
    console.log("[SOCKET: " + socket.localPort + "] , " + ((new Date()).getTime() - t) + " , W:" + socket.bytesWritten + " / R:" + socket.bytesRead);

    function _onEnd() {
        console.log("** END ** ")
    }

    function _onClose() {
        console.log("** CLOSE **");
    }

    if (!socket.localPort) { 
        socket.setNoDelay(true)
        socket.setKeepAlive(true, 0);

        socket.on("end", _onEnd);
        socket.on("close", _onClose);
    }
    }

    function _onResponse(response) {
    var len = response.headers["content-length"];
    var encoding = response.headers["transfer-encoding"];
    var payload = "";

    console.log(" <<< [RESPONSE: " + response.statusCode+ "] " + (new Date()).getTime() + " , " + ((new Date()).getTime() - t));

    function _onEnd() {
        switch (response.statusCode) {
        case 200: // ok
        case 201: // created
        try { 
            payload = JSON.parse(payload || "");
        } catch (e) {
            self.error("Error parsing payload");
            cb(undefined,e.message,{});

            return;
            /*****************************************************/
        }
        break;
        case 400: // bad request
        break;
        case 404: // not found
        break;
        case 409: // conflict
        break;
        case 415: // Unsupported Media Type
        break;
        default:
        alert("ACK! unknown code: " + response.statusCode);
        break;
        }

        //console.log("PAYLOAD: " + JSON.stringify(payload));
        cb(response.statusCode, payload, response.headers);
    }

    function _onClose() {
        console.log(">> CLOSE");
    }

    function _onData(chunk) {
        payload += (chunk || "");
    }


    self.debug("response returned in (ms): " + ((new Date()).getTime() - t));
    // console.log((new Date()).getTime());

    response.on("data", _onData);
    response.on("end", _onEnd);
    response.on("close", _onClose);
    }

    function _onClose() {
    console.log("[CLOSE]");
    }

    function _onEnd() {
    console.log("[END]");
    }

    req.on("socket", _onSocket);
    req.on("response", _onResponse);
    req.on("error", _onError);
    req.on("close", _onClose);
    req.on("end", _onEnd);
    console.log(" >>> [REQUEST: " + method + " " + path + "] " + t)
    req.end(buf);
}


var __cb = [
    function(msg) {
    console.log("CONNECTED " + msg);
    }
];



var o = new Class();
o._onInit({host: "localhost"
       ,port: 5984
       ,name: "stack"});

o.onMsgFor["connect_request_in"].call(o);

谢谢你

4

1 回答 1

1

我当然不是专家,但这可能是由于他们将节点运行时绑定到 webkit 运行时的方式。

Node 有一个高度优化、高度精简的运行时,任何时候只运行最必要的。

在 webkit 中情况并非如此(至少在服务器编程方面),因为在任何时候它都会有渲染引擎、任何加载的模块/扩展和页面脚本、css 引擎等等,所有这些都在流失。

查看 Node-Webkit wiki 的链接,该链接解释了运行时是如何合并的:https ://github.com/rogerwang/node-webkit/wiki/How-node.js-is-integrated-with-chromium

希望这能有所启发!

于 2013-08-16T05:41:45.327 回答