|
| 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 | +::: |
0 commit comments