模拟用户参加活动脚本

因为各个活动没有数据做支持,一个一个试又比较麻烦,所以有些活动,我使用了一些数据来进行模拟,这样可以比较快的模拟多用户参加活动。

这里可以使用crontab写定时任务,但是我使用的是虚拟机,不是一直开机状态,不能保证脚本一定执行,所以我每次是直接在虚拟机上执行脚本

//执行脚本增加一个用户,模拟参加盲盒交友活动
/usr/bin/php /var/www/html/iblog/index.php job/Job/job

//虚拟用户参加骰子活动
/usr/bin/php /var/www/html/iblog/index.php job/Job/diceJob
    
//虚拟用户参加开启盲盒活动
/usr/bin/php /var/www/html/iblog/index.php job/Job/blindJob
job脚本

执行该脚本增加一个用户,模拟参加盲盒交友活动,模拟用户浏览文章

//虚拟数据脚本
    //* * * * * /usr/bin/php /var/www/html/iblog/index.php job/Job/job
    public function job()
    {
        $user = [
            'username' => getRandomStr(rand(3, 10)),
            'password' => encrypt('123'),
            'phone' => getRandomMobile(),
            'nickname' => getChar(rand(3, 10)),
            'sign' => getChar(rand(5, 10)),
            'email' => getRandomStr(rand(3, 10)) . '@email.com',
            'true_name' => getChar(rand(2, 4)),
            'create_time' => time(),
        ];
        //新增用户
        $this->Job_model->commonInsert('users', $user);

        //模拟用户参加活动
        //查看用户总数
        $res = $this->Users_model->commonQuery('count(id) count',[],'users');
        $count = (int)$res[0]['count'];
        for ($i = 1; $i <= $count; $i++) {
            //放入纸条
            $data1 = [
                'uid' => rand(2, 10),
                'code' => getRandomStr(4, 8),
                'type' => rand(1, 2),
                'content' => getChar(5, 10)
            ];
            curl_request("http://10.0.0.200/iblog/index.php/Activity/sendBlindBoxNote", true, $data1, false);
            //抽取纸条
            $data2 = [
                'uid' => rand(2, 10),
                'type' => rand(1, 2),
            ];
            curl_request("http://10.0.0.200/iblog/index.php/Activity/getBlindBoxNote", true, $data2, false);

            //访问文章
            $url1 = "http://10.0.0.200/iblog/index.php/article/" . rand(1, 7);
            curl_request($url1, false, '', false);
        }
    }

这里面使用了一些生成随机数据的函数定义在自定义的公共函数中

//生成随机中文字符
if (!function_exists('getChar')){
    function getChar($num)  // $num为生成汉字的数量
    {
        $b = '';
        for ($i=0; $i<$num; $i++) {
            // 使用chr()函数拼接双字节汉字,前一个chr()为高位字节,后一个为低位字节
            $a = chr(mt_rand(0xB0,0xD0)).chr(mt_rand(0xA1, 0xF0));
            // 转码
            $b .= iconv('GB2312', 'UTF-8', $a);
        }
        return $b;
    }
}

//随机生成n条手机号
if (!function_exists('getRandomMobile')) {
    function getRandomMobile($num = 1)
    {
        //前三位
        $tel_arr = array(
            '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '144', '147', '150', '151', '152', '153', '155', '156', '157', '158', '159', '176', '177', '178', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189',
        );
        if ($num == 1){
            $phone = $tel_arr[array_rand($tel_arr)] . mt_rand(1000, 9999) . mt_rand(1000, 9999);
            return $phone;
        }else{
            for ($i = 0; $i < $num; $i++) {
                //随机前三位加上两个随机的1000-9999
                $phone[] = $tel_arr[array_rand($tel_arr)] . mt_rand(1000, 9999) . mt_rand(1000, 9999);
            }
            //去除重复的号码
            return array_unique($phone);
        }
    }
}

/**
 * 获得随机字符串
 * @param $len             需要的长度
 * @param $special        是否需要特殊符号
 * @return string       返回随机字符串
 */
if (!function_exists('getRandomStr')) {
    function getRandomStr($len, $special = false)
    {
        $chars = array(
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );

        if ($special) {
            $chars = array_merge($chars, array(
                "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
                "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
                "}", "<", ">", "~", "+", "=", ",", "."
            ));
        }

        $charsLen = count($chars) - 1;
        shuffle($chars);                            //打乱数组顺序
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $charsLen)];    //随机取出一位
        }
        return $str;
    }
}
diceJob脚本

模拟用户参加骰子活动

 /*
     * 虚拟参加掷骰子活动
     * /usr/bin/php /var/www/html/iblog/index.php job/Job/diceJob
     */
    public function diceJob()
    {
        for ($j=0;$j<rand(10,20);$j++){
            //获取随机用户
            $res = $this->Users_model->getUserCount();
            $count = $res[0]['count'];
            $userId = rand(1,$count);
            //给该用户增加骰子
            for ($i=0;$i<rand(1,10);$i++){
                $this->Activity_service->addDice($userId,getRandomStr(5));
            }
            //掷骰子
            $data= [
                'uid' => $userId
            ];
            $url = "http://10.0.0.200/iblog/index.php/Activity/rollDice";
            $res = curl_request($url,true,$data,false);
        }
    }
blindJob脚本

模拟用户参加开启盲盒活动

 /*
     * 模拟参加开启盲盒活动
     */
    public function blindJob()
    {
        for ($j=0;$j<rand(10,20);$j++){
            //获取随机用户
            $res = $this->Users_model->getUserCount();
            $count = $res[0]['count'];
            $userId = rand(1,$count);
            //给该用户增加盲盒
            $num = rand(1,10);
            $this->Activity_service->getBlindBox($userId,$num);
            //开启盲盒
            $data = [
                'uid' => $userId,
                'num' => $num
            ];
            $url = "http://10.0.0.200/iblog/index.php/Activity/openBlindBox";
            $res = curl_request($url,true,$data,false);
        }
    }

执行后的就有了一定的数据

在这里插入图片描述

在这里插入图片描述

有了数据就可以写一些关于统计的脚本,来统计用户参加活动情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值