http://www.cnblogs.com/lwcoding/p/6596089.html
shell curl 实现rest 并发测试
for i in {1..50};
do
curl http://10.43.95.26:5812/rdk/service/app/example/server/my_service &
done
[root@web_dev yansiyu]# cat concurrent.sh
#!/bin/bash
for i in {1..100}
do
#echo $i
curl -Is "http://www.test.com/member/checkmember.php?uid={F032AA26-12C5-46E1-B46D-9D050D3AB6DF}" &
done
#for i in {1..100}
#do
#{
##echo $i
#curl -Is "http://www.test.com/member/checkmember.php?uid={F032AA26-12C5-46E1-B46D-9D050D3AB6DF}"
#}&
#done
#wait
test.php
<?php
//php 使用多线程模拟多用户请求
//模拟500个用户
$co=0;
for ($i=0; $i<200; $i++) {
$pid = pcntl_fork();
if ($pid==-1) {
die("counld not fork\n");
} else if ($pid) {
//父进程会得到子进程号,所以这里是父进程执行的逻辑
$pid_arr[]=$pid;
} else {
//子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
auser($i,$co);
file_put_contents('test.txt','task done'.$i."\n",8);
exit(0);
}
}
foreach ($pid_arr as $pid)
$times=rand(10,100);
$times=1;
for($i=0;$i<$times;$i++){
#$re=curlPost('http://beta2.m.test.com/api/news_list',$datastring,$header);
$re=curlGet('http://www.test.com/member/checkmember.php?uid={F032AA26-12C5-46E1-B46D-9D050D3AB6DF}',$header);
var_dump($re);
if($re=='{"mobile":"","is_vip":"false","expire_time":0}')
$co++;
//$re=curlPost('http://beta2.m.test.com/api/deep_video',$datastring,$header);
$sleep=rand(1,10);
sleep($sleep);
file_put_contents('test.txt','user'.$udi.'read'.$i.'/'.$times.'sleep'.$sleep."\n",8);
}
}
//$re='xxx';
function curlPost($url, $keysArr, $header = array(), $flag = 0)
{
$ch = curl_init();
if (!$flag) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
if (count($header) > 0) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch);
$err = curl_error($ch);
if ($err) {
//??????
}
curl_close($ch);
return $ret;
}
function curlGet($url, $header = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (count($header) > 0) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 1.5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$err = curl_error($ch);
if ($err) {
//做调试
}
curl_close($ch);
//-------请求为空
if (empty($response)) {
return null;
}
return $response;
}