ๆ่ฟๅจ่ๆ ฎๅฐๅ ฌๅธๆไบๅฐๆก็ๆ็จๆนๅฐ node.js ไธ้ข,ไธป่ฆๆฏ็จๅผๆฌ้ซ็ driver ่ท MySQL ไน้็ๆ่ฝ,ๅ ่ฌ็ต่ซ,ๅฆๆๆ็จ MVC ็ไบบไธ่ฆ็จๅ งๅปบ็ ORM ่ทๅคง่ณๆๅฏซๅ ฅ็ๆ็จ,้็ถ ORM ๆ ขๆฏๅคงๅฎถ้ฝ็ฅ้็ไบๆ ,็ข็ซไปๆฌไพๅฐฑๆฏๅๆนไพฟ็ๅทฅๅ ท,ไฝๆฒๆณๅฐๅคง้่ณๆๅฏซๅ ฅไปๅฏไปฅๆ ขๅฐๅฐ่ฟไธๅ,ๆฌๆธฌ่ฉฆๅท่ก node.js、PHP PDO,ไธๆฌก่ท 3000 ๅ insert ๅ่ทๅๆฌก,ไปฅไธๆฏๅฏซๆณไปฅๅ็ตๆ。
็ฐๅข่ชชๆ
- PHP 5.6.15
- MySQL Ver 15.1 Distrib 10.1.9-MariaDB
- node.js 5.8.0
- Windows 7 64bit
- 8GB Ram
ๅฅไปถ่ชชๆ
็จๅผๅฏซๆณ
node.js
console.time('add');
console.log('start');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : '',
password : '',
database : 'test',
});
var length = process.argv[2] || 100;
var counter = 1;
connection.connect();
var add = (counter) => {
connection.query('INSERT INTO `users` SET ?', {name: 'test'}, (err, rows, fields) => {
if (err) {
console.log(err);
}
if (counter < length) {
add(++counter);
} else {
console.log('all done');
console.timeEnd('add');
connection.end();
}
});
};
add(counter);
PHP PDO
<?php
include 'vendor/autoload.php';
echo 'start'.PHP_EOL;
use TimeBenchmark\Stopwatch;
$length = isset($argv[1]) ? $argv[1] : 100;
$counter = 1;
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
$db = new PDO($dsn, '');
$stopwatch = Stopwatch::create();
$stopwatch->start();
$add = function($counter) use ($length, $db, $stopwatch, &$add) {
$sql = "INSERT INTO `users`(`name`) VALUES(:name)";
$stmt = $db->prepare($sql);
$stmt->bindValue(':name', 'test');
$stmt->execute();
if ($counter < $length) {
$add(++$counter);
} else {
$stopwatch->stop();
echo $stopwatch->getElapsedMilliseconds().'ms'.PHP_EOL;
}
};
$add($counter);
ๆธฌ่ฉฆ็ตๆ
node.js
- 75500.335ms
- 76234.479ms
- 60025.145ms
- 68833.407ms
- 71067.471ms
- 63238.021ms
- 63908.226ms
- 57938.820ms
- 66872.004ms
- 66817.116ms
PHP PDO
- 63311.331ms
- 66825.682ms
- 67424.741ms
- 67320.732ms
- 62134.213ms
- 65586.558ms
- 67117.711ms
- 54427.443ms
- 66046.604ms
- 67741.773ms
็ต่ซ
ๅ ถๅฏฆๆธๆๆฒๆๅทฎๅพๅค,ไฝ node.js ๆฏ่ผๆ้ฃ,ๆไปฅ้ๆฏ่งๅฏไธไธๅง。