首页 > 其他分享 >locust多任务按顺序执行

locust多任务按顺序执行

时间:2023-11-30 14:35:06浏览次数:34  
标签:顺序 url self req locust client task 多任务 def

背景

想要低并发验证不同通道的响应时间,即不同的域名访问同一资源,需要验证不同的域名响应时长。

实践1

写了个简单的locustfile.py内容如下:

# locustfile.py
from locust import HttpUser, task, TaskSet, between

class MyUser(HttpUser):
    wait_time = between(0.1, 1.0)  # 每个请求间可能等待的时间
    
    @task
    def fun1(self):
        self.client.request_name='方法1'
        url='http://api1.sample.com/api/file?path=xxxx'
        self.req(url)
   
    
    @task
    def fun2(self):
        self.client.request_name='方法2'
        url='http://api2.sample.com/api/file?path=xxxx'
        self.req(url)
   
    @task
    def fun3(self):
        self.client.request_name='方法3'
        url='http://api3.sample.com/api/file?path=xxxx'
        self.req(url)
   
    @task
    def fun4(self):
        self.client.request_name='方法4'
        url='http://api4.sample.com/api/file?path=xxxx'
        self.req(url)
   
    def req(self,url):
        with self.client.get(url, headers=headers, catch_response=True) as r:
            if r.status_code != 200:
                r.failure(f'{urlparse(url).netloc} 返回码非200!')
            elif 'json' in r.headers.get('Content-Type'):  # 因为是流媒体,请求失败可能会返回异常的json信息,所以增加判断响应头
                r.failure(f'{urlparse(url).netloc} 请求失败,返回内容非文件')

执行 locust --headless -u 4 -t 5m MyUser -H https://www.baiduu.com/

即并发4用户执行5分钟。然后分析了请求的链路发现本应耗时比较短的域名平均耗时居然比较高,研发说是因为四个域名同时执行,DNS解析可能耗时有问题。。。。

就是说要分别单独执行,然后再重新分析耗时。所以问题来了,如果要单独执行每个域名,每次需要注释掉其他不需要执行的三个任务,最主要的是单独执行出来的报告只能展示一个请求的图表,不利于分析,为了解决这俩问题,重新实现。

实践2

改写方法如下:

# locustfile.py
from locust import HttpUser, task, TaskSet, between
from locust.exception import StopUser

class MyUser(HttpUser):
    wait_time = between(0.1, 1.0)  # 每个请求间可能等待的时间
    
    # 启动前执行,所以等于是单独测试了不同的域名低并发的请求响应时长
    def on_start(self):
        # 当Locust用户开始时只执行此方法一次
        self.execute_task_for_duration(self.fun1, 5 * 60)
        self.execute_task_for_duration(self.fun2, 5 * 60)
        self.execute_task_for_duration(self.fun3, 5 * 60)
        self.execute_task_for_duration(self.fun4, 5 * 60)
        raise StopUser  # 并不会继续执行其他的任务了,按顺序执行完上面的任务就结束

    def execute_task_for_duration(self, task_function, duration_seconds):
        end_time = time.time() + duration_seconds
        while time.time() < end_time:
            task_function()  # 调用任务函数
            time.sleep(self.user.wait_time())  # 等待指定时间
    
    @task
    def fun1(self):
        self.client.request_name='方法1'
        url='http://api1.sample.com/api/file?path=xxxx'
        self.req(url)
   
    
    @task
    def fun2(self):
        self.client.request_name='方法2'
        url='http://api2.sample.com/api/file?path=xxxx'
        self.req(url)
   
    @task
    def fun3(self):
        self.client.request_name='方法3'
        url='http://api3.sample.com/api/file?path=xxxx'
        self.req(url)
   
    @task
    def fun4(self):
        self.client.request_name='方法4'
        url='http://api4.sample.com/api/file?path=xxxx'
        self.req(url)
   
    def req(self,url):
        with self.client.get(url, headers=headers, catch_response=True) as r:
            if r.status_code != 200:
                r.failure(f'{urlparse(url).netloc} 返回码非200!')
            elif 'json' in r.headers.get('Content-Type'):  # 因为是流媒体,请求失败可能会返回异常的json信息,所以增加判断响应头
                r.failure(f'{urlparse(url).netloc} 请求失败,返回内容非文件')

最后的图表如下:
image-20231129170901077

标签:顺序,url,self,req,locust,client,task,多任务,def
From: https://www.cnblogs.com/wjlv/p/17867269.html

相关文章

  • SpringBoot的配置文件application.yml及加载顺序详解
    SpringBoot配置文件application.yml及加载顺序配置文件分类自定义配置文件配置文件总结Springboot中application.yml、application.properties和bootStrap.yml加载顺序SpringApplication位于项目根目录以jar包发布springboot项目时若application.yml和bootStra......
  • SQL 关键字执行顺序
    1.FromandJOIN首先我们进行查询的时候,肯定是先获得一份数据集的,From语句和JOIN被先执行的,就是为了获得数据集的。2.WHERE一旦数据集给拿到了,WHERE限制条件会被用到某些行上,并把不满足的行给抛弃掉。并且,这能够用到各种数据类型中3.GROUPBYWHERE条件限定之后,接下来就是GRO......
  • Jmeter组件执行顺序与作用域
    一、Jmeter重要组件:1)配置元件---ConfigElement:用于初始化默认值和变量,以便后续采样器使用。配置元件大其作用域的初始阶段处理,配置元件仅对其所在的测试树分支有效,如,在同一个作用域的任何采样器前。2)前置处理器---PreProcessors:前置处理器会在采样器发出请求之前做......
  • Kafka 如何保证消息消费的全局顺序性
    哈喽大家好,我是咸鱼今天我们继续来讲一讲Kafka当有消息被生产出来的时候,如果没有指定分区或者指定key,那么消费会按照【轮询】的方式均匀地分配到所有可用分区中,但不一定按照分区顺序来分配我们知道,在Kafka中消费者可以订阅一个或多个主题,并被分配一个或多个分区如果一......
  • ggplot2 绘图 x轴标签顺序
     001、测试数据及绘图x<-c("B","A","D","C","E")##测试数据顺序y<-c(5,6,7,8,9)df<-data.frame(x=x,y=y)dflibrary("ggplot2")ggplot(data=df,aes(x=x,y=y))......
  • 秦疆的Java课程笔记:35 流程控制 顺序结构
    Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。顺序结构是最简单的算法结构。publicclassShunXuDemo{publicstaticvoidmain(String[]args){System.out.println("hello1");System.out.println("hello2");......
  • 构造函数的执行顺序
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceBCode_Framework_ConsoleApp{classProgram{staticvoidMain(string[]args){Customercust......
  • 微服务 过滤器链执行顺序
       ......
  • vue中watch、computed、methods的执行顺序
    一、默认加载情况如果watch不加immediate:true属性(页面初加载的时候,不会执行watch,只有值变化后才执行),则只执行computed(在mounted后执行);如果watch添加immediate:true属性(在beforeCreate后created前执行),则先执行watch、再执行computed;二、触发某一事件后先执行method,再watch,再......
  • 13-基础SQL-DQL(数据查询语言)-执行顺序(编写顺序和执行顺序)
    DQL-介绍(常用)DQL英文全称是DataQueryLanguage(数据查询语言),数据查询语言用来查询数据库中表的记录查询关键字:SELECTDQL-语法DQL-语法(编写顺序和执行顺序) 案例:创建一个emp员工表,添加一些员工数据CREATETABLEemp(idintcomment"编号",worknovarchar(10......