我在比较MySQL数据库的写性能方面比较了node.js和PHP.我正在使用Apache Benchmark,虚拟机中的linux Mint,最新的mysql-server(5.5.43)和带有node.js from here的MySQL驱动程序.
我用的代码是
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到Apache2服务器的index.php文件
ab -n 1000 -c 100 http://localhost/
PHP MySQL写性能
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到server.js文件中的node.js服务器
ab -n 1000 -c 100 http://localhost:1337/
node.js MySQL写性能
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
我的印象是node.js在I / O数据库操作中优于PHP.因此,令我惊讶的是,无论我尝试哪种SQL语句(也尝试过SELECT * FROM Persons),node.js都会变得更糟.
不仅如此,而且当cuncurrency级别为100时,节点会记录大量“执行查询时出错”.消息到控制台,1000个请求中只有约500个被写入数据库. mysql节点驱动程序是不是很糟糕,或者我在这里做错了什么?我将衷心感谢您的帮助 :)
谢谢
解决方法:
首先,你没有完成server.js代码.有一个错误,连接到DB的连接太多了.为了解决这个问题,我使用了connectionPool.第二个Apache使用worker来并行运行同一脚本的许多副本.
现在Apache PHP MySQL(XAMP)的结果作为参考点:
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
现在等于我修复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/');
和节点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
如您所见,结果非常接近.但这是针对11名Apache工作者的一个节点进程.如果我在方程式中添加聚类会发生什么?这是修改后的代码:
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);
}
四个节点工人的结果:
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
为了好奇,我为10名工人添加了节点的结果:
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
我的笔记本电脑是Core2Duo T6600,Ubuntu 14.04.3,php 5.5.9,节点0.10.37,mysql 5.5.44