一、Locust工具介绍

1.概述

Locust是一款易于使用的分布式负载测试工具,完全基于事件,使用python开发,即一个locust节点也可以在一个进程中支持数千并发用户,不使用回调,通过gevent使用轻量级过程(即在自己的进程内运行)。

2.常见性能测试工具比较

3.环境搭建

源码安装:下载源码https://github.com/locustio/locust,进入文件夹执行安装命令:python setup.py install

pip安装:pip install locust

二、Locust常用类和方法

三、Locust常用参

四、案例

1.参数化

# -*- coding: utf-8 -*-
import os,random
from locust import TaskSet, task,HttpUser, between #任务类
class Testlocust(TaskSet):
def on_start(self):
self.login_headers={'x-client-id': 'xxxxxx'} #头文件
self.data=[
{
"account_name": "登录账号1",
"user_name": "登录账号1",
"hashed_password": "登录密码1"
},
{
"account_name": "登录账号2",
"user_name": "登录账号2",
"hashed_password": "登录密码2"
},
{
"account_name": "登录账号3",
"user_name": "登录账号3",
"hashed_password": "登录密码3"
}
] #登录账号密码
print("------on start------") @task()
def userlogin(self):
r = self.client.post(url='/v1/auth/users/login', headers=self.login_headers,
json=random.choice(self.data),name='登录',verify=False) #使用choice方法参数化随机登录
assert r.status_code == 200 and r.json()['status']==0 #登录断言 def on_stop(self):
print("------on stop------") #用户类
class WebsiteUser(HttpUser): #locust1.0版本以前是HttpLocust
tasks = [Testlocust] #locust1.0版本以前是task_set=Testlocust
host = 'https://xxxxxx' #被测主机地址
wait_time = between(min_wait=1,max_wait=5) #任务之间的等待时间 if __name__ == "__main__":
os.system("locust -f locust_XX.py") #执行locust脚本

2.关联

# -*- coding: utf-8 -*-
import os,requests
from locust import TaskSet, task,HttpUser, between #任务类
class Testlocust(TaskSet):
def on_start(self):
print("------on start------")
access_token = self.userlogin() #返回登录token
self.headers = {} #定义headers
self.headers['x-api-key'] = 'fZkQSHC1dp2s0tL21EMtaNX3UjF7P6L9' #添加headers值
self.headers['Authorization'] = 'Bearer ' + access_token
self.headers['x-key-hash'] = '1607675936446;abcdefg;bca1ef2b5835e454a15929f7ce9cb5d7ebaf580377624019002'
self.headers['Content-Type'] = 'application/json'
self.params = {"name": "CDR", "page": "1", "size": "10", "series": "CDR80"} #查询接口参数 def userlogin(self): #登录方法
login_url = 'https://xxxxxx/v1/auth/users/login' #登录url
data = {
"account_name": "账号1",
"user_name": "账号1",
"hashed_password": "密码1"
} #登录账号密码
login_headers = {'x-client-id': '46bf882df2959ea2'}
r = requests.request('POST', url=login_url, headers=login_headers, json=data) #登录
return r.json()['access_token'] #返回登录token @task()
def search(self): #查询设备信息
r = self.client.get(url='/v1/assets/device/search', headers=self.headers,
params=self.params, name='查询', verify=False) #查询
assert r.status_code == 200 and r.json()['code'] == 0 #结果断言 #用户类
class WebsiteUser(HttpUser): #locust1.0版本以前是HttpLocust
tasks = [Testlocust] #locust1.0版本以前是task_set=Testlocust
host = 'https://xxxxxx.com' #被测主机地址
wait_time = between(min_wait=1,max_wait=5) #任务之间的等待时间 if __name__ == "__main__":
os.system("locust -f locust_XX.py") #执行参数

