当前位置:网站首页>使用ApacheBench来对美多商城的秒杀功能进行高并发压力测试
使用ApacheBench来对美多商城的秒杀功能进行高并发压力测试
2022-08-08 10:27:00 【用户9127725】
秒杀功能众所周知,低廉的价格会引来很多用户趋之若鹜的争抢点击,导致一系列的服务器负载问题,服务器负载太大而影响程序效率也是很常见的,Apache服务器自带有一个叫AB(ApacheBench)的工具,可以对服务器进行负载测试
同时美多商城的秒杀功能也会被高负载影响,从而导致超卖现象
安装xampp软件
进入 c:/xampp/apache/bin
基本用法: ab -n 全部请求数 -c 并发数测试url 可以将ab.exe 加入系统环境变量;或直接切换置 ab 目录执行。如: C:WindowsSystem32> cd C:xamppapachebin
关于秒杀很好理解,就是每一个用户抢到商品之后,库存进行递减操作
#定义秒杀接口
def miaosha(request):
res_one = News.objects.get(pk=1)
if res_one.pd > 0:
time.sleep(5)
with connection.cursor() as c:
c.execute(' update news set pd = pd - 1 where id = 1 ')
return HttpResponse('ok')
else:
return HttpResponse('没有了')索然逻辑上很严谨,代码也很简单,但是在高并发没有锁的情况下,数据库会过载导致超卖现象,也就是库存变为负数
于是就得引入redis来解决这一个问题:
r = redis.Redis(host='localhost', port=6379)
#定义过载限制
def limit_handler():
"""
return True: 允许; False: 拒绝
"""
amount_limit = 3 # 限制数量
keyname = 'limit' # redis key name
incr_amount = 1 # 每次增加数量
# 判断key是否存在
if not r.exists(keyname):
# 为了方便测试,这里设置默认初始值为95
# setnx可以防止并发时多次设置key
r.setnx(keyname, 0)
# 数据插入后再判断是否大于限制数
if r.incrby(keyname, incr_amount) <= amount_limit:
return True
return False
#定义秒杀接口
def miaosha(request):
res_one = News.objects.get(pk=1)
if limit_handler():
#if res_one.pd > 0:
time.sleep(5)
with connection.cursor() as c:
c.execute(' update news set pd = pd - 1 where id = 1 ')
return HttpResponse('ok')
else:
return HttpResponse('没有了')这样只要配合这个方法,在进行修改mysql数据库的操作,就可以防止超限
边栏推荐
- 面试突击72:输入URL之后会执行什么流程?
- String equals hashcode
- MongoDB是什么,怎么用?
- Flutter Game Tutorial Recreate the famous T-Rex game with Flutter and Flame
- 深度强化学习发展史
- "Inversion of Control" and "Dependency Inversion", can't you tell the difference?
- Loadrunner12.0.2 installation and Chinese language pack installation (Chinese)
- Redis 定长队列的探索和实践
- 苹果开发者账号申请流程完整版
- vs2019+boost library (boost_1_67_0) installation
猜你喜欢
随机推荐
Loadrunner的录制event为0的问题解决方法与思路
d实验新异常
Using classification weights, it is easy to solve the problem of data imbalance
Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
文档数据库是怎么定位一个文档的呀?
2022世界机器人大会即将举办,智能机器人助推传统行业向智能化、数字化转型升级
ReentrantLock原理,ReentrantLock和synchronized区别
快速定位线上慢 SQL 问题,掌握这几个性能排查工具可助你一臂之力
In the.net core, the use of c # realize fastdfs batch file upload more
Tensorflow basic concepts
A concise tutorial on expanding (increasing capacity) of VMWare Esxi virtual system data storage
机器学习模型太慢?来看看英特尔(R) 扩展加速
English token preprocessing, used to process English sentences into words
关于振弦采集模块及采集仪振弦频率值准确率的问题
Vulnhub靶机:GEMINI INC_ 1
有哪些典型的列存储数据库呢?
简单混合运算计算器
Dubins curve study notes and related thinking
Redis是持久化键值数据库嘛?
idea installation steps









