python_locust_实现100并发打开百度

官网:https://docs.locust.io/en/stable/

第一步、创建虚拟环境:
(关于python虚拟环境的另外一些总结:https://blog.csdn.net/weixin_45451320/article/details/115681874)
在这里插入图片描述
第二步、pycharm进入terminal窗口,执行命令:(其中docker的话执行activate.bat文件)
(venv) D:\huicelocust\venv>pip install locust【安装locust库】
(venv) D:\huicelocust\venv>locust -V【查看版本】

在这里插入图片描述
在这里插入图片描述
第三步、创建目录,输入以下代码,执行命令:(venv) D:\huicelocust\huice>locust
在这里插入图片描述

from locust import User, task
class Login(User):
    host = "http://localhost:8089/"
    @task
    def openindex(self):
        pass
注解:
from locust import User,task
# 性能测试核心:用户、干什么
# 用户
class Login(User): 
    # 压测地址
    host = "http://localhost:8089/"
    # 干什么
    @task 
    def openindex(self):
        pass

第四步、打开网址:http://localhost:8089/
在这里插入图片描述
这是输入的5,就相当于你模拟了5个用户,例如:
在这里插入图片描述
第五步、实例打开百度网页

from locust import HttpUser, task, between

# from locust.contrib.fasthttp import FastHttpUser
# class MyUser(FastHttpUser):

class Login(HttpUser): # httpuser 是基于http在user基础上封装而来的,比user更好用点,具体参考产品文档源码
    host = "http://121.36.91.190"
    wait_time = between(0.5, 3) # 在0.5和3秒之间,执行下一个
    # wait_time = constant(5) # 5秒内,执行下一个
    # wait_time = constant_pacing(6) # 每6秒,执行下一个
    @task
    def openindex(self):
        self.client.request('GET', url='https://www.baidu.com/?tn=62095104_19_oem_dg', name="打开首页")

在这里插入图片描述

其他:

第一、locust解决了什么?
locust:解决了web的接口性能的压测问题。

第二、接口测试分为?
1、app+接口:(app:客户端,接口:服务端)
2、web+接口:(web:客户端_浏览器,接口:服务器端)
3、纯接口

第三、我们平时说的性能指:大多数是针对后端服务器的压测

第四、压测工具locust、jmeter区别?
loadrunner、jmeter基于线程
locust基于协程
进程、线程、协程、cpu关系是什么?
(1)进程就是:windows右键打开任务管理器里的进程,进程是具有一定独立功能的程序。如QQ、微信、pycharm
在这里插入图片描述
(2)线程:进程下面分了好多线程。微信在上图显示一个进程,你同时打开多个微信聊天窗口,就是多个线程。所以:进程用来管理线程的。线程是CPU(中央处理器)调度和分派的基本单位,jmeter、loadrunner是直接调用线程处理的。
(3)协程:一个线程可以有0到多个协程,本质是一个线程时间分片去执行协程代码段。所以:执行效率非常高。因为子程序切换不是线程切换,而是由程序自身控制,线程越多,协程越优势,locust用的利用协程处理的。

猜你喜欢

转载自blog.csdn.net/weixin_45451320/article/details/117263174
今日推荐