本人新手, 没有从事过后端开发, 最近在自学nodejs。我在用阿帕奇的测试工具ab 来测试并发访问:请问下 10000条总数据, 1000并发。每次差不多10秒钟。 算快还是慢。 应该是我代码写的有问题吧。 请大佬帮忙看看谢谢。
Mysql连接池模块的代码:
const mysql = require("mysql");
var mysqlPool = mysql.createPool({
host:'localhost',
user:'root',
password:'123456',
database:'test'
});
function query(sql,callback){
mysqlPool.getConnection(function(err,conn){
if(err){
callback(err,null,null);
}else{
conn.query(sql,function(err,rows){
//释放连接
conn.release();
//事件驱动回调
callback(err,rows);
});
}
});
}
exports.query=query;
主Server代码:
const mysql = require("mysql");
const express = require("express");
const bodyParser = require("body-parser");
var mysqlpool=require("./mysqlpool");
var server = express();
var sqlConnect = mysql.createConnection({
host:'localhost',
password:'123456',
user:'root',
database:"test"
});
sqlConnect.connect();
//请求
server.get("/testSql",(req,res)=>{
var userName = req.query.userName;
var userAge = req.query.userAge;
var userGender = req.query.userGender;
console.log(userName,userAge,userGender);
var sql = "INSERT INTO userinfo(userName,userAge,userGender) values ("+"'"+userName+"',"+userAge+","+userGender+")";
sqlConnect.query(sql,(error,data)=>{
if(error){
console.log(error);
res.send("error");
}else{
res.send("ok");
}
});
});
// 测试的就是这个方法。
server.get("/testPool",(req,res)=>{
var userName = req.query.userName;
var userAge = req.query.userAge;
var userGender = req.query.userGender;
console.log(userName,userAge,userGender);
var sql = "INSERT INTO userinfo(userName,userAge,userGender) values ("+"'"+userName+"',"+userAge+","+userGender+")";
mysqlpool.query(sql,(qerr,vals)=>{
if(!qerr){
res.send("ok");
}else{
res.send("error");
}
});
});
server.get("/testNormal",(req,res)=>{
res.send("ok");
});
//开启成功
server.listen(3030,()=>{
console.log("开启成功");
});
这是测试命令:
ab -n 10000 -c 1000 "http://localhost:3030/testPool?userName=zky&userAge=20&userGender=1"
这是测试结果!
Requests per second: 107.98 [#/sec] (mean)
Time per request: 9261.330 [ms] (mean)
Time per request: 9.261 [ms] (mean, across all concurrent requests)
Transfer rate: 21.09 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 7.2 1 502
Processing: 1491 8744 3529.9 7745 17801
Waiting: 1443 8739 3532.3 7741 17793
Total: 1491 8746 3529.9 7746 17803
Percentage of the requests served within a certain time (ms)
50% 7746
66% 7955
75% 9778
80% 11212
90% 16620
95% 17316
98% 17486
99% 17524
100% 17803 (longest request)