Locust压力测试使用

上次做接口压力测试前一直研究使用jmeter,本以为可以拿来使用了,但是真正进行并发接口时,发现jmeter在单机下并发1000个时,台式电脑单机资源早就被使用完,整个jmeter卡得死死的,结果那晚使用jmeter并发失败,幸好之前也准备了另外一个压测小公举Apache ab。最后使用ab勉强完成任务。估计jmeter压测接口研究得远远不够。后续继续研究吧。事后发现基于Python开发的Locust单机并发能力很理想,于是乎在测试环境拿来压测那次的接口。好像真的可以实现几千的并发。记录下方便自己下次使用:

Locust压测接口性能2

Locust是完全基于Python,http请求完全是基于requests库。Locust支持http、https协议,还支持测试其他协议,websocket等,只要采用Python调用对应的库就可以了。

http/https采用requests;

websocket采用websocket ;

等等。

Locust和jmeter、lr优点对比3

性能测试很难在单机上获得很高的并发量,lr、jmeter这类工具采用进程和线程来压测,单机并发量不高。(一般都要用几台电脑做压力发射机)

Locust并发机制摈弃了进程和线程,采用协程(gevent)机制。协程避免了系统级资源调度,可以大大提高单机并发能力。

Locust安装使用3

centos6.7使用Locust

Python安装Locust,从GitHub克隆下来,进入输入Python setup.py install  进行安装;

GitHub地址:https://github.com/locustio/locust

 

在安装之前最好先安装下面依赖包:

install_requires=["gevent>=1.2.2", "flask>=0.10.1", "requests>=2.9.1", "msgpack-python>=0.4.2", "six>=1.10.0", "pyzmq==15.2.0"]

 

  1. 1.gevent:在Python中实现协程的第三方库。协程又叫微线程Corouine。使用gevent可以获取极高的并发能力;
  2. 2.flask:Python的一个web开发框架,和django相当;
  3. 3.requests:支持http/https访问的库;
  4. 4.msgpack-python:一种快速、紧凑的二进制序列化格式,使用与类似json的数据;
  5. 5.six:提供了一些简单的工具封装Python2和Python3 之间的差异;
  6. 6.pyzmq:安装这个第三方库,可以把Locust运行在多个进程或多个机器(分布式执行测试任务)

 

在centos 6.7安装这些库(注意版本):使用pip安装就可以了;

最后在命令行中输入Python setup.py  install ,首先会检查以上的三方库有没有合法安装,最后安装成功;

验证安装:

命令航输入 locust  --help

 

 
  1. [root@localhost locust]# locust --help

  2. Usage: locust [options] [LocustClass [LocustClass2 ... ]]

  3.  
  4. Options:

  5. -h, --help show this help message and exit

  6. -H HOST, --host=HOST Host to load test in the following format:

  7. http://10.21.32.33 #设置运行的测试地址(被测试系统的域名或IP)

  8. --web-host=WEB_HOST Host to bind the web interface to. Defaults to '' (all

  9. interfaces)

  10. -P PORT, --port=PORT, --web-port=PORT

  11. Port on which to run web host

  12. -f LOCUSTFILE, --locustfile=LOCUSTFILE #指定性能测试脚本文件

  13. Python module file to import, e.g. '../other.py'.

  14. Default: locustfile

  15. --csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE

  16. Store current request stats to files in CSV format.

  17. --master Set locust to run in distributed mode with this

  18. process as master

  19. --slave Set locust to run in distributed mode with this

  20. process as slave

  21. --master-host=MASTER_HOST

  22. Host or IP address of locust master for distributed

  23. load testing. Only used when running with --slave.

  24. Defaults to 127.0.0.1.

  25. --master-port=MASTER_PORT

  26. The port to connect to that is used by the locust

  27. master for distributed load testing. Only used when

  28. running with --slave. Defaults to 5557. Note that

  29. slaves will also connect to the master node on this

  30. port + 1.

  31. --master-bind-host=MASTER_BIND_HOST

  32. Interfaces (hostname, ip) that locust master should

  33. bind to. Only used when running with --master.

  34. Defaults to * (all available interfaces).

  35. --master-bind-port=MASTER_BIND_PORT

  36. Port that locust master should bind to. Only used when

  37. running with --master. Defaults to 5557. Note that

  38. Locust will also use this port + 1, so by default the

  39. master node will bind to 5557 and 5558.

  40. --expect-slaves=EXPECT_SLAVES

  41. How many slaves master should expect to connect before

  42. starting the test (only when --no-web used).

  43. --no-web Disable the web interface, and instead start running

  44. the test immediately. Requires -c and -r to be

  45. specified. #表示不使用web界面运行测试,接口压力测试一般使用该些参数

  46. -c NUM_CLIENTS, --clients=NUM_CLIENTS

  47. Number of concurrent clients. Only used together with

  48. --no-web #设置虚拟用户数

  49. -r HATCH_RATE, --hatch-rate=HATCH_RATE #设置每秒启动的虚拟用户数

  50. The rate per second in which clients are spawned. Only

  51. used together with --no-web

  52. -n NUM_REQUESTS, --num-request=NUM_REQUESTS #设置请求个数

  53. Number of requests to perform. Only used together with

  54. --no-web

  55. -L LOGLEVEL, --loglevel=LOGLEVEL

  56. Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.

  57. Default is INFO.

  58. --logfile=LOGFILE Path to log file. If not set, log will go to

  59. stdout/stderr

  60. --print-stats Print stats in the console

  61. --only-summary Only print the summary stats

  62. --no-reset-stats Do not reset statistics once hatching has been

  63. completed

  64. -l, --list Show list of possible locust classes and exit

  65. --show-task-ratio print table of the locust classes' task execution

  66. ratio

  67. --show-task-ratio-json

  68. print json data of the locust classes' task execution

  69. ratio

  70. -V, --version show program's version number and exit

