简体   繁体   中英

NodeJS web server lagging

After 1-2 min. running web server on raspberry pi, i get lag. I use nodeJS, socket.io and sqlite database. I suspect this:

io.sockets.on('connection', function(socket){
setInterval(function(){
fs.readFile('/sys/bus/w1/devices/'+ tempId +'/w1_slave', function(error, buffer){
  // Read data from file (using fast node ASCII encoding).
  var data = buffer.toString('ascii').split(" "); // Split by space
  // Extract temperature from string and divide by 1000 to give celsius
  var temp  = parseFloat(data[data.length-1].split("=")[1])/1000.0;
  // Round to one decimal place
  temp = Math.round(temp * 10) / 10;
  // Add date/time to temperature
    var data = {
        temp_irasai:[{
        laikas: Date.now()+10800000,
        laipsnis: temp
        }]};
    socket.emit('realtime', data);
    var statement = db.prepare("INSERT INTO temp_irasai VALUES (?, ?)");
    statement.run(data.temp_irasai[0].laikas, data.temp_irasai[0].laipsnis);
    statement.finalize();   
});
}, 8000);
});

then i comment this line:

var statement = db.prepare("INSERT INTO temp_irasai VALUES (?, ?)");
statement.run(data.temp_irasai[0].laikas, data.temp_irasai[0].laipsnis);
statement.finalize();

Lag gone, so that's wrong with this function.

But its not working:

db.run("BEGIN TRANSACTION;");
var statement = db.prepare("INSERT INTO temp_irasai VALUES (?, ?);");
statement.run(data.temp_irasai[0].laikas, data.temp_irasai[0].laipsnis);
statement.finalize();   
db.run("commit;");

This is an extremely common problem in sqlite that has been answered in the FAQ. Take a look at https://www.sqlite.org/faq.html#q19 for more details. As quoted:

Actually, SQLite will easily do 50,000 or more INSERT statements per second on an average desktop computer. But it will only do a few dozen transactions per second. Transaction speed is limited by the rotational speed of your disk drive. A transaction normally requires two complete rotations of the disk platter, which on a 7200RPM disk drive limits you to about 60 transactions per second.

Transaction speed is limited by disk drive speed because (by default) SQLite actually waits until the data really is safely stored on the disk surface before the transaction is complete. That way, if you suddenly lose power or if your OS crashes, your data is still safe. For details, read about atomic commit in SQLite..

By default, each INSERT statement is its own transaction. But if you surround multiple INSERT statements with BEGIN...COMMIT then all the inserts are grouped into a single transaction. The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced.

EDIT:

To fix it, what you want to do is to execute "BEGIN" before and "COMMIT" after all of the insert statements are executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM