节点.js MySQL 性能
我正在比较node.js与PHP在MySQL数据库的写入性能方面。我在这里使用Apache Benchmark,linux Mint,最新的mysql-server(5.5.43)和带有node.js的MySQL驱动程序。我使用的代码是
服务器.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/');
索引.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 写入性能
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.js file 中的服务器
ab -n 1000 -c 100 http://localhost:1337/
节点.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)节点.js结果都变得更糟。
不仅如此,当cunrency级别为100时,node会将大量“执行查询时出错”消息记录到控制台,并且1000个请求中只有约500个被写入数据库。mysql节点驱动程序就是那么糟糕,还是我在这里做错了什么?我真的很感激你的帮助:)
谢谢