1
\$\begingroup\$

I am working on a NodeJS port listener code that listens to a particular TCP port on the server. The obtaining the data from the port, the data is split using a delimited (for instance comma ,) and inserts this data into the MySQL database.

I have written the code and I was successfully able to insert data into MySQL server. My issue is that the process is consuming more CPU than expected.

For instance, I ran the code on a 2 core 4 GB RAM Linux Server. The process consumes about 25-50% CPU when only one packet is received every 60 secs. The project should be able to listen to at least 00 packets of data per second on this configuration.

var net = require('net');

    var mysql = require('mysql');
    var PORT = 5000;

    net.createServer(function(sock) {
     console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

  sock.on('data', function(data) {

    console.log('DATA ' + sock.remoteAddress + ': ' + data);
    var dataArray = data.toString().split(',');

    var pool = mysql.createPool({
    host: hostname,
    user: "abc",
    password: "123",
    database: "abc",
    connectionLimit:100
});

  console.log("Connected!");
  var sql = "INSERT INTO `table`(`val1`, `val2`, `val3`, `val4`, `val5`, `val6`) VALUES ('1','"+dataArray[6]+"','"+dataArray[10]+"','"+dataArray[9]+"','"+dataArray[11]+"','"+dataArray[13]+"')";
  console.log(sql);

  pool.getConnection(function(err, connection) {
        if (err) throw err;

  connection.query(sql, function (err, result) {
    connection.release() 
    if (err) throw err;

    console.log("Data Inserted into Table1");
  });

  var sql = "INSERT INTO `table2`(`datafield`) VALUES ('"+data+"')";
  console.log(sql);
  pool.getConnection(function(err, connection) {
        if (err) throw err;

  connection.query(sql, function (err, result) {
    connection.release() 
    if (err) throw err;
    console.log("Data Inserted into Table2");
  });

    sock.write('You said "' + data + '"');

    connection.release();
  });

 sock.on('close', function(data) {

   console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);

 });
}).listen(PORT);

console.log('Server listening on '+ PORT);
\$\endgroup\$
  • \$\begingroup\$ Please take care to format your code correctly. \$\endgroup\$ – πάντα ῥεῖ Aug 3 at 6:33
  • \$\begingroup\$ I have removed the ` from the code, which I think was the issue with the format. \$\endgroup\$ – Nixon Aug 3 at 6:42
  • \$\begingroup\$ Also do you need all those bloating empty lines? \$\endgroup\$ – πάντα ῥεῖ Aug 3 at 6:44
  • \$\begingroup\$ Not Necessarily. It can be removed, not an issue. But do you have a solution to the issue I face? How can we optimized the solution for lesser CPU consumption? \$\endgroup\$ – Nixon Aug 3 at 6:52
  • \$\begingroup\$ Your code is not complete both net.createServer and sock.on do not close! I've attempted to correct this and other minor formatting issues so that everyone can read your intent... have ya by chance looked into async and await? ... I've a feeling that such syntactic sugar might be a treat for making the code a bit more responsive. \$\endgroup\$ – S0AndS0 Aug 3 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.