K6

K6 | InfunxDB + Grafana

如何使用 Grafana 可视化 k6 性能测试指标?

Posted by Haauleon on May 25, 2021

背景

  Grafana 是开源的可视化和分析软件,它支持可以查询、可视化、警报和浏览数据指标。是一个提供将时间序列数据库(TSDB)数据转换为精美的图形和可视化效果的工具。

  K6 可以将测试结果指标上载到 InfluxDB 实例,而 Grafana 可以从 InfluxDB 查询 k6 性能测试指标。



使用

  该博文内所有的下载安装运行均在 ubuntu 环境下进行。

一、安装并运行 infuxdata
1
2
3
4
5
6
7
8
安装
$ wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
$ source /etc/os-release
$ echo "deb https://repos.influxdata.com/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
$ sudo apt-get update && sudo apt-get install influxdb

运行
$ sudo service influxdb start



二、安装并运行 grafana
1
2
3
4
5
6
7
8
9
10
11
安装
$ sudo apt-get install -y apt-transport-https
$ sudo apt-get install -y software-properties-common wget
$ wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
$ echo "deb https://packages.grafana.com/enterprise/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
$ sudo apt-get update
$ sudo apt-get install grafana-enterprise

运行
$ sudo service grafana-server start
$ sudo service grafana-server status



三、配置 grafana

(1)进入 grafana 首页

  浏览器进入 http://localhost:3000,初始登录账户:admin/admin。


(2)下载模板

  grafana 提供了很多仪表盘模板,进入模板页面 https://grafana.com/grafana/dashboards?search=k6,选择一个模板并点击进入,然后复制模板 id。


(3)添加数据来源

  进入数据资源页面 http://localhost:3000/datasources,添加类型为 influxdb 的数据配置

配置信息如下:


(4)进入导入模板页面

http://localhost:3000/dashboard/import



四、运行脚本

使用命令:
k6 run -o influxdb=http://localhost:8086/mydb test-get.js

mydb 是 influxbd 的一个自定义名称的数据库,该数据库在 grafana 添加 influxdb 数据来源时已进行配置。



五、进入监控页面

进入性能测试监控概览页面:
http://localhost:3000/d/m5L_jo3Mk/k6-load-testing-results