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