一、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. 更改mysql的密码

    mysql> set password for 'root'@'localhost' =PASSWORD('');Query OK, 0 rows affected (0.17 sec) mys ...

  2. 【Linux】同时插入多行数据到文本文件中

    如果想同时插入多行数据到指定的文本中,可以用下面的命令EOF cat >> test.txt <<EOF 1234 5678 GOOD EOF 将上述3行插入到 test.tx ...

  3. 【ORA】ORA-39002,ORA-39070,ORA-29283, ORA-06512,ORA-29283解决办法

    今天使用IMPDP导入的时候报了一个错误 ORA-39002: invalid operation  ORA-39070: Unable to open the log file.  ORA-2928 ...

  4. oracle 12C单实例打PSU

    前提: oracle不管打什么样的补丁,readme都是很好的参考资料. Oracle每季度都会更新一个最新的PSU,现在12.1.0.2.0的最新的PSU是Patch 26925311. 由于今天白 ...

  5. SAP表的锁定与解锁

    表的锁定模式有三种模式. lock mode有三种模式:分别是S,E,X.含义如下:     S (Shared lock, read lock)     E (Exclusive lock, wri ...

  6. jQ实现图片无缝轮播

    在铺页面的过程中,总是会遇到轮播图需要处理,一般我是会用swiper来制作,但总会有哪个几个个例需要我自己来写功能,这里制作了一个jq用来实现图片无缝轮播的dome,分享给大家ヽ( ̄▽ ̄)ノ. dom ...

  7. Linux top命令里面%CPU和cpu(s)的差别

    有的同学会把%CPU和us%搞晕,也就是下图所示在top的时候查看cpu的信息. 这时有的同学会问:这两个CPU到底哪个是对的. 其实都是对的,只是表达的意思不一样. 官方解释如下 Cpu(s):34 ...

  8. jQuery mock.js模拟的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. Go for Pythonistas Go and the Zen of Python 禅

    Go for Pythonistas https://talks.golang.org/2013/go4python.slide#1 Things I don't like about Python ...

  10. 关于js中each()使用return不能终止循环

    Jquery的each里面用return false代替break:return ture代替continue $(xx).each(function() { if(xx){ return false ...