時間:2020/12/31
本文是對文章https://www.cnblogs.com/jinziguang/p/13610209.html的補充:將locust生成的數據通過prometheus收集,再通過grafana展示出來
環境:
windows:運行locust的master機和slave機;瀏覽器打開grafana頁面查看統計數據
linux:運行prometheus和grafana
軟件版本:
grafana7.3.6-1.x86_64.rpm
prometheus2.8.0.linux-amd64.tar.gz(https://github.com/prometheus/prometheus/releases/download/v2.8.0/prometheus-2.8.0.linux-amd64.tar.gz)
locust 1.4.1(實測該版本可以和boomer:github.com/myzhan/boomer匹配使用)(pip3.exe install locustio==1.4.1)
執行思路:
1.通過boomer項目中prometheus_exporter.py作為master機啟動,將數據采集在prometheus
2.slave機依然由go語言去做,作為一個負載生成器(此處也可以用python去做slave機)
3,服務器linux上安裝prometheus,配置文件中將slave機添加為節點機;服務器安裝grafana,將prometheus作為數據源進行展示
搭建環境:
1.修改prometheus_exporter.py,修改后的結果:
# 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 @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)) class Dummy(User): @task(20) def hello(self): pass
2.服務器安裝grafana:
sudo yum localinstall grafana7.3.6-1.x86_64.rpm安裝即可,systemctl restart grafana-server重啟服務。
瀏覽器輸入服務器ip:3000即可打開頁面,賬戶密碼默認admin,admin
3.服務器安裝prometheus:
將prometheus-2.8.0.linux-amd64.tar.gz解壓后執行./prometheus --web.enable-lifecycle --web.enable-admin-api &,之后修改yaml文件后只需要執行curl -X POST http://服務器ip:9090/-/reload即可
瀏覽器輸入服務器ip:9090即可打開頁面
4.修改prometheus配置文件(prometheus.yml),添加在最下面添加如下配置:
- job_name: locust metrics_path: '/export/prometheus' static_configs: - targets: ['slave機ip:8089'] labels: instance: locust
5.grafana可視化配置
a.添加數據源,選擇prometheus,輸入ip為服務器ip:9090即可
b.導入儀表盤,推薦使用https://grafana.com/grafana/dashboards/12081(該儀表盤需要grafana版本為7.3.6,筆者之前使用6.4.1版本結果導入儀表盤出錯,執行rpm -Uvh 進行升級即可)
以上兩步做完之后就可以看到grafana的儀表盤了,只是沒有數據。
執行壓測:
1.運行master機:locust --master --web-host=本機ip -f prometheus_exporter.py
2.檢查是否正在監聽:
cmd中執行netstat -ano|findstr 8089,發現當前服務器ip和master機ip正在ESTABLISH着8089端口
瀏覽器輸入master機ip:8089/export/prometheus可查看到prometheus數據
3.運行負載機:go run test.go --master-host=master機ip --master-port=5557
4..瀏覽器輸入master機ip:8089,輸入總user數+ramp up數,開始壓測
5.瀏覽器打開服務器ip:3000,查看儀表盤,正常顯示當前locust的執行數據