shell并发测试

发布时间:2017-09-11 15:40:00 阅读:1137次

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;
}

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
上一篇:php socket
下一篇:curl cookie

转载请注明:shell并发测试 出自老鄢博客 | 欢迎分享