linux ab命令 | php 模拟并发现象

19 篇文章 0 订阅

ab命令

# 总请求数300   并发100  脚本地址 http://test.com/test.php
ab -r -n 300 -c 100 http://test.com/test.php  

php 模拟脚本

文件名test.php

# 环境yii2.0 框架  
<?php
$connection  = \Yii::$app->db;
$sql     = "SELECT flag from test where id=1";
$command = $connection->createCommand($sql);
$res     = $command->queryOne();
if ($res['flag'] >= 10) {
    exit;  # 理论上flag最终值为10
} else {
	// 我的实验现象——无sleep:flag最终值10  sleep 1秒:最终值为11或者12
	// sleep的作用是模拟代码执行时间长的情况下的并发现象
    sleep(1);     
    $connection  = \Yii::$app->db;
    $sql     = "SELECT flag from test where id=1";
    $command = $connection->createCommand($sql);
    $res     = $command->queryOne();
    $flag = $res['flag']+1;
    $sql     = "update test set flag = ".$flag." where id=1";
    $command = $connection->createCommand($sql);
    $res     = $command->queryOne();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值