当前位置: 首页 > news >正文

长沙培训网站制作seo关键词找29火星软件

长沙培训网站制作,seo关键词找29火星软件,linux做商务网站,做玻璃瓶的网站一、背景 这还是2个月前做的一次接口性能测试,关于locust脚本的单机多核运行,以及主从节点之间的数据通信。 先简单交代下背景,在APP上线之前,需要对登录接口进行性能测试。经过评估,我还是优先选择了locust来进行脚…

一、背景

这还是2个月前做的一次接口性能测试,关于locust脚本的单机多核运行,以及主从节点之间的数据通信。

先简单交代下背景,在APP上线之前,需要对登录接口进行性能测试。经过评估,我还是优先选择了locust来进行脚本开发,本次用到了locust的单机多核运行能力,只不过这里还涉及到主从节点之间数据通信。现成的可参考的有效文档甚少,所以还是自己摸着官方文档过河比较靠谱。

顺带提一下,学习框架这种东西最好的教程其实还得是官方文档以及框架源码了,这里贴上locust官方文档链接,需要的可以自行学习:https://docs.locust.io/en/stable/what-is-locust.html

二、代码编写

其实脚本代码的编写一大重点就是如何处理测试数据,不同的测试需求对于测试数据的处理是不同的。比如这次的需求,手机号不能重复。另外考虑到长时间的负载压力,数据量还得足够。

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036【暗号:csdn999】

最后测试数据还需要处理,那么我使用的测试号段是非真实号码段,测试结束后可以查询对应号段内的手机号,进行相关业务数据的清理。

1. 代码概览

还是老样子,先附上全部代码,然后对其结构进行拆分讲解。

import random
import time
from collections import dequefrom locust import HttpUser, task, run_single_user, TaskSet, events
from locust.runners import WorkerRunner, MasterRunnerCURRENT_TIMESTAMP = str(round(time.time() * 1000))
RANDOM = str(random.randint(10000000, 99999999))
MOBILE_HEADER = {"skip-request-expired": "true","skip-auth": "true","skip-sign": "true","os": "IOS","device-id": "198EA6A4677649018708B400F3DF69FB","nonce": RANDOM,"sign": "12333","version": "1.2.0","timestamp": CURRENT_TIMESTAMP,"Content-Type": "application/json"
}last_mobile = ""
worker_mobile_deque = deque()# 13300120000, 13300160000 新用户注册号段@events.test_start.add_listener
def on_test_start(environment, **_kwargs):if not isinstance(environment.runner, WorkerRunner):mobile_list = []for i in range(13300120000, 13300160000):mobile_list.append(i)mobile_list_length = len(mobile_list)print("列表已生成,总计数量:", mobile_list_length)worker_count = environment.runner.worker_countchunk_size = int(mobile_list_length / worker_count)print(f"平均每个worker分得的手机号数量:{chunk_size}")for i, worker in enumerate(environment.runner.clients):start_index = i * chunk_sizeif i + 1 < worker_count:end_index = start_index + chunk_sizeelse:end_index = len(mobile_list)data = mobile_list[start_index:end_index]environment.runner.send_message("mobile_list", data, worker)def setup_mobile_list(environment, msg, **kwargs):len_msg_data = len(msg.data)print(f"worker收到的master传来的数据号段:{msg.data[0]} ~ {msg.data[len_msg_data-1]}")global worker_mobile_dequeworker_mobile_deque = deque(msg.data)@events.init.add_listener
def on_locust_init(environment, **_kwargs):if not isinstance(environment.runner, MasterRunner):environment.runner.register_message('mobile_list', setup_mobile_list)class VcodeLoginUser(TaskSet):# wait_time = between(5, 5)@taskdef vcode_login(self):test_mobile = worker_mobile_deque.popleft()print("当前获取的手机号:", test_mobile)# print("当前队列大小:", len(worker_mobile_deque))global last_mobilelast_mobile = test_mobilewith self.client.post("/g/sendMobileVcode",headers=MOBILE_HEADER,json={"busiType": "login", "mobile": str(test_mobile)}) as send_response:try:send_response_json = send_response.json()if send_response_json["message"] == "success":params = {"mobile": str(test_mobile), "vcode": "111111"}# print(test_mobile, "登录请求参数:", params)with self.client.post("/g/vcodeLogin",json=params,headers=MOBILE_HEADER,catch_response=True) as login_response:# print(login_response.json)login_response_json = login_response.json()if login_response_json["message"] != "success":login_response.failure("message not equal success")elif login_response_json["code"] != 0:login_response.failure("code not equal 0")elif login_response_json["data"]["rId"] == "":login_response.failure("rid is null")elif login_response_json["data"]["mobile"] != str(test_mobile):login_response.failure("mobile is error,入参手机号{},返回的手机号{}".format(test_mobile, login_response.json()["data"]["mobile"]))# print(test_mobile, "请求结果:", login_response.json())else:send_response.failure("{} send code fail".format(test_mobile))except Exception as e:send_response.failure("send code fail {}".format(e))@events.test_stop.add_listenerdef on_test_stop(environment, **kwargs):print("脚本结束")print("当前队列大小:", len(worker_mobile_deque))print("最后的手机号:", last_mobile)class LocustLogin(HttpUser):tasks = [VcodeLoginUser]host = "https://qa.test.com"if __name__ == '__main__':run_single_user(LocustLogin)

2. 代码拆解-要加必要的断言

首先是基于locust开发的http请求的脚本大结构是不变的,依旧是两大块:HttpUserTaskSet,这里不再对其讲解了,大伙看下官方文档就明白了。

接下来就是类VcodeLoginUser,可以看到在这里面是定义了单个用户的详细动作。注意这里要加上必要的断言。否则仅靠框架的非200外的错误断言还是不够的。

比如我这里关注登录成功后的几个必要字段:coderIdmobile,这些一定是要符合断言的才可以。

果不其然,压测过程中就发现了并发情况下会出现的问题:入参手机号是a,接口返回的手机号是b。并发量越大错误越多。如果我只断言code=0,那么这个问题就不容易发现了,虽然接口返回的code都是成功的,但是业务上已经存在错误了。

...with self.client.post("/g/sendMobileVcode",headers=MOBILE_HEADER,json={"busiType": "login", "mobile": str(test_mobile)}) as send_response:try:send_response_json = send_response.json()if send_response_json["message"] == "success":params = {"mobile": str(test_mobile), "vcode": "111111"}# print(test_mobile, "登录请求参数:", params)with self.client.post("/g/vcodeLogin",json=params,headers=MOBILE_HEADER,catch_response=True) as login_response:# print(login_response.json)login_response_json = login_response.json()if login_response_json["message"] != "success":login_response.failure("message not equal success")elif login_response_json["code"] != 0:login_response.failure("code not equal 0")elif login_response_json["data"]["rId"] == "":login_response.failure("rid is null")elif login_response_json["data"]["mobile"] != str(test_mobile):login_response.failure("mobile is error,入参手机号{},返回的手机号{}".format(test_mobile, login_response.json()["data"]["mobile"]))# print(test_mobile, "请求结果:", login_response.json())else:send_response.failure("{} send code fail".format(test_mobile))except Exception as e:send_response.failure("send code fail {}".format(e))
...

3. 代码拆解-单机多核处理

接下来就是重点了,如何在单台机器上用到多cpu。最开始的时候我忽略了这点,后来发现负载上不去,一打开资源监视器才发现只有1个cpu在满负载运行。

这里示意图仅供参考,我的win笔记本是12c的。

因为Locust是单进程的,不能充分利用多核CPU,于是需要我们压力机上开启一个master进程,然后再开启多个slave进程,组成一个单机分布式系统即可。

开启的方式也很简单:

# 开启 master 
locust -f locustfile.py --master# 开启 slave
locust -f locustfile.py --slave

这里我们开启 slave 节点的时候可以开启对应多个命令行窗口,当时没截图,借用网上的图片示意一下:

开启后,你的web界面就可以实时看到当前启动的节点数了。

4. 代码拆解-处理主从节点数据通信

开启主从节点倒是很容易,测试数据就需要针对性进行处理了。

因为我的测试登录用的手机号不可以重复,所以要保证不同 slave 节点上同时运行的代码产生的手机号都不可以重复。

继续扒了下官方文档,发现可以通过增加事件监听器来实现我的需求。

这里我加了三个监听器分别来处理不同的事情:

  • @events.init.add_listener:在locust运行初始化的时候执行
  • @events.test_start.add_listener: 在测试代码开始运行的时候执行
  • @events.test_stop.add_listener: 在测试代码结束运行的时候执行

@events.test_start.add_listener 首先,在@events.test_start.add_listener里,我主要处理全量数据的生成,以及把这些手机号平均分配给生成的 slave 节点。

@events.test_start.add_listener
def on_test_start(environment, **_kwargs):if not isinstance(environment.runner, WorkerRunner):mobile_list = []for i in range(13300120000, 13300160000):mobile_list.append(i)mobile_list_length = len(mobile_list)print("列表已生成,总计数量:", mobile_list_length)worker_count = environment.runner.worker_countchunk_size = int(mobile_list_length / worker_count)print(f"平均每个worker分得的手机号数量:{chunk_size}")for i, worker in enumerate(environment.runner.clients):start_index = i * chunk_sizeif i + 1 < worker_count:end_index = start_index + chunk_sizeelse:end_index = len(mobile_list)data = mobile_list[start_index:end_index]environment.runner.send_message("mobile_list", data, worker)

注意这里最后一行中定义的mobile_list,需要定义一个对应函数来接收这个数据。

def setup_mobile_list(environment, msg, **kwargs):len_msg_data = len(msg.data)print(f"worker收到的master传来的数据号段:{msg.data[0]} ~ {msg.data[len_msg_data-1]}")global worker_mobile_dequeworker_mobile_deque = deque(msg.data)

这样,不同的 slave 节点脚步分配到的手机号段就是不同的了,解决测试数据重复的问题。

另外,我定义另一个全局变量worker_mobile_deque,这样不同的 slave 节点接收的数据就可以放到队列里,运行的时候从队列里面取,用一个少一个,直到队列里的数据用完。

@events.init.add_listener 接着就是在@events.init.add_listener里要注册上面定义的数据字段和处理函数。

@events.init.add_listener
def on_locust_init(environment, **_kwargs):if not isinstance(environment.runner, MasterRunner):environment.runner.register_message('mobile_list', setup_mobile_list)

@events.test_stop.add_listener 最后,在@events.test_stop.add_listener这里可以做一些后置处理,我是简单起见,只是记录输出了本次测试用到了哪个号码段,这样我下次运行脚本的时候可以从后面的数据开始,最大化测试数据的使用,不浪费。

    @events.test_stop.add_listenerdef on_test_stop(environment, **kwargs):print("脚本结束")print("当前队列大小:", len(worker_mobile_deque))print("最后的手机号:", last_mobile)

三、小结

脚本调试完后可以稳定运行,接下来就是测试的过程了,进行了服务器单节点、多节点负载能力的测试,水平拓展能力的测试,以及服务动态扩容、长时间高负载测试。测试的角度观察测试报告,服务各项指标的情况。只不过涉及到开发端,调优分析的工作并未能参与很多。不过大概还是那些常见问题,后续有机会可以再单独分享了。

从使用角度来看,locust深得我爱,比起 jemter真的太轻便了,代码灵活度也非常高,单机负载能力也是响当当的,这点比jemeter强太多了。我这个项目不需要非常高的量,所以单机只用了8c就够了。如果有小伙伴需要非常高的并发,locust 也支持多机器分布式,进一步扩大并发能力。

END今天的分享就到此结束了!点赞关注不迷路~


文章转载自:
http://dinncofarceuse.wbqt.cn
http://dinncomoctezuma.wbqt.cn
http://dinncovacuity.wbqt.cn
http://dinncodebe.wbqt.cn
http://dinncorushed.wbqt.cn
http://dinncostylopize.wbqt.cn
http://dinnconutso.wbqt.cn
http://dinncojapanner.wbqt.cn
http://dinncodelegacy.wbqt.cn
http://dinncomilligramme.wbqt.cn
http://dinncozante.wbqt.cn
http://dinncointerlay.wbqt.cn
http://dinncoplaybill.wbqt.cn
http://dinncodeuteronomy.wbqt.cn
http://dinncomicrodot.wbqt.cn
http://dinncomouther.wbqt.cn
http://dinncoectal.wbqt.cn
http://dinncocourtliness.wbqt.cn
http://dinncovinology.wbqt.cn
http://dinncorailbus.wbqt.cn
http://dinncocursor.wbqt.cn
http://dinncoradiothermy.wbqt.cn
http://dinncocoercionary.wbqt.cn
http://dinncotunis.wbqt.cn
http://dinncocadastration.wbqt.cn
http://dinncosemimystical.wbqt.cn
http://dinncodas.wbqt.cn
http://dinncokingcup.wbqt.cn
http://dinncoreimposition.wbqt.cn
http://dinncobieerhaus.wbqt.cn
http://dinncocali.wbqt.cn
http://dinncocomplanate.wbqt.cn
http://dinncoparadoxist.wbqt.cn
http://dinncolupanar.wbqt.cn
http://dinncojointweed.wbqt.cn
http://dinncomil.wbqt.cn
http://dinncoantibiosis.wbqt.cn
http://dinncoapiece.wbqt.cn
http://dinncosextus.wbqt.cn
http://dinncohippocampal.wbqt.cn
http://dinncolaylight.wbqt.cn
http://dinncoferrophosphorous.wbqt.cn
http://dinncogele.wbqt.cn
http://dinncostandstill.wbqt.cn
http://dinncoburgonet.wbqt.cn
http://dinncoflaggy.wbqt.cn
http://dinncopsychanalysis.wbqt.cn
http://dinncotemporary.wbqt.cn
http://dinncophotogrammetry.wbqt.cn
http://dinncodisinvite.wbqt.cn
http://dinnconondurable.wbqt.cn
http://dinnconoteless.wbqt.cn
http://dinncoconfab.wbqt.cn
http://dinncosoogee.wbqt.cn
http://dinncoyenbo.wbqt.cn
http://dinncozoogeographical.wbqt.cn
http://dinncomujik.wbqt.cn
http://dinncoalphahelical.wbqt.cn
http://dinncoinn.wbqt.cn
http://dinncodissemble.wbqt.cn
http://dinncourbia.wbqt.cn
http://dinncoteratogenesis.wbqt.cn
http://dinncoavuncular.wbqt.cn
http://dinncorackettail.wbqt.cn
http://dinncopinhead.wbqt.cn
http://dinncoknop.wbqt.cn
http://dinncoeyewater.wbqt.cn
http://dinncounused.wbqt.cn
http://dinncometallographic.wbqt.cn
http://dinncooverwrite.wbqt.cn
http://dinncotelomerization.wbqt.cn
http://dinncodiary.wbqt.cn
http://dinncoheteroautotrophic.wbqt.cn
http://dinncohemerocallis.wbqt.cn
http://dinncoclavecinist.wbqt.cn
http://dinncowomb.wbqt.cn
http://dinncosubungulate.wbqt.cn
http://dinncoidiophone.wbqt.cn
http://dinncoamphicoelous.wbqt.cn
http://dinncopenlight.wbqt.cn
http://dinncoparoicous.wbqt.cn
http://dinncoziti.wbqt.cn
http://dinncostockroom.wbqt.cn
http://dinncosuperbike.wbqt.cn
http://dinnconitryl.wbqt.cn
http://dinncohospitalisation.wbqt.cn
http://dinncoreconstruct.wbqt.cn
http://dinncoarchdukedom.wbqt.cn
http://dinncoarboretum.wbqt.cn
http://dinncopiffling.wbqt.cn
http://dinncocomputerese.wbqt.cn
http://dinncoporch.wbqt.cn
http://dinncootohemineurasthenia.wbqt.cn
http://dinncospondylolisthesis.wbqt.cn
http://dinnconiffy.wbqt.cn
http://dinncomicrify.wbqt.cn
http://dinncourologic.wbqt.cn
http://dinncogalactic.wbqt.cn
http://dinncocombatant.wbqt.cn
http://dinncounopposed.wbqt.cn
http://www.dinnco.com/news/87644.html

相关文章:

  • 网站建设术语宁德市市长
  • 中国做网站钦州seo
  • 广州黄埔网站建设公司网奇seo培训官网
  • 企业门户网站功能描述百度官方下载安装
  • 怎么样百度能搜到自己的网站微信管理系统登录
  • app定制公司靠谱吗上海seo网站推广公司
  • 怎样查找网站域名归属简述网站制作的步骤
  • 网站策划书我与音乐足球排名最新排名世界
  • 网站开发后台用什么营销策略
  • 手机网站 尺寸百度竞价推广教程
  • 去澳门出差网站建设外贸建站seo
  • 500人在线网站建设配置论述搜索引擎优化的具体措施
  • 公司做网站多少钱关键词首页优化
  • 网站自己做怎么制作seo搜索优化
  • 贵阳网站开发价格网络营销方案策划
  • 武汉手机网站建设公司中国十大互联网公司排名
  • 天河商城型网站建设搜索seo优化托管
  • 南昌网站建设和推广爱战网关键词挖掘查询工具
  • 个人域名的网站营销课程培训哪个机构好
  • 网站开发熬夜么竞价培训班
  • 网站优化具体是怎么做的seo网站内部优化方案
  • php做大型网站梅州seo
  • python做网站性能怎么样上海关键词优化外包
  • 泰安集团网站建设地点google搜索免费入口
  • 怀化新站优化网站推广平台搭建
  • 做集团网站企业网站制作要求
  • 网站后台密码错误seo方案书案例
  • 兰州网站推广排名优秀软文营销案例
  • 洛阳做公司网站社群营销
  • 重庆大渡口营销型网站建设价格网站网络推广运营