High Node.JS CPU usage while query executes #78

Closed
rayaesh opened this Issue Oct 3, 2012 · 5 comments

Comments

Projects
None yet
3 participants
@rayaesh

rayaesh commented Oct 3, 2012

Hello

We are seeing high cpu usage for the node process when executing a MS SQL Server Stored Procedure while waiting for the query to complete.

Once the response from the DB is received the CPU usage comes down while the response to the client is being assembled and sent back.

Any pointers/suggestions greatly appreciated, Thanks !

Our code is as follows:


function do_something(request, response) { //HTTP request/response objects
    sql.open(conn_str, function(err,conn){
        if (err){
            //log error
            return;
        }
        var query = "exec SomeStoredProcedure ?,?,?";
        var params = [];
        log("Connection made. Executing query: " + query + " - " + params, "debug");
        var self = conn;
        conn.queryRaw(query, params, function(err,results) {
            if (err) {
                log("Error running query: " + err, "error");
                return; 
            }
            log("Got response. Assembling full response", "debug");
            //read results and populate my_results object    
            response.writeHead(200, {
                'Content-Type': 'text/plain'
            });
            response.write(JSON.stringify(my_results));
            log(JSON.stringify(my_results), "debug");
            self.close();
            response.end();
        });
    });
}

@jkint

This comment has been minimized.

Show comment Hide comment
@jkint

jkint Oct 3, 2012

Contributor

Which branch are you using? master is not up to date. The develop branch contains several bug fixes that should help.

Contributor

jkint commented Oct 3, 2012

Which branch are you using? master is not up to date. The develop branch contains several bug fixes that should help.

@rayaesh

This comment has been minimized.

Show comment Hide comment
@rayaesh

rayaesh Oct 3, 2012

Thank you! Confirming that building it out of the development branch fixed the issue.

One other question: I'm currently having to close the connection explicitly - see "self.close()" . Otherwise I'm seeing a spike in number of open files. Am I doing it right because I haven't seen any code sample which does this but if I don't do this then the process eventually runs out of fd's.

Please advise.

P.S: This happens with the development branch too.

rayaesh commented Oct 3, 2012

Thank you! Confirming that building it out of the development branch fixed the issue.

One other question: I'm currently having to close the connection explicitly - see "self.close()" . Otherwise I'm seeing a spike in number of open files. Am I doing it right because I haven't seen any code sample which does this but if I don't do this then the process eventually runs out of fd's.

Please advise.

P.S: This happens with the development branch too.

@jkint

This comment has been minimized.

Show comment Hide comment
@jkint

jkint Oct 4, 2012

Contributor

How long is the process running? I've seen similar but it eventually stabilizes when the V8 collector kicks in. However, we included the close method for finer resource control explicitly for situations such as yours.

Contributor

jkint commented Oct 4, 2012

How long is the process running? I've seen similar but it eventually stabilizes when the V8 collector kicks in. However, we included the close method for finer resource control explicitly for situations such as yours.

@rayaesh

This comment has been minimized.

Show comment Hide comment
@rayaesh

rayaesh Oct 4, 2012

The typical restart interval is 4 to 5 days. The GC seems to be collecting the memory but the number of handles remains unchanged.

Coming back to my question, is it safe and correct for me to call the close method explicitly. Please also comment on the location where I'm calling close (just before sending the response back)

rayaesh commented Oct 4, 2012

The typical restart interval is 4 to 5 days. The GC seems to be collecting the memory but the number of handles remains unchanged.

Coming back to my question, is it safe and correct for me to call the close method explicitly. Please also comment on the location where I'm calling close (just before sending the response back)

@jkint

This comment has been minimized.

Show comment Hide comment
@jkint

jkint Oct 4, 2012

Contributor

Yes, as I mentioned, we exposed the close method for situations such as yours. Please use it as necessary.

And your location of the close call looks fine.

Contributor

jkint commented Oct 4, 2012

Yes, as I mentioned, we exposed the close method for situations such as yours. Please use it as necessary.

And your location of the close call looks fine.

@jguerin jguerin closed this Oct 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment