1 package com.griddynamics.jagger.invoker.scenario;
3 import com.griddynamics.jagger.engine.e1.Provider;
4 import com.griddynamics.jagger.engine.e1.collector.MetricAggregatorProvider;
5 import com.griddynamics.jagger.engine.e1.collector.MetricDescription;
6 import com.griddynamics.jagger.engine.e1.collector.invocation.InvocationInfo;
7 import com.griddynamics.jagger.engine.e1.collector.invocation.InvocationListener;
8 import com.griddynamics.jagger.engine.e1.services.ServicesAware;
9 import com.griddynamics.jagger.invoker.InvocationException;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.HashSet;
14 import java.util.List;
16 import java.util.concurrent.ConcurrentSkipListSet;
18 import static com.google.common.collect.Lists.newArrayList;
19 import static com.google.common.collect.Sets.newHashSet;
20 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.AVG_AGGREGATOR;
21 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.FAILS_AGGREGATOR;
22 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.MAX_AGGREGATOR;
23 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.MIN_AGGREGATOR;
24 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.PERCENTILE_AGGREGATOR;
25 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.STD_DEV_AGGREGATOR;
26 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.SUCCESS_AGGREGATOR;
27 import static com.griddynamics.jagger.invoker.scenario.DefaultAggregatorsProvider.SUM_AGGREGATOR;
28 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.ITERATIONS_SAMPLES;
29 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.ITERATION_SAMPLES_ID;
30 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.LATENCY_ID;
31 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.LATENCY_SEC;
32 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.SUCCESS_RATE;
33 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.SUCCESS_RATE_ID;
34 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.generateMetricDisplayName;
35 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.generateMetricId;
36 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.generateScenarioId;
37 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.generateScenarioStepId;
46 private final Set<String> createdMetrics =
new ConcurrentSkipListSet<>();
47 private List<MetricAggregatorProvider> latencyAggregatorProviders =
new ArrayList<>();
52 this.latencyAggregatorProviders = newArrayList(builder.latencyAggregatorProviders);
58 Set<MetricAggregatorProvider> latencyAggregatorProviders =
new HashSet<>();
61 this.latencyAggregatorProviders.addAll(newArrayList(STD_DEV_AGGREGATOR, AVG_AGGREGATOR));
66 this.latencyAggregatorProviders.addAll(newArrayList(MAX_AGGREGATOR, MIN_AGGREGATOR));
71 this.latencyAggregatorProviders.add(PERCENTILE_AGGREGATOR(percentile));
72 Arrays.stream(percentiles).forEach(p -> this.latencyAggregatorProviders.add(PERCENTILE_AGGREGATOR(p)));
86 return new InvocationListener() {
88 public void onStart(InvocationInfo invocationInfo) { }
91 public void onSuccess(InvocationInfo invocationInfo) {
92 if (invocationInfo.getResult() != null) {
95 String scenarioId = generateScenarioId(invocationResult.
getScenarioId());
98 if (!createdMetrics.contains(scenarioId)) {
99 createdMetrics.add(scenarioId);
102 getMetricService().saveValue(generateMetricId(scenarioId, ITERATION_SAMPLES_ID), 1);
103 getMetricService().saveValue(generateMetricId(scenarioId, LATENCY_ID),
104 invocationInfo.getDuration() / 1000.0);
105 getMetricService().saveValue(generateMetricId(scenarioId, SUCCESS_RATE_ID), invocationResult.
getSucceeded() ? 1 : 0);
109 Integer stepIndex = 1;
111 String scenarioStepId = generateScenarioStepId(invocationResult.
getScenarioId(), stepResult.getStepId(), stepIndex);
112 if (!createdMetrics.contains(scenarioStepId)) {
113 createdMetrics.add(scenarioStepId);
114 createScenarioStepMetricDescriptions(scenarioStepId,String.format(
"%02d. %s ", stepIndex, stepResult.getStepDisplayName()));
116 getMetricService().saveValue(generateMetricId(scenarioStepId, LATENCY_ID),
117 stepResult.getLatency().doubleValue() / 1000);
118 getMetricService().saveValue(generateMetricId(scenarioStepId, SUCCESS_RATE_ID), stepResult.getSucceeded() ? 1 : 0);
119 getMetricService().saveValue(generateMetricId(scenarioStepId, ITERATION_SAMPLES_ID), 1);
126 private void createScenarioMetricDescriptions(String scenarioId, String scenarioDisplayName) {
127 getMetricService().createMetric(
129 displayName(generateMetricDisplayName(scenarioDisplayName, ITERATIONS_SAMPLES)).
130 addAggregator(SUM_AGGREGATOR));
132 getMetricService().createMetric(
134 displayName(generateMetricDisplayName(scenarioDisplayName, LATENCY_SEC)).
135 addAggregator(AVG_AGGREGATOR));
137 getMetricService().createMetric(
139 displayName(generateMetricDisplayName(scenarioDisplayName, SUCCESS_RATE)).
141 addAggregator(SUCCESS_AGGREGATOR).
142 addAggregator(FAILS_AGGREGATOR));
145 private void createScenarioStepMetricDescriptions(String scenarioStepId, String scenarioStepDisplayName) {
148 displayName(generateMetricDisplayName(scenarioStepDisplayName,LATENCY_SEC))
150 if (latencyAggregatorProviders.isEmpty())
151 latencyAggregatorProviders.addAll(newHashSet(MAX_AGGREGATOR, MIN_AGGREGATOR, STD_DEV_AGGREGATOR, AVG_AGGREGATOR));
152 latencyAggregatorProviders.forEach(metricDescription::addAggregator);
153 getMetricService().createMetric(metricDescription);
155 getMetricService().createMetric(
157 displayName(generateMetricDisplayName(scenarioStepDisplayName, ITERATIONS_SAMPLES)).
158 addAggregator(SUM_AGGREGATOR));
160 getMetricService().createMetric(
162 displayName(generateMetricDisplayName(scenarioStepDisplayName, SUCCESS_RATE)).
164 addAggregator(SUCCESS_AGGREGATOR).
165 addAggregator(FAILS_AGGREGATOR));
169 public void onFail(InvocationInfo invocationInfo, InvocationException e) {
174 public void onError(InvocationInfo invocationInfo, Throwable error) {