2013-07-08 3 views
2

내가 로컬 호스트에 연결하는 일반 Node.js를 VS 노드 웹킷홀수 속도 차이 노드 사이 (~ 10ms의 VS ~ 520ms) 노드 웹킷

내가 뭘 모두가 사이의 속도에 상당한 차이가 납니까 couchdb HTTP 모듈을 사용합니다. 걸리는 반면

동일한 코드

노드 - 웹킷 도움의보다 심층적 인 지식을 가진 사람이 설명 할 수 일반 Node.js를에 ~ 연결을 설정하고 노드 웹킷의 응답을 반환하는 ~ 10ms의를 520ms 소요 무엇이 원인인가?

코드는

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); 

당신에게 슬림

감사합니다 아래

답변

1

내가 확실히 전문가가 아니지만, 그것 때문에 그들이 웹킷 런타임에 노드 런타임을 연결하는 방식에 아마 .

노드는 고도로 최적화 된 hight 간소화 된 런타임을 제공하며 가장 자주 실행되는 경우가 있습니다.

웹 엔진은 적어도 서버 프로그래밍과 관련하여 항상 렌더링 엔진,로드 된 모듈/확장명 및 페이지 스크립트와 CSS 엔진 등을 포함하고 있습니다.

체크 아웃 런타임이 통합 된 방법을 설명 노드 웹킷 위키 링크 : https://github.com/rogerwang/node-webkit/wiki/How-node.js-is-integrated-with-chromium

희망이 몇 가지 실마리를!