Skip to content

Commit d639a06

Browse files
Add support for prometheus listener
1 parent d5b75ae commit d639a06

File tree

11 files changed

+3437
-0
lines changed

11 files changed

+3437
-0
lines changed

docs/guide/reporting/real-time/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ When running tests with JMeter (and in particular with jmeter-java-dsl) a usual
55
<!-- @include: influxdb.md -->
66
<!-- @include: graphite.md -->
77
<!-- @include: elasticsearch.md -->
8+
<!-- @include: prometheus.md -->
89
<!-- @include: datadog.md -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#### Prometheus
2+
3+
As in previous scenarios, you can also use Prometheus and Grafana.
4+
5+
To use the module, you will need to include the following dependency in your project:
6+
7+
:::: code-group
8+
::: code-group-item Maven
9+
```xml
10+
<dependency>
11+
<groupId>us.abstracta.jmeter</groupId>
12+
<artifactId>jmeter-java-dsl-prometheus</artifactId>
13+
<version>1.27</version>
14+
<scope>test</scope>
15+
</dependency>
16+
```
17+
:::
18+
::: code-group-item Gradle
19+
```groovy
20+
testImplementation 'us.abstracta.jmeter:jmeter-java-dsl-prometheus:1.27'
21+
```
22+
:::
23+
::::
24+
25+
And use provided `prometheusListener()` method like in this example:
26+
27+
```java
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
30+
import static us.abstracta.jmeter.javadsl.prometheus.DslPrometheusListener.*;
31+
32+
import java.io.IOException;
33+
import java.time.Duration;
34+
import org.junit.jupiter.api.Test;
35+
import us.abstracta.jmeter.javadsl.core.TestPlanStats;
36+
37+
public class PerformanceTest {
38+
39+
@Test
40+
public void testPerformance() throws IOException {
41+
TestPlanStats stats = testPlan(
42+
threadGroup(2, 10,
43+
httpSampler("http://my.service")
44+
),
45+
prometheusListener()
46+
).run();
47+
assertThat(stats.overall().sampleTimePercentile99()).isLessThan(Duration.ofSeconds(5));
48+
}
49+
50+
}
51+
```
52+
53+
As in previous cases, you can to try it locally by running `docker-compose up` inside [this directory](/docs/guide/reporting/real-time/prometheus). After containers are started, you can follow the same steps as in previous scenarios.
54+
55+
::: warning
56+
Use the provided `docker-compose` settings for local tests only. It uses weak credentials and is not properly configured for production purposes.
57+
:::
58+
59+
Check [DslPrometheusListener](/jmeter-java-dsl-prometheus/src/main/java/us/abstracta/jmeter/javadsl/prometheus/DslPrometheusListener.java) for details on listener settings.
60+
61+
Here is an example that shows the default settings used by `prometheusListener`:
62+
63+
```java
64+
import us.abstracta.jmeter.javadsl.prometheus.DslPrometheusListener.PrometheusMetric;
65+
...
66+
prometheusListener()
67+
.metrics(
68+
PrometheusMetric.responseTime("ResponseTime", "the response time of samplers")
69+
.labels(PrometheusMetric.SAMPLE_LABEL, PrometheusMetric.RESPONSE_CODE)
70+
.quantile(0.75, 0.5)
71+
.quantile(0.95, 0.1)
72+
.quantile(0.99, 0.01)
73+
.maxAge(Duration.ofMinutes(1)),
74+
PrometheusMetric.successRatio("Ratio", "the success ratio of samplers")
75+
.labels(PrometheusMetric.SAMPLE_LABEL, PrometheusMetric.RESPONSE_CODE)
76+
)
77+
.port(9270)
78+
.host("0.0.0.0")
79+
.endWait(Duration.ofSeconds(10))
80+
...
81+
```
82+
> Note that the default settings are different from the used JMeter Prometheus Plugin, to allow easier usage and avoid missing metrics at the end of test plan execution.
83+
84+
::: tip
85+
When configuring the `prometheusListener` always consider setting a `endWait` that is greater thant the Prometheus Server configured `scrape_interval` to avoid missing metrics at the end of test plan execution (e.g.: 2x the scrape interval value).
86+
:::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
services:
2+
prometheus:
3+
image: prom/prometheus:v2.51.2
4+
ports:
5+
- '9090:9090'
6+
volumes:
7+
- prometheus-data:/prometheus
8+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
9+
grafana:
10+
image: grafana/grafana:10.4.2
11+
ports:
12+
- '3000:3000'
13+
volumes:
14+
- grafana-data:/var/lib/grafana
15+
- ./grafana-provisioning/:/etc/grafana/provisioning
16+
depends_on:
17+
- prometheus
18+
environment:
19+
- GF_SECURITY_ADMIN_USER=admin
20+
- GF_SECURITY_ADMIN_PASSWORD=1234
21+
- GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/etc/grafana/provisioning/dashboards/jmeter.json
22+
volumes:
23+
prometheus-data:
24+
grafana-data:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: 1
2+
providers:
3+
- name: JMeter Dashboards
4+
allowUiUpdates: true
5+
options:
6+
path: /etc/grafana/provisioning/dashboards

0 commit comments

Comments
 (0)