locust_plugins之TimescaleListener踩坑

locust_plugins之TimescaleListener踩坑

介绍

TimescaleListener是locust的一个插件,主要作用是把压测过程中产生的数据存到配置了Timescale 功能的Postgres 数据库中,并用Grafana图表显示出来,相比于locust原生的界面,使用该插件后能够显示历史数据,能够显示的内容也比较丰富,极大提升了locust的可用性和易用性,十分推荐使用。

需要的组件
  • locust
  • Postgres
  • Grafana
安装步骤
1、安装Postgres 并配置Timescale

https://docs.timescale.com/latest/getting-started/setup

按照上述链接中的安装步骤后无法执行timescaledb-tune和启动Postgres数据库,timescaledb-tune需要指定pg_conf配置文件来进行配置:

timescaledb-tune --pg-config=/usr/pgsql-12/bin/pg_config

配置完之后可以通过以下命令启动Postgres数据库:

systemctl start postgresql-12

此时可能无法使用psql命令执行psql文件,需要在/usr/bin下建立软链接:

ln -s psql /usr/pgsql-12/bin/psql

然后需要在pg_hba.conf中添加策略来开启postgres用户登录:

#访问来源(本地是local,远程是host) 能够访问的数据库   用户名         地址(访问来源是host时配置)      无需密码
 host                             all           postgres             127.0.0.1/32             trust

然后修改postgresql.conf配置文件中的host、端口、日志级别、数据目录等配置。

然后再根据https://docs.timescale.com/latest/getting-started/setup中的步骤进行建库建表的配置。

2、建表

https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/timescale_schema.sql将该链接中的sql文件保存到服务器,进入上步中所建的数据库中:

psql -U postgres -h localhost -d tutorial

执行sql文件:

\i timescale_schema.sql
3、配置TimescaleListener所需的Postgres环境变量

TimescaleListener所需的Postgres数据库配置是通过环境变量的方式配置,Postgres所有环境变量列表通过该链接:https://www.postgresql.org/docs/current/libpq-envars.html

一般情况下所需的配置如下:

export PGHOST=127.0.0.1
export PGPORT=5432
export PGDATABASE=tutorial
export PGUSER=postgres
export PGPASSWORD=xxxxxx
4、安装和配置Grafana

Grafana安装可以直接参考官网文档。

安装完成后配置数据源为PostgreSQL ,参考该链接:https://grafana.com/docs/grafana/latest/datasources/postgres/

配置插件所需的Dashboard可以导入该json:https://grafana.com/grafana/dashboards/10878

没有啥坑,按步骤执行即可。

5、导入LOCUST_GRAFANA_URL 环境变量

跟第三步导入数据库环境变量一样,但是不知道有啥用:

export LOCUST_GRAFANA_URL=https://my.grafana.host.com/d/qjIIww4Zz/locust?orgId=1

也是因为这个环境变量导致TimescaleListener插件只能通过locust命令行的方式启动压测,不能以导入库的方式启动,需要自己改写一下才行。

6、运行压测

按照locust命令行的方式启动:

locust --headless -f mytestplan.py

如果出现Make sure the TimescaleDB extension has been preloaded报错的话,需要到数据库中关闭ts_insert_blocker触发器:

# 进入数据库
psql -U postgres -h localhost -d tutorial

# 查看有哪些表
tutorial=# \d

           List of relations
 Schema |    Name    | Type  |  Owner   
--------+------------+-------+----------
 public | events     | table | postgres
 public | request    | table | postgres
 public | testrun    | table | postgres
 public | user_count | table | postgres
 
 # 上述表中request、testrun、user_count都需要关闭触发器
 tutorial=# DROP TRIGGER ts_insert_blocker ON public.request;
 tutorial=# DROP TRIGGER ts_insert_blocker ON public.testrun;
 tutorial=# DROP TRIGGER ts_insert_blocker ON public.user_count;

再次运行压测,刷新Grafana就可以看到数据了。

你可能感兴趣的:(locust_plugins之TimescaleListener踩坑)