Locust脚本使用说明3

注意:增加打开文件限制的最大数量:

在每一个HTTP连接的机器上打开一个新文件(技术文件描述符)。操作系统可以设置一个可以打开的文件的最大数量的下限。如果限制小于模拟用户的数量,在测试时,会发生故障。增加操作系统的默认最大数量的文件限制到一个数字高于模拟用户数的数量,才能达到你想要的测试,在centos中在命令行中执行ulimit 655336,更改文件描述符最大就行不会报open too many file 的错误。

并发单个url的例子:

 

 
  1. from locust import HttpLocust,TaskSet,task

  2.  
  3. # 定义用户行为,继承TaskSet类,用于描述用户行为

  4. # (这个类下面放各种请求,请求是基于requests的,每个方法请求和requests差不多,请求参数、方法、响应对象和requests一样的使用,url这里写的是路径)

  5. # client.get===>requests.get

  6. # client.post===>requests.post

  7. class test_126(TaskSet):

  8. # task装饰该方法为一个事务方法的参数用于指定该行为的执行权重。参数越大,每次被虚拟用户执行概率越高,不设置默认是1,

  9. @task()

  10. def test_baidu(self):

  11. # 定义requests的请求头

  12. header = {"User-Agent": "Mozilla/5.0 "

  13. "(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 "

  14. "(KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"}

  15. # r是包含所有响应内容的一个对象

  16. r = self.client.get("/",timeout=30,headers=header)

  17. # 这里可以使用assert断言请求是否正确,也可以使用if判断

  18. assert r.status_code == 200

  19.  
  20. # 这个类类似设置性能测试,继承HttpLocust

  21. class websitUser(HttpLocust):

  22. # 指向一个上面定义的用户行为类

  23. task_set = test_126

  24. #执行事物之间用户等待时间的下界,单位毫秒,相当于lr中的think time

  25. min_wait = 3000

  26. max_wait = 6000

启动测试:
 

 

 

 
  1. [root @ localhost locust_baidu] # locust -f locustfile.py --host=http://www.126.com

  2.  
  3. [2017 - 06 - 08 12: 33:42, 662] localhost.localdomain / INFO / locust.main: Starting web monitor at *: 8089

  4. [2017 - 06 - 08 12: 33:42, 663] localhost.localdomain / INFO / locust.main: Starting Locust 0.8 a3

1.-f 指定性能测试文件;

 

2.--host 指定被测试url的主机地址(IP地址)
通过浏览器访问:http://192.168.0.107:8089/(默认端口是8089)

点击start swarming进行测试:

 

  • 1.Type:请求类型;
  • 2.Name:请求路径;
  • 3.requests:当前请求的数量;
  • 4.fails:当前请求失败的数量;
  • 5.Median:中间值,单位毫秒,一般服务器响应时间低于该值,而另一半高于该值;
  • 6.Average:所有请求的平均响应时间,毫秒;
  • 7.Min:请求的最小的服务器响应时间,毫秒;
  • 8.Max:请求的最大服务器响应时间,毫秒;
  • 9.Content Size:单个请求的大小,单位字节;
  • 10.reqs/sec:每秒钟请求的个数。

 

图表显示:

从上到下:

 

  • 1.吞吐量/每秒响应事务数(rps)实时统计
  • 2.平均响应时间/平均事务数实时统计
  • 3.虚拟用户数运行

 

另外,还有其他统计表格:失败请求、异常请求(断言失败结果显示在这里)、下载测试结果。

这个是通过浏览器测试的形式,可以通过测试场景设计的时间长度运行,压力测试时间(一般都是5分钟以上吧)足够后,需要手动点击stop按钮,结束压力测试。

如果希望请求数量维持在一定的情况,可以使用--no-web形式:

在shell中直接输入:

 

 
  1. [root@localhost locust_baidu]# locust -f locustfile.py --host=http://www.126.com --no-web -c 10 -r 10 -n 30

  2. [2017-06-08 13:13:31,385] localhost.localdomain/INFO/locust.main: Starting Locust 0.8a3

  3. [2017-06-08 13:13:31,385] localhost.localdomain/INFO/locust.runners: Hatching and swarming 10 clients at the rate 10 clients/s...

  4. Name

  5. # 测试过程的实时数据

  6. 。。。。。。

  7. #测试结束

  8. [2017-06-08 13:13:52,588] localhost.localdomain/INFO/locust.runners: All locusts dead

  9. #数据统计

  10. [2017-06-08 13:13:52,588] localhost.localdomain/INFO/locust.main: Shutting down (exit code 0), bye.

  11. Name # reqs # fails Avg Min Max | Median req/s

  12. --------------------------------------------------------------------------------------------------------------------------------------------

  13. GET / 39 0(0.00%) 305 28 5155 | 40 2.00

  14. --------------------------------------------------------------------------------------------------------------------------------------------

  15. Total 39 0(0.00%) 2.00

  16.  
  17. Percentage of the requests completed within given times

  18. Name # reqs 50% 66% 75% 80% 90% 95% 98% 99% 100%

  19. --------------------------------------------------------------------------------------------------------------------------------------------

  20. GET / 39 40 41 44 47 52 5100 5200 5200 5155

  21. --------------------------------------------------------------------------------------------------------------------------------------------

启动参数说明:

 

  • 1.--no-web:表示不使用web界面运行测试;
  • 2.-c:设置虚拟用户总数;
  • 3.-r:设置每秒启动虚拟用户数;
  • 4.-n:设置请求总个数;

 

测试环境压测接口脚本:

参数化数据:

readData.py

 

 
  1. # 思路:在userId.txt、type.txt文件中填写数据源,两个文件数据是一一对应的,通过readlines()读取,保存为一个列表,

  2. # 通过list的索引读取数据,索引使用随机数

  3. import random

  4. class Readdata():

  5. # 随机数放在init方法中,保证只获取一次随机数,因为类只实例化一次

  6. def __init__(self):

  7. with open("./userId.txt") as f:

  8. userId = f.readlines()

  9. # 随机数范围0--(数组长度-1)为了和list下标一样,从0开始

  10. self.ran = random.randint(0,len(userId)-1)

  11.  
  12. def readUserid(self):

  13. with open("./userId.txt") as f:

  14. userId = f.readlines()

  15. userIds = []

  16. # readlines获取每一行数据保存为list,每一行数据是一个元素,字符串形式,

  17. # 这里要遍历转为int可以去掉换行符号再append一个新数组。

  18. for i in userId:

  19. data = int(i)

  20. userIds.append(data)

  21. # 随机获取一个数

  22. userId = userIds[self.ran]

  23. return userId

  24. def readType(self):

  25. with open("./type.txt") as f1:

  26. type_ = f1.readlines()

  27. # 去掉list中的换行符号\n

  28. type1 = ''.join(type_).strip('\n')

  29. # 分割字符串,保存为list,

  30. type2 = type1.split(',')

  31. type3 = type2[self.ran]

  32. # print(type(type2))

  33. return type3

  34. if __name__ == "__main__":

  35. rd = Readdata()

  36. print(rd.readType())

  37. print(rd.readUserid())

 

请求公参:

common.py

 

 
  1. import sys,time,random,hashlib

  2. sys.path.append('../db_set')

  3. # from db_set.mysql_db import DB

  4. def setUp_():

  5. tim = time.time()

  6. tim = tim*1000

  7. tim = str(tim)

  8. # ts时间戳

  9. ts = tim.split('.')[0]

  10. ran = random.randint(100,999)

  11. ran = str(ran)

  12. # reqId时间戳拼接随机数

  13. reqId =ts + ran

  14. # 密匙

  15. secret = 'xxxxxxxxxxx'

  16. #请求头

  17. header = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"}

  18. #数据库

  19. # db = DB()

  20. #测试userId

  21. userId = "100000001000153"

  22. #测试liveId

  23. liveId = "140000002000038"

  24. #测试地址

  25. host = "http://ixxxxxxxhat.com/"

  26. return (ts,reqId,secret,header,userId,liveId,host)

  27. def md5(reqSign_):

  28. md5 = hashlib.md5()

  29. str_bytes_utf8 = reqSign_.encode(encoding="utf-8")

  30. md5.update(str_bytes_utf8)

  31. sign = md5.hexdigest()

  32. return sign

locust文件:

 

 
  1. from locust import HttpLocust,TaskSet,task

  2. from common import setUp_,md5

  3. from readData import Readdata # 导入参数化的类

  4.  
  5. # 定义用户行为

  6. class SpeakAdd(TaskSet):

  7. # 定义类的实例变量

  8. ts = setUp_()[0]

  9. reqId = setUp_()[1]

  10. secret = setUp_()[2]

  11. header = setUp_()[3]

  12. # db = setUp_()[4]

  13. # userId = setUp_()[4]

  14. # liveId = setUp_()[5]

  15. # 获取参数化数据

  16. rd = Readdata()

  17. # 发言类型type,因为不同用户这个是不同的

  18. type = rd.readType()

  19. # 获取发言人id,这里和发言类型对应,上面做了处理

  20. userId = rd.readUserid()

  21. #加密

  22. reqSign = reqId + ':' + secret + ':' + ts

  23. sign = md5(reqSign)

  24. # 请求参数

  25. data = { "id": reqId,

  26. "timestamp": ts,

  27. "sign": sign,

  28. "data": { "commentId":"",

  29. "topicId": "100000046000082",

  30. "type": type,

  31. "liveId": "140000002000038",

  32. "content": "我正在发言," + ts,

  33. "isReplay": "N",

  34. "page": {"size": "20", "page": "1"},

  35. "userId": userId}}

  36. @task(1)

  37. def testSpeakadd(self):

  38. # 和requests请求一样写

  39. r = self.client.post("/h5/speak/add",json=self.data,headers=self.header,timeout=30)

  40. result = r.json()# 返回字典

  41. # 断言

  42. assert r.status_code == 200

  43. assert result['state']['code'] == 0

  44.  
  45.  
  46. # 设置性能测试

  47. class WebsiteUser(HttpLocust):

  48. task_set = SpeakAdd

  49. # 接口测试think time 设置为0

  50. min_wait = 0

  51. max_wait = 0

  52.  
  53. # 下面可以通过直接运行Python locustfile1.py c r n 进行测试,这种方式适合 --no-web运行

  54. if __name__ == "__main__":

  55. import os,sys

  56. # sys.argv是一个list,元素是用户自定义的,原来是str类型,需要转换为int

  57. c = int(sys.argv[1])

  58. r = int(sys.argv[2])

  59. t = int(sys.argv[3])

  60. for i in range(2):

  61. # os.system(命令),可以运行shell、Windows命令

  62. os.system("locust -f locustfile1.py --host=http://xxxxxt2.qlchat.com --no-web -c %d -r %d -n %d" %(c,r,t))


 

 

测试方案一:

启动接口压力测试:

 

locust  -f locustfile.py  --host=http://inner.test2.qlchat.com

浏览器打开:
http://192.168.0.107:8089/

根据场景进行压力测试:

场景1:运行5分钟,用户数1000,每秒启动100,记录测试结果数据,服务器性能指标;

场景2:运行5分钟,用户数1000,每秒启动200,记录测试结果数据,服务器性能指标;

场景3:运行5分钟,用户数1000,每秒启动500,记录测试结果数据,服务器性能指标;

服务器指标:CPU、内存等

性能测试分析:

。。。。。。

测试方案二:

使用另外一种测试方式,--no-web、-c、-r、-n:

命令行中执行测试:每种场景记录测试结果数据,服务器性能指标;

场景一:locust -f locustfile.py --host=http://inner.test2.qlchat.com --no-web -c 1000 -r 100 -n 30000

场景二:locust -f locustfile.py --host=http://inner.test2.qlchat.com --no-web -c 1000 -r  500 -n 30000

场景三:locust -f locustfile.py --host=http://inner.test2.qlchat.com --no-web -c 1000 -r 1000 -n 30000

总结:

不足之处:发言人id需要不不是同一个,也准备不同的发言人,参数化。使场景更加接近用户使用场景。但是并发时还是同一个用户 ==,伤心。。。有高手看到麻烦指导下。。。

 

多个url压力测试也就是业务性能测试3:

。。。。

。。。

。。

思路:

在一个完整的业务场景中(从用户登录到退出系统),点击打开的一个页面到另一个页面,每个页面都是由url定义的,可以先写请求登录的方法,在写另一个页面的请求方法,最后把所有请求方法连接起来,就是一个完整的业务场景了。然后做登录关联、用户参数化。设置虚拟用户、每秒启动虚拟用户个数,设计场景去并发。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值