Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

php - node.js MySQL performance

I am comparing node.js versus PHP in terms of write performance to MySQL database. I am using Apache Benchmark, linux Mint in virtual machine, newest mysql-server(5.5.43) and driver for MySQL with node.js from here. The code I used is

server.js

var http = require('http');
var mysql = require('mysql');
var server = http.createServer(function (req, res) {

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    database : 'testDB'
});

connection.connect();
connection.query("INSERT INTO Persons (LastName, FirstName, Address, City) VALUES ('Futterkiste', 'Alfreds', 'Obere Str. 57', 'Berlin')", function(err, rows, fields) {
    if (!err)
        console.log('The solution is: ', rows);
    else
        console.log('Error while performing Query.');
});

connection.end();

res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
});

server.listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

index.php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "testDB";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO Persons (LastName, FirstName, Address, City) VALUES ('Futterkiste', 'Alfreds', 'Obere Str. 57', 'Berlin')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
echo "Hello world";
?>

Apache Benchmark to Apache2 server with index.php file

ab -n 1000 -c 100 http://localhost/

PHP MySQL write performance

Concurrency Level:      100
Time taken for tests:   1.328 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      230000 bytes
HTML transferred:       43000 bytes
Requests per second:    752.99 [#/sec] (mean)
Time per request:       132.804 [ms] (mean)
Time per request:       1.328 [ms] (mean, across all concurrent requests)
Transfer rate:          169.13 [Kbytes/sec] received

Apache Benchmark to node.js server in server.js file

ab -n 1000 -c 100 http://localhost:1337/

node.js MySQL write performance

Concurrency Level:      100
Time taken for tests:   3.896 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      113000 bytes
HTML transferred:       12000 bytes
Requests per second:    256.68 [#/sec] (mean)
Time per request:       389.585 [ms] (mean)
Time per request:       3.896 [ms] (mean, across all concurrent requests)
Transfer rate:          28.33 [Kbytes/sec] received

I was under the impression that node.js outperforms PHP in I/O database operations. So it surprises me that no matter which SQL statement I try (also tried SELECT * FROM Persons) node.js turns out worse.

Not only that but also when cuncurrency level is 100, node logs a ton of 'Error while performing Query.' messages to the console and only ~500 out of 1000 requests get written to the database. Are mysql node drivers just that bad or I am doing something very wrong here? I would really appreciate your help :)

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all you didn't finish server.js code. There is a bug with too many connections opened to DB. To fix this I used connectionPool. And second of all Apache use workers to run many copies of same script in parallel.

Now the result for Apache + PHP + MySQL (XAMP) as a reference point:

Concurrency Level:      100
Time taken for tests:   7.476 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      230000 bytes
HTML transferred:       42000 bytes
Requests per second:    133.77 [#/sec] (mean)
Time per request:       747.557 [ms] (mean)
Time per request:       7.476 [ms] (mean, across all concurrent requests)
Transfer rate:          30.05 [Kbytes/sec] received

Now to equal the chances I fixed server.js

var http = require('http');
var mysql = require('mysql');
var connection = mysql.createPool({
    connectionLimit: 10,
    host     : 'localhost',
    user     : 'test',
    password : 'test',
    database : 'testDB'
});
var server = http.createServer(function (req, res) {
    connection.query("INSERT INTO Persons (LastName, FirstName, Address, City) VALUES ('Futterkiste', 'Alfreds', 'Obere Str. 57', 'Berlin')", function(err, rows, fields) {
        if (!err)
            console.log('The solution is: ', rows);
        else {
            console.log('Error while performing Query.');
        }
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello World');
    });
});
server.listen(1337, '127.0.0.1');
server.on('close', function() {
    connection.end();
})
console.log('Server running at http://127.0.0.1:1337/');

And results of Node + MySQL:

Concurrency Level:      100
Time taken for tests:   7.289 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      112000 bytes
HTML transferred:       11000 bytes
Requests per second:    137.19 [#/sec] (mean)
Time per request:       728.899 [ms] (mean)
Time per request:       7.289 [ms] (mean, across all concurrent requests)
Transfer rate:          15.01 [Kbytes/sec] received

As you can see the results are very close. But this is one node process against 11 Apache workers. What happens if I add clusters to the equation? Here is the modified code:

var http = require('http');
var mysql = require('mysql');
var cluster = require('cluster');

if (cluster.isMaster) {
    cluster.fork();
    cluster.fork();
    cluster.fork();
    cluster.fork();
} else {
    var connection = mysql.createPool({
        connectionLimit: 10,
        host     : 'localhost',
        user     : 'test',
        password : 'test',
        database : 'testDB'
    });
    var server = http.createServer(function (req, res) {
        connection.query("INSERT INTO Persons (LastName, FirstName, Address, City) VALUES ('Futterkiste', 'Alfreds', 'Obere Str. 57', 'Berlin')", function(err, rows, fields) {
            if (!err)
                console.log('The solution is: ', rows);
            else {
                console.log('Error while performing Query.');
            }
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('Hello World');
        });
    });

    server.listen(1337, '127.0.0.1');
    server.on('close', function() {
        connection.end();
    })
    console.log('Server running at http://127.0.0.1:1337/ worker:' + cluster.worker.id);
}

Four node workers results:

Concurrency Level:      100
Time taken for tests:   2.782 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      112000 bytes
HTML transferred:       11000 bytes
Requests per second:    359.48 [#/sec] (mean)
Time per request:       278.179 [ms] (mean)
Time per request:       2.782 [ms] (mean, across all concurrent requests)
Transfer rate:          39.32 [Kbytes/sec] received

For curiosity I add results for node with 10 workers:

Concurrency Level:      100
Time taken for tests:   2.647 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      112000 bytes
HTML transferred:       11000 bytes
Requests per second:    377.84 [#/sec] (mean)
Time per request:       264.665 [ms] (mean)
Time per request:       2.647 [ms] (mean, across all concurrent requests)
Transfer rate:          41.33 [Kbytes/sec] received

My laptop is Core2Duo T6600, Ubuntu 14.04.3, php 5.5.9, node 0.10.37, mysql 5.5.44


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...