Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JHttpUserScenarioInvocationListener.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker.scenario;
2 
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;
10 
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentSkipListSet;
17 
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;
38 
45 public class JHttpUserScenarioInvocationListener extends ServicesAware implements Provider<InvocationListener> {
46  private final Set<String> createdMetrics = new ConcurrentSkipListSet<>();
47  private List<MetricAggregatorProvider> latencyAggregatorProviders = new ArrayList<>();
48 
50 
52  this.latencyAggregatorProviders = newArrayList(builder.latencyAggregatorProviders);
53  }
54 
55  public static Builder builder() {return new Builder();}
56 
57  public static class Builder {
58  Set<MetricAggregatorProvider> latencyAggregatorProviders = new HashSet<>();
59 
61  this.latencyAggregatorProviders.addAll(newArrayList(STD_DEV_AGGREGATOR, AVG_AGGREGATOR));
62  return this;
63  }
64 
66  this.latencyAggregatorProviders.addAll(newArrayList(MAX_AGGREGATOR, MIN_AGGREGATOR));
67  return this;
68  }
69 
70  public Builder withLatencyPercentileAggregators(Double percentile, Double... percentiles) {
71  this.latencyAggregatorProviders.add(PERCENTILE_AGGREGATOR(percentile));
72  Arrays.stream(percentiles).forEach(p -> this.latencyAggregatorProviders.add(PERCENTILE_AGGREGATOR(p)));
73  return this;
74  }
75 
77  return new JHttpUserScenarioInvocationListener(this);
78  }
79  }
80 
81  @Override
82  protected void init() {}
83 
84  @Override
85  public InvocationListener provide() {
86  return new InvocationListener() {
87  @Override
88  public void onStart(InvocationInfo invocationInfo) { }
89 
90  @Override
91  public void onSuccess(InvocationInfo invocationInfo) {
92  if (invocationInfo.getResult() != null) {
93  JHttpUserScenarioInvocationResult invocationResult = ((JHttpUserScenarioInvocationResult) invocationInfo.getResult());
94  List<JHttpUserScenarioStepInvocationResult> stepInvocationResults = invocationResult.getStepInvocationResults();
95  String scenarioId = generateScenarioId(invocationResult.getScenarioId());
96 
97  // Create & save scenario metrics
98  if (!createdMetrics.contains(scenarioId)) {
99  createdMetrics.add(scenarioId);
100  createScenarioMetricDescriptions(scenarioId, invocationResult.getScenarioDisplayName());
101  }
102  getMetricService().saveValue(generateMetricId(scenarioId, ITERATION_SAMPLES_ID), 1);
103  getMetricService().saveValue(generateMetricId(scenarioId, LATENCY_ID),
104  invocationInfo.getDuration() / 1000.0); // ms -> s
105  getMetricService().saveValue(generateMetricId(scenarioId, SUCCESS_RATE_ID), invocationResult.getSucceeded() ? 1 : 0);
106 
107 
108  // Create & save step metrics
109  Integer stepIndex = 1; // index 0 for scenario (not steps) metrics
110  for (JHttpUserScenarioStepInvocationResult stepResult : stepInvocationResults) {
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()));
115  }
116  getMetricService().saveValue(generateMetricId(scenarioStepId, LATENCY_ID),
117  stepResult.getLatency().doubleValue() / 1000); // ms -> s
118  getMetricService().saveValue(generateMetricId(scenarioStepId, SUCCESS_RATE_ID), stepResult.getSucceeded() ? 1 : 0);
119  getMetricService().saveValue(generateMetricId(scenarioStepId, ITERATION_SAMPLES_ID), 1);
120 
121  stepIndex++;
122  }
123  }
124  }
125 
126  private void createScenarioMetricDescriptions(String scenarioId, String scenarioDisplayName) {
127  getMetricService().createMetric(
128  new MetricDescription(generateMetricId(scenarioId, ITERATION_SAMPLES_ID)).
129  displayName(generateMetricDisplayName(scenarioDisplayName, ITERATIONS_SAMPLES)).
130  addAggregator(SUM_AGGREGATOR));
131 
132  getMetricService().createMetric(
133  new MetricDescription(generateMetricId(scenarioId, LATENCY_ID)).
134  displayName(generateMetricDisplayName(scenarioDisplayName, LATENCY_SEC)).
135  addAggregator(AVG_AGGREGATOR));
136 
137  getMetricService().createMetric(
138  new MetricDescription(generateMetricId(scenarioId, SUCCESS_RATE_ID)).
139  displayName(generateMetricDisplayName(scenarioDisplayName, SUCCESS_RATE)).
140  plotData(true).
141  addAggregator(SUCCESS_AGGREGATOR).
142  addAggregator(FAILS_AGGREGATOR));
143  }
144 
145  private void createScenarioStepMetricDescriptions(String scenarioStepId, String scenarioStepDisplayName) {
146  MetricDescription metricDescription =
147  new MetricDescription(generateMetricId(scenarioStepId,LATENCY_ID)).
148  displayName(generateMetricDisplayName(scenarioStepDisplayName,LATENCY_SEC))
149  .plotData(true);
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);
154 
155  getMetricService().createMetric(
156  new MetricDescription(generateMetricId(scenarioStepId, ITERATION_SAMPLES_ID)).
157  displayName(generateMetricDisplayName(scenarioStepDisplayName, ITERATIONS_SAMPLES)).
158  addAggregator(SUM_AGGREGATOR));
159 
160  getMetricService().createMetric(
161  new MetricDescription(generateMetricId(scenarioStepId, SUCCESS_RATE_ID)).
162  displayName(generateMetricDisplayName(scenarioStepDisplayName, SUCCESS_RATE)).
163  plotData(true).
164  addAggregator(SUCCESS_AGGREGATOR).
165  addAggregator(FAILS_AGGREGATOR));
166  }
167 
168  @Override
169  public void onFail(InvocationInfo invocationInfo, InvocationException e) {
170  //TODO: JFG-1122
171  }
172 
173  @Override
174  public void onError(InvocationInfo invocationInfo, Throwable error) {
175  //TODO: JFG-1122
176  }
177  };
178  }
179 }