locust_plugins之TimescaleListener踩坑
介紹
TimescaleListener是locust的一個插件惨驶,主要作用是把壓測過程中產(chǎn)生的數(shù)據(jù)存到配置了Timescale 功能的Postgres 數(shù)據(jù)庫中,并用Grafana圖表顯示出來,相比于locust原生的界面婉称,使用該插件后能夠顯示歷史數(shù)據(jù)庇忌,能夠顯示的內(nèi)容也比較豐富,極大提升了locust的可用性和易用性邪蛔,十分推薦使用。
需要的組件
- locust
- Postgres
- Grafana
安裝步驟
1扎狱、安裝Postgres 并配置Timescale
https://docs.timescale.com/latest/getting-started/setup
按照上述鏈接中的安裝步驟后無法執(zhí)行timescaledb-tune和啟動Postgres數(shù)據(jù)庫侧到,timescaledb-tune需要指定pg_conf配置文件來進(jìn)行配置:
timescaledb-tune --pg-config=/usr/pgsql-12/bin/pg_config
配置完之后可以通過以下命令啟動Postgres數(shù)據(jù)庫:
systemctl start postgresql-12
此時可能無法使用psql命令執(zhí)行psql文件,需要在/usr/bin下建立軟鏈接:
ln -s psql /usr/pgsql-12/bin/psql
然后需要在pg_hba.conf中添加策略來開啟postgres用戶登錄:
#訪問來源(本地是local淤击,遠(yuǎn)程是host) 能夠訪問的數(shù)據(jù)庫 用戶名 地址(訪問來源是host時配置) 無需密碼
host all postgres 127.0.0.1/32 trust
然后修改postgresql.conf配置文件中的host匠抗、端口、日志級別污抬、數(shù)據(jù)目錄等配置汞贸。
然后再根據(jù)https://docs.timescale.com/latest/getting-started/setup中的步驟進(jìn)行建庫建表的配置。
2印机、建表
https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/timescale_schema.sql將該鏈接中的sql文件保存到服務(wù)器矢腻,進(jìn)入上步中所建的數(shù)據(jù)庫中:
psql -U postgres -h localhost -d tutorial
執(zhí)行sql文件:
\i timescale_schema.sql
3、配置TimescaleListener所需的Postgres環(huán)境變量
TimescaleListener所需的Postgres數(shù)據(jù)庫配置是通過環(huán)境變量的方式配置耳贬,Postgres所有環(huán)境變量列表通過該鏈接: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安裝可以直接參考官網(wǎng)文檔。
安裝完成后配置數(shù)據(jù)源為PostgreSQL 咒劲,參考該鏈接:https://grafana.com/docs/grafana/latest/datasources/postgres/
配置插件所需的Dashboard可以導(dǎo)入該json:https://grafana.com/grafana/dashboards/10878
沒有啥坑顷蟆,按步驟執(zhí)行即可。
5腐魂、導(dǎo)入LOCUST_GRAFANA_URL 環(huán)境變量
跟第三步導(dǎo)入數(shù)據(jù)庫環(huán)境變量一樣帐偎,但是不知道有啥用:
export LOCUST_GRAFANA_URL=https://my.grafana.host.com/d/qjIIww4Zz/locust?orgId=1
也是因為這個環(huán)境變量導(dǎo)致TimescaleListener插件只能通過locust命令行的方式啟動壓測,不能以導(dǎo)入庫的方式啟動蛔屹,需要自己改寫一下才行削樊。
6、運行壓測
按照locust命令行的方式啟動:
locust --headless -f mytestplan.py
如果出現(xiàn)Make sure the TimescaleDB extension has been preloaded
報錯的話,需要到數(shù)據(jù)庫中關(guān)閉ts_insert_blocker
觸發(fā)器:
# 進(jìn)入數(shù)據(jù)庫
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都需要關(guān)閉觸發(fā)器
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就可以看到數(shù)據(jù)了迅脐。