locust结合Prometheus和grafana(一)

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: locust结合Prometheus和grafana

一、Locust介绍

一个开源性能测试工具,使用 Python 代码来定义用户行为。用它可以模拟百万计的并发用户访问你的系统,方便、高效、可分布式等

二、工具安装

环境python3
pip install locust
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn locust

三、locust类库介绍

Locust官方文档

Httpuser类:就是用户行为的操作
Client方法:发送请求
Task类:任务控制的类,权重,跳过等
Taskset类:测试集类
On_start方法:类似于自动化中的setup,每个虚拟用户执行前都会执行这个方法
On_stop方法:类似于自动化中的teardown,每个虚拟用户执行结束后执行这个方法
@task装饰器,如果task后面带有数字,则代表的是测试集中的权重。参数越大每次被虚拟用户执行的概率越高。如果不设置默认为1
Between方法中的是休眠时间:会取between数值中间的随机数
Locust中还有其他很多类,我暂时没用到,有兴趣可以下去了解下,非常不错的一款性能工具

四、locust使用

Windows

我这里使用的是云服务器2c4g, 带宽:8Mbps

代码:以登录vsite网站来压吧 100并发

# coding=utf-8
"""
    @Project :pachong-master 
    @File    :test001.py
    @Author  :gaojs
    @Date    :2022/5/19 21:47
    @Blogs   : https://www.gaojs.com.cn
"""
from locust import HttpUser, between, task, TaskSet, tag
import os
# 强制去掉控制台InsecureRequestWarning
import urllib3
# 随机产生UA类库
from faker import Factory
urllib3.disable_warnings()
class TaskTest(TaskSet):
    def on_start(self):
        """
        description:每个用户执行压测之前都会获取随机数和数字签名
        :return:
        """
        # 每个用户执行压测之前都会获取随机数和数字签名
        # print('====================清理log结束,压测开始, 获取随机数和数字签名!!!========================')
        pass
    # 压测任务,也可以是@task(10)啥的,这个数字是代表权重,数值越大,执行的频率就越高
    @task
    def stress_get(self):
        """
        登录虚拟站点
        :return:
        """
        url = '/prx/000/http/localh/login'
        data = {
            "Content-Type": "application/x-www-form-urlencoded, charset=UTF-8",
            'Connection': 'close',
            "method": "method1",
            "uname": "t",
            "pwd1": None,
            "pwd2": None,
            "pwd": "t",
            "submitbutton": "Sign In"
        }
        randon_ua = Factory.create()
        ua = randon_ua.user_agent()
        headers = {
            'User-Agent': ua
        }
        rsp = self.client.post(url=url, headers=headers, data=data, verify=False, allow_redirects=False, name='loginVsite压测')
        # print(rsp.status_code)
        print(rsp.text)
        assert rsp.status_code == 302
    # 执行并发测试后执行的动作,比如保存log等操作,查看报告http://localhost:8089/
    def on_stop(self):
        pass
class UserBehavior(HttpUser):
    host = '192.168.120.209'
    # 每次请求停顿时间
    wait_time = between(3, 10)
    tasks = [TaskTest]
if __name__ == "__main__":
    os.system("locust -f test004.py --host=https://192.168.120.209 --web-host=127.0.0.1")

执行

十秒起100个虚拟用户

image.png

报告

image.png

charts

image.png

image.png

RPS

image.png

分析结果:

平均相应时间:
吞吐量:1000以内
最小相应时间:
最大响应时间:
网络是否有波动:
带宽是否影响:


相关文章
|
3月前
|
Prometheus Cloud Native Java
微服务框架(二十三)Prometheus + Grafana 安装、配置及使用
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 安装、配置及使用 本系列文章中所使用的框架版本为Spring ...
|
2天前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
12 2
|
2月前
|
存储 Prometheus Cloud Native
Grafana 系列 - 统一展示 -2-Prometheus 数据源
Grafana 系列 - 统一展示 -2-Prometheus 数据源
|
2月前
|
JSON Prometheus Cloud Native
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
|
2月前
|
Prometheus Kubernetes Cloud Native
「译文」使用 Prometheus 和 Grafana 实现 SLO
「译文」使用 Prometheus 和 Grafana 实现 SLO
|
3月前
|
Prometheus 监控 Kubernetes
Prometheus + Grafana安装
Prometheus + Grafana安装
|
4月前
|
Prometheus 监控 前端开发
prometheus|云原生|grafana-9.4.3版本的主题更改
prometheus|云原生|grafana-9.4.3版本的主题更改
98 0
|
6月前
|
Prometheus 监控 Cloud Native
一文带你吃透MySQL性能监控解决方案:Prometheus+Grafana
MySQL性能监控解决方案:Prometheus+Grafana问题描述 在对MySQL进行主从复制、分库分表等架构之后,MySQL的节点数量变得越来越多,无法实时监控到每一台MySQL节点,此时应当如何处理? 问题分析与解决方案 针对上面的问题,需要用Prometheus + Grafana对服务器进行统一监控、规划与报警,时刻关注服务器的响应情况。当出现宕机或异常时,Grafana可迅速通过短信、钉钉、邮件等方式通知相关人员,进而快速对生产环节进行补救。 Prometheus概述与适用场景 Prometheus 是 一 个 开 源 的 服 务 监 控 系 统 和 时 间 序 列 数 据
481 0
|
5月前
|
Prometheus Cloud Native
Mac下安装 Prometheus+Grafana
Mac下安装 Prometheus+Grafana
149 0
|
3月前
|
Prometheus 监控 Cloud Native
微服务框架(二十二)Prometheus + Grafana 可视化监控
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 可视化监控的介绍,下篇为Prometheus + Grafana...