在本机windows环境下的测试:8G内存,4GCPU,win7系统
插入1000条记录测试比较,php和nodejs的性能
php代码段(php5.6)
$link=mysqli_connect('127.0.0.1','root','666666','db_name');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}else{
echo 'success';
$sql = "insert into web_pic (`uid`,`isimage`,`imgurl`,`pic_date`,`pic_name`) values('2260335','0','/13/orgin/201607131138020369508811.jpg','2018-09-02','sdd')";
$time_start = microtime_float();
for($i=1;$i<1000;$i++){
mysqli_query($link,$sql);
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "it consumes $time seconds\n";
}
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
耗时0.21152591705322 seconds
nodejs代码段:
var connection = mysql.createConnection({
host:'localhost',
'user':'root',
'password':'666666',
database:'db_name',
port:'3306'
});
connection.connect(function(err){
if(err){
console.log('[query]-:'+err);
return;
}
console.log('[connection connect] succeed!');
});
//execute insert operation
var useradd = 'insert into web_pic (`uid`,`isimage`,`imgurl`,`pic_date`,`pic_name`) values(?,?,?,?,?)';
var param=['3000000','1','/13/orgin/201607131138020369508811.jpg','2018-11-01','vvv'];
var st = new Date().getTime();
for(var i=1;i<1000;i++){
connection.query(useradd,param,function(err,rs){
if(err){
console.log('insert err:',err.message);
return;
}else{
//console.log('insert success');
}
});
}
var et = new Date().getTime();
console.log(et-st);
耗时35微秒。
当改为10000条记录插入,php耗时1.650171995163 seconds,nodejs耗时318微秒
据说php7性能很高,可能会比php5.6好些。