apache ab测试命令详解
https://www.cnblogs.com/dragonflyer/p/6137514.html
-c 并发请求个数。默认是一
-n 总的请求个数。默认是一
-p post请求的参数文件,可以为相对路径或绝对路径。eg:id=1&uid=2
-T 设置Content-Type头部。eg:application/x-www-form-urlencoded
get请求
D:\PHPStudy\PHPTutorial\Apache\bin>ab -c 3 -n 6 http://www.test.cn/index.php?id=1
post请求
D:\PHPStudy\PHPTutorial\Apache\bin>ab -c 3 -n 6 -p “post_data.txt” -T “applicati
on/x-www-form-urlencoded” http://www.test.cn/index.php?id=1
index.php
<?php
require "DateBase.php";
header("Content-type: text/html; charset=utf-8");
//连接数据库
$conn = DateBase::getInstence();
if($_GET){
$sql = 'insert into goods (name,uid) values ('.$_POST['id'].','.$_POST['uid'].')';
$conn->query($sql);
}else{
$sql = 'select * from goods';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo $row['id'].'--'.$row['name'].'--'.$row['uid'].'<br>';
}
} else {
echo "0 结果";
}
}
DateBase.php
class DateBase{
private static $_instence = null;
private function __construct(){}
public static function getInstence(){
if(!self::$_instence instanceof self){
self::$_instence = new self();
}
return self::$_instence->connect();
}
private function connect(){
//连接数据库
$conn = new mysqli('localhost','root','root','test');
//设置数据库编码格式
$conn->set_charset('utf8');
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
return $conn;
}
}