Skip to content

Commit a5bc55f

Browse files
committed
basic framework for performance tests (closes #119)
1 parent 0e46a2e commit a5bc55f

3 files changed

Lines changed: 200 additions & 2 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.wickedsource.coderadar.performance;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.wickedsource.coderadar.analyzer.rest.analyzerconfiguration.AnalyzerConfigurationResource;
6+
import org.wickedsource.coderadar.analyzingjob.rest.AnalyzingJobResource;
7+
import org.wickedsource.coderadar.filepattern.domain.FileSetType;
8+
import org.wickedsource.coderadar.filepattern.rest.FilePatternDTO;
9+
import org.wickedsource.coderadar.filepattern.rest.FilePatternResource;
10+
import org.wickedsource.coderadar.project.domain.InclusionType;
11+
import org.wickedsource.coderadar.project.domain.VcsType;
12+
import org.wickedsource.coderadar.project.rest.ProjectResource;
13+
import org.wickedsource.coderadar.restclient.CoderadarRestClient;
14+
15+
import java.util.Arrays;
16+
import java.util.Date;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
public class JobPerformanceTestRunner {
21+
22+
private static final List<String> MONITORED_METRICS = Arrays.asList(
23+
"coderadar.CommitAnalyzer.commits.oneMinuteRate",
24+
"coderadar.CommitAnalyzer.files.oneMinuteRate",
25+
"coderadar.CommitMetaDataScanner.commits.oneMinuteRate",
26+
"coderadar.CommitToFileAssociator.commits.oneMinuteRate",
27+
"coderadar.CommitToFileAssociator.files.oneMinuteRate",
28+
"coderadar.FileMetadataScanner.commits.oneMinuteRate",
29+
"coderadar.FileMetadataScanner.files.oneMinuteRate"
30+
);
31+
32+
private final static int DURATION_IN_SECONDS = 600;
33+
34+
private final static int MONITORING_INTERVAL_IN_SECONDS = 5;
35+
36+
private static Logger logger = LoggerFactory.getLogger(JobPerformanceTestRunner.class);
37+
38+
public static void main(String[] args) throws InterruptedException {
39+
CoderadarRestClient client = new CoderadarRestClient("http://localhost:8080");
40+
41+
createProject(client);
42+
addFilePatterns(client);
43+
addAnalyzerConfigurations(client);
44+
addAnalyzingJob(client);
45+
46+
MetricsMonitor monitor = new MetricsMonitor();
47+
for(String metric : MONITORED_METRICS) {
48+
monitor.addMetric(metric);
49+
}
50+
51+
logger.info("monitoring performance metrics every {} seconds for a duration of {} seconds ... be patient", MONITORING_INTERVAL_IN_SECONDS, DURATION_IN_SECONDS);
52+
Map<String, Number> maxMetrics = monitor.startMonitoring(client, DURATION_IN_SECONDS, MONITORING_INTERVAL_IN_SECONDS);
53+
logger.info("performance test concluded ... see results in 'metrics_report.csv'");
54+
logger.info("max metric values:");
55+
for(String metric : maxMetrics.keySet()) {
56+
logger.info(String.format("%-60s: %f", metric, maxMetrics.get(metric)));
57+
}
58+
59+
}
60+
61+
private static ProjectResource createProject(CoderadarRestClient client) {
62+
ProjectResource projectResource = new ProjectResource();
63+
projectResource.setVcsType(VcsType.GIT);
64+
projectResource.setName("coderadar");
65+
projectResource.setVcsUrl("https://github.com/reflectoring/coderadar.git");
66+
return client.createProject(projectResource);
67+
}
68+
69+
private static FilePatternResource addFilePatterns(CoderadarRestClient client){
70+
FilePatternResource patterns = new FilePatternResource();
71+
FilePatternDTO pattern = new FilePatternDTO();
72+
pattern.setInclusionType(InclusionType.INCLUDE);
73+
pattern.setFileSetType(FileSetType.SOURCE);
74+
pattern.setPattern("**/src/main/java/**/*.java");
75+
patterns.addFilePattern(pattern);
76+
return client.setFilePatterns(1L, patterns);
77+
}
78+
79+
private static void addAnalyzerConfigurations(CoderadarRestClient client){
80+
client.addAnalyzerConfiguration(1L, new AnalyzerConfigurationResource("org.wickedsource.coderadar.analyzer.checkstyle.CheckstyleSourceCodeFileAnalyzerPlugin", true));
81+
client.addAnalyzerConfiguration(1L, new AnalyzerConfigurationResource("org.wickedsource.coderadar.analyzer.loc.LocAnalyzerPlugin", true));
82+
}
83+
84+
private static AnalyzingJobResource addAnalyzingJob(CoderadarRestClient client){
85+
AnalyzingJobResource job = new AnalyzingJobResource();
86+
job.setActive(true);
87+
job.setFromDate(new Date(0));
88+
job.setRescan(true);
89+
return client.addAnalyzingJob(1L, job);
90+
}
91+
92+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.wickedsource.coderadar.performance;
2+
3+
import org.apache.commons.lang.ArrayUtils;
4+
import org.joda.time.format.DateTimeFormat;
5+
import org.joda.time.format.DateTimeFormatter;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.wickedsource.coderadar.restclient.CoderadarRestClient;
9+
10+
import java.util.*;
11+
import java.util.stream.Collectors;
12+
13+
/**
14+
* Monitors the monitoring metrics REST endpoint of the coderadar application over a specified
15+
* duration and regularly outputs the current metric values into a CSV file for later analysis.
16+
*/
17+
public class MetricsMonitor {
18+
19+
private static Logger logger = LoggerFactory.getLogger(MetricsMonitor.class);
20+
21+
private List<String> monitoredMetrics = new ArrayList<>();
22+
23+
private String logPattern = "{};";
24+
25+
private Map<String, Number> maxMetricValues = new HashMap<>();
26+
27+
private DateTimeFormatter dateFormat = DateTimeFormat.shortDateTime().withLocale(Locale.ENGLISH);
28+
29+
/**
30+
* Add a metric to be monitored.
31+
*
32+
* @param metricName the name of the metric to monitor.
33+
*/
34+
public void addMetric(String metricName) {
35+
this.monitoredMetrics.add(metricName);
36+
logPattern += "{};";
37+
}
38+
39+
/**
40+
* Starts monitoring all metrics that have been added using {@link #addMetric(String)}.
41+
*
42+
* @param client the client to the coderadar REST interface
43+
* @param durationInSeconds the duration in seconds defining how long to monitor the metrics.
44+
* @param monitoringIntervalInSeconds the interval in seconds at which to output the current metric values as a semicolon-separated line.
45+
* The output format and target can be configured for the Logger org.wickedsource.coderadar.performance.MetricMonitor
46+
* in the logging configuration.
47+
* @return a Map containing the maximum value for each metric over the whole duration of the monitoring process.
48+
*/
49+
public Map<String, Number> startMonitoring(CoderadarRestClient client, int durationInSeconds, int monitoringIntervalInSeconds) throws InterruptedException {
50+
logHeaderLine();
51+
long passedSeconds = 0;
52+
while (passedSeconds < durationInSeconds) {
53+
Map<String, Number> metricsSnapshot = client.getMonitoringMetrics();
54+
logMetricsSnapshot(metricsSnapshot);
55+
updateMaxMetricValues(metricsSnapshot);
56+
Thread.sleep(monitoringIntervalInSeconds * 1000);
57+
passedSeconds += monitoringIntervalInSeconds;
58+
}
59+
return maxMetricValues;
60+
}
61+
62+
private void logHeaderLine() {
63+
String headerLine = "date;";
64+
for (String metric : monitoredMetrics) {
65+
headerLine += metric + ";";
66+
}
67+
logger.info(headerLine);
68+
}
69+
70+
private void logMetricsSnapshot(Map<String, Number> metrics) {
71+
List<Number> monitoredMetricValues = metrics.entrySet().stream()
72+
.filter(item -> monitoredMetrics.contains(item.getKey()))
73+
.map(item -> item.getValue())
74+
.collect(Collectors.toList());
75+
76+
Object[] args = ArrayUtils.addAll(new Object[]{dateFormat.print(System.currentTimeMillis())}, monitoredMetricValues.toArray());
77+
logger.info(logPattern, args);
78+
}
79+
80+
private void updateMaxMetricValues(Map<String, Number> snapshot) {
81+
for (String metric : monitoredMetrics) {
82+
Number oldValue = maxMetricValues.get(metric);
83+
Number newValue = snapshot.get(metric);
84+
85+
if (oldValue == null) {
86+
oldValue = 0;
87+
}
88+
89+
if (newValue == null) {
90+
newValue = 0;
91+
}
92+
93+
maxMetricValues.put(metric, max(oldValue, newValue));
94+
}
95+
}
96+
97+
private Number max(Number n1, Number n2) {
98+
if (n1.doubleValue() > n2.doubleValue()) {
99+
return n1;
100+
} else {
101+
return n2;
102+
}
103+
}
104+
105+
106+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Jan 03 15:59:55 UTC 2017
1+
#Sat Feb 11 19:32:48 CET 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 commit comments

Comments
 (0)