Python3.7.9+Locust1.4.3版本性能测试工具案例分享的更多相关文章

  1. windows10(64位)Anaconda3+Python3.6搭建Tensorflow(cpu版本)及keras

    转自:windows10(64位)Anaconda3+Python3.6搭建Tensorflow(cpu版本)及keras 1.本来电脑安装的是anaconda3 5.3.1,但安装的python版本 ...

  2. [linux]centos7.4上升级python2版本到python3.6.5 【安装双版本,默认python3】

    版本声明 centos7.4 前言:linux上的python默认是版本2的,之前学django项目用的是3的版本 所以得升级下版本~ 1.下载python3.6.5 cd /usr/local/ w ...

  3. 性能测试工具Locust的使用

    一.写在前面 官网:https://www.locust.io/ 官方使用文档:https://docs.locust.io/en/latest/ 大并发量测试时,建议在linux系统下进行. 二.L ...

  4. 性能测试工具Locust的介绍和使用

    内容来自网络 https://www.w3xue.com/exp/article/20191/16707.html https://blog.csdn.net/qq_36255988/article/ ...

  5. Locust 性能测试工具安装使用说明

    1. 介绍     它是一个开源性能测试工具.使用 Python 代码来定义用户行为.用它可以模拟百万计的并发用户访问你的系统. 性能工具对比 LoadRunner 是非常有名的商业性能测试工具,功能 ...

  6. 性能测试工具 wrk 安装与使用

    介绍 今天给大家介绍一款开源的性能测试工具 wrk,简单易用,没有Load Runner那么复杂,他和 apache benchmark(ab)同属于性能测试工具,但是比 ab 功能更加强大,并且可以 ...

  7. 性能测试工具Locust

    An open source load testing tool. 一个开源性能测试工具. define user behaviour with python code, and swarm your ...

  8. 给CentOS6.3 + PHP5.3 安装PHP性能测试工具 XHProf-0.9.2

    一.什么是XHProf XHProf官网:http://pecl.php.net/package/xhprof XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括 阻塞 ...

  9. Ceph性能测试工具和方法。

    0. 测试环境 同 Ceph 的基本操作和常见故障排除方法 一文中的测试环境. 1. 测试准备 1.1 磁盘读写性能 1.1.1 单个 OSD 磁盘写性能,大概 165MB/s. root@ceph1 ...

随机推荐

  1. explain extended;show warnings

    mysql> explain extended select count(*) from xuehao;+----+-------------+-------+------+---------- ...

  2. 【Oracle】11G 11.2.0.4 RAC环境打补丁

    一.准备工作 1,数据库环境 操作系统版本  : RedHat 7.2 x64   数据库版本    : Oracle 11.2.0.4 x64 RAC    Grid          : 11.2 ...

  3. CTFshow萌新赛-千字文

    打开靶机 下载完成后,为一张二维码图片 使用StegSolve 解出隐写图像 保存后使用PS或其他工具去除白边 然后使用脚本分割这个图像(25*25) from PIL import Image im ...

  4. 小试牛刀ElasticSearch大数据聚合统计

    ElasticSearch相信有不少朋友都了解,即使没有了解过它那相信对ELK也有所认识E即是ElasticSearch.ElasticSearch最开始更多用于检索,作为一搜索的集群产品简单易用绝对 ...

  5. Spring Initializr中生成的mvnw是干吗的?

    当我们使用Spring Initializr来创建Spring Boot工程的时候,有没有发现在工程根目录下有两个名为mvnw的文件: 从命名.图标.扩展名来猜测,这两个文件的作用应该是一样的,只是c ...

  6. 1V升压5V和1.5V升压5V的集成电路芯片

    1.5V和1V输入,要升压输出5V的集成电路芯片合适? 干电池标准电压是1.5V,放电电压后面在0.9V-1V左右,如果要选用干电池1.5V升压到5V的合适的芯片,需要满足低压1V或者0.9V更好的低 ...

  7. celery应用

    celery---分布式任务队列 Celery是一个简单,灵活且可靠的分布式系统,可以处理大量消息,同时为操作提供维护该系统所需的工具. Celery是一个基于python开发的模块,可以帮助我们对任 ...

  8. 大数据谢列3:Hdfs的HA实现

    在之前的文章:大数据系列:一文初识Hdfs , 大数据系列2:Hdfs的读写操作 中Hdfs的组成.读写有简单的介绍. 在里面介绍Secondary NameNode和Hdfs读写的流程. 并且在文章 ...

  9. windows 系统 MySQL_5.6.21安装教程

      1.双击安装文件 mysql_installer_community_V5.6.21.1_setup.1418020972.msi,等待安装界面出现,见下图: 2.勾选:I accept thel ...

  10. LoadRunner监控Centos和Ubuntu资源之服务器配置

    Centos 我用的版本是Centos6.8   首先更新源以及基础操作我就不说了,直接上步骤: Step 1 安装相关程序 执行命令:yum install inetd,这一步是为了安装rstatd ...