Docker 下 【Locust 2.8】 + prometheus + influxdb + grafana 性能测试结果采集、监控、数据持久化

本文介绍如何使用Locust进行性能测试,并通过Prometheus和InfluxDB收集及展示测试数据。文中提供了详细的代码实现及配置步骤。
本文使用docker部署,建议使用docker-compose一键部署方式,观看这篇:
https://blog.csdn.net/qq_41522024/article/details/128997583

环境准备

  1. Docker + Docker Compose 环境已配置好
  2. 打包好你的镜像,并且镜像中已经包含了你的测试代码

性能测试结果采集代码

网上 99.99% 的教程都是用这个:https://github.com/myzhan/boomer/blob/master/prometheus_exporter.py

当然我也不例外 😛

需要注意的是,2.8.3 版本,在分布式运行的时候,需要你在 master 以及 worker 都要有相同的采集代码

# coding: utf8

import six
from itertools import chain

from flask import request, Response
from locust import stats as locust_stats, runners as locust_runners
from locust import User, task, events
from prometheus_client import Metric, REGISTRY, exposition

# This locustfile adds an external web endpoint to the locust master, and makes it serve as a prometheus exporter.
# Runs it as a normal locustfile, then points prometheus to it.
# locust -f prometheus_exporter.py --master

# Lots of code taken from [mbolek's locust_exporter](https://github.com/mbolek/locust_exporter), thx mbolek!


class LocustCollector(object):
    registry = REGISTRY

    def __init__(self, environment, runner):
        self.environment = environment
        self.runner = runner

    def collect(self):
        # collect metrics only when locust runner is spawning or running.
        runner = self.runner

        if runner and runner.state in (locust_runners.STATE_SPAWNING, locust_runners.STATE_RUNNING):
            stats = []
            for s in chain(locust_stats.sort_stats(runner.stats.entries), [runner.stats.total]):
                stats.append({
                    "method": s.method,
                    "name": s.name,
                    "num_requests": s.num_requests,
                    "num_failures": s.num_failures,
                    "avg_response_time": s.avg_response_time,
                    "min_response_time": s.min_response_time or 0,
                    "max_response_time": s.max_response_time,
                    "current_rps": s.current_rps,
                    "median_response_time": s.median_response_time,
                    "ninetieth_response_time": s.get_response_time_percentile(0.9),
                    # only total stats can use current_response_time, so sad.
                    # "current_response_time_percentile_95": s.get_current_response_time_percentile(0.95),
                    "avg_content_length": s.avg_content_length,
                    "current_fail_per_sec": s.current_fail_per_sec
                })

            # perhaps StatsError.parse_error in e.to_dict only works in python slave, take notices!
            errors = [e.to_dict() for e in six.itervalues(runner.stats.errors)]

            metric = Metric('locust_user_count', 'Swarmed users', 'gauge')
            metric.add_sample('locust_user_count', value=runner.user_count, labels={})
            yield metric

            metric = Metric('locust_errors', 'Locust requests errors', 'gauge')
            for err in errors:
                metric.add_sample('locust_errors', value=err['occurrences'],
                                  labels={'path': err['name'], 'method': err['method'],
                                          'error': err['error']})
            yield metric

            is_distributed = isinstance(runner, locust_runners.MasterRunner)
            if is_distributed:
                metric = Metric('locust_slave_count', 'Locust number of slaves', 'gauge')
                metric.add_sample('locust_slave_count', value=len(runner.clients.values()), labels={})
                yield metric

            metric = Metric('locust_fail_ratio', 'Locust failure ratio', 'gauge')
            metric.add_sample('locust_fail_ratio', value=runner.stats.total.fail_ratio, labels={})
            yield metric

            metric = Metric('locust_state', 'State of the locust swarm', 'gauge')
            metric.add_sample('locust_state', value=1, labels={'state': runner.state})
            yield metric

            stats_metrics = ['avg_content_length', 'avg_response_time', 'current_rps', 'current_fail_per_sec',
                             'max_response_time', 'ninetieth_response_time', 'median_response_time',
                             'min_response_time',
                             'num_failures', 'num_requests']

            for mtr in stats_metrics:
                mtype = 'gauge'
                if mtr in ['num_requests', 'num_failures']:
                    mtype = 'counter'
                metric = Metric('locust_stats_' + mtr, 'Locust stats ' + mtr, mtype)
                for stat in stats:
                    # Aggregated stat's method label is None, so name it as Aggregated
                    # locust has changed name Total to Aggregated since 0.12.1
                    if 'Aggregated' != stat['name']:
                        metric.add_sample('locust_stats_' + mtr, value=stat[mtr],
                                          labels={'path': stat['name'], 'method': stat['method']})
                    else:
                        metric.add_sample('locust_stats_' + mtr, value=stat[mtr],
                                          labels={'path': stat['name'], 'method': 'Aggregated'})
                yield metric


# prometheus监听端口
@events.init.add_listener
def locust_init(environment, runner, **kwargs):
    print("locust init event received")
    if environment.web_ui and runner:
        @environment.web_ui.app.route("/export/prometheus")
        def prometheus_exporter():
            registry = REGISTRY
            encoder, content_type = exposition.choose_encoder(request.headers.get('Accept'))
            if 'name[]' in request.args:
                registry = REGISTRY.restricted_registry(request.args.get('name[]'))
            body = encoder(registry)
            return Response(body, content_type=content_type)

        REGISTRY.register(LocustCollector(environment, runner))

使用方式:

# 可以是以模块的方式导入你的 locustfile 中
from prometheus_exporter import *

运行脚本之后,访问http://localhost:8089/export/prometheus 测试一下,如图输出信息即为成功
在这里插入图片描述

prometheus + influxdb + grafana

prometheus influxdb配置

这个时候,你的 locust 镜像应该已经创建好了,镜像里面也应该包含 采集代码 以及的 测试脚本

运行 prometheus + influxdb 的 docker-compose-prometheus-influxdb.yml

docker-compose.yml 包含了 prometheus 、 influxdb 、 locust_master 的容器

docker-compose -f /your/path/docker-compose-prometheus-influxdb.yml up -d

控制台会输出:

Creating influxdb … done
Creating locust_master … done
Creating prometheus … done

这个时候,访问http://IP:9090/targets ,如图 prometheus + influxdb 的配置即为成功

因为 docker-compose.yml 中已经配置好了卷的映射,关闭容器或者重启容器数据都不会丢失

grafana 的配置
  1. 启动容器

    docker run -d -p 3000:3000 --name grafana grafana/grafana
    
  2. 访问 http://IP:3000/

    初始账户密码:admin/admin

  3. 添加数据源,选择prometheus在这里插入图片描述在这里插入图片描述

  4. 输入prometheus的IP以及端口在这里插入图片描述

  5. 点击 import,选择模板:12081在这里插入图片描述

  6. 导入刚刚创建的数据源 在这里插入图片描述

  7. 查看仪表盘在这里插入图片描述

  8. 执行测试之后的结果:在这里插入图片描述

### 搭建 Docker、JMeter、InfluxDBGrafana 的性能监控平台 为了使用 Docker 部署 JMeter、InfluxDBGrafana 构建性能监控平台,需要完成以下关键步骤。这些步骤涵盖了从安装 Docker 到配置 InfluxDB 数据库以及使用 Grafana 可视化数据的完整过程。 #### 1. 安装 Docker 在开始之前,确保系统上已正确安装 Docker。如果尚未安装 Docker,请参考官方文档或卸载旧版本[^5]。可以使用以下命令检查 Docker 是否已正确安装: ```bash docker --version ``` #### 2. 启动 InfluxDB 容器 启动一个 InfluxDB 容器以存储性能测试数据。可以通过以下命令运行 InfluxDB 容器: ```bash docker run -d --name influxdb -p 8086:8086 influxdb ``` 此命令会拉取并运行 InfluxDB 容器,并将端口 8086 映射到主机上[^3]。 #### 3. 创建 InfluxDB 数据库 进入 InfluxDB 容器并创建一个名为 `jmeter` 的数据库,用于存储 JMeter 的性能测试数据: ```bash docker exec -it influxdb /bin/bash influx CREATE DATABASE jmeter USE jmeter EXIT ``` 上述命令会进入 InfluxDB CLI 并创建所需的数据库[^3]。 #### 4. 启动 Grafana 容器 启动一个 Grafana 容器以可视化性能测试数据。可以通过以下命令运行 Grafana 容器: ```bash docker run -d --name grafana -p 3000:3000 grafana/grafana ``` 此命令会拉取并运行 Grafana 容器,并将端口 3000 映射到主机上[^3]。 #### 5. 配置 Grafana 数据源 访问 Grafana Web 界面(默认地址为 `http://localhost:3000`),登录后添加 InfluxDB 作为数据源。具体步骤如下: - 登录到 Grafana,默认用户名和密码为 `admin`。 - 进入 `Configuration -> Data Sources`。 - 点击 `Add data source`,选择 `InfluxDB`。 - 配置数据源信息,包括 URL (`http://influxdb:8086`) 和数据库名称 (`jmeter`)[^3]。 #### 6. 启动 JMeter 容器 启动一个 JMeter 容器并将结果发送到 InfluxDB。可以通过以下命令运行 JMeter 容器: ```bash docker run -d --name jmeter \ -v $(pwd)/test-plan.jmx:/test-plan.jmx \ -v $(pwd)/jmeter.properties:/jmeter.properties \ -e INFLUXDB_URL=http://influxdb:8086 \ -e INFLUXDB_DATABASE=jmeter \ blazemeter/jmeter-influxdb-grafana ``` 此命令会启动 JMeter 容器,并将试计划文件和配置文件挂载到容器中[^4]。 #### 7. 验证平台功能 运行 JMeter 试计划后,返回 Grafana 界面查看性能测试数据的可视化图表。通过优化 InfluxDBGrafana 配置,可以更全面地监控性能,并及时发现和解决问题[^2]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值