-
Notifications
You must be signed in to change notification settings - Fork 69
/
InternalPerformanceMeter.java
291 lines (251 loc) · 12.1 KB
/
InternalPerformanceMeter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors: IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.test.internal.performance;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.test.internal.performance.data.DataPoint;
import org.eclipse.test.internal.performance.data.Dim;
import org.eclipse.test.internal.performance.data.Sample;
import org.eclipse.test.internal.performance.db.Variations;
import org.eclipse.test.internal.performance.eval.StatisticsSession;
import org.eclipse.test.internal.performance.eval.StatisticsUtil;
import org.eclipse.test.internal.performance.eval.StatisticsUtil.Percentile;
import org.eclipse.test.performance.Dimension;
import org.eclipse.test.performance.PerformanceMeter;
public abstract class InternalPerformanceMeter extends PerformanceMeter {
private static class DimensionComparator implements Comparator<Dim> {
@Override
public int compare(Dim o1, Dim o2) {
return o1.getId() - o2.getId();
}
}
public static final int AVERAGE = -3;
public static final int SIZE = -4;
public static final int STDEV = -5;
public static final int BEFORE = 0;
public static final int AFTER = 1;
protected static final String VERBOSE_PERFORMANCE_METER_PROPERTY = "InternalPrintPerformanceResults"; //$NON-NLS-1$
protected static final String CSV_PERFORMANCE_METER_PROPERTY = "InternalPrintCsvPerformanceResults"; //$NON-NLS-1$
private static final String DUMP_PERFORMANCE_DATA_PROPERTY = "InternalWriteRawPerformanceData"; //$NON-NLS-1$
private String fScenarioId;
private String fShortName;
private Dimension[] fSummaryDimensions;
private boolean fSummaryIsGlobal;
private int fCommentType;
private String fComment;
public InternalPerformanceMeter(String scenarioId) {
fScenarioId = scenarioId;
}
@Override
public void dispose() {
fScenarioId = null;
}
public abstract Sample getSample();
/**
* Answer the scenario ID.
*
* @return the scenario ID
*/
public String getScenarioName() {
return fScenarioId;
}
@Override
public void commit() {
Sample sample = getSample();
if (sample != null) {
if (fSummaryDimensions != null) {
sample.tagAsSummary(fSummaryIsGlobal, fShortName, fSummaryDimensions, fCommentType, fComment);
} else if (this.fComment != null) {
sample.setComment(this.fCommentType, this.fComment);
}
Variations variations = PerformanceTestPlugin.getVariations();
if (System.getProperty(VERBOSE_PERFORMANCE_METER_PROPERTY) != null) {
printSample(System.out, sample);
// printSampleCSV(System.out, sample);
}
String property = System.getProperty(CSV_PERFORMANCE_METER_PROPERTY);
if (property != null) {
File output = new File(property);
try (PrintStream ps = new PrintStream(new FileOutputStream(output))) {
printSampleCSV(ps, sample);
} catch (IOException e) {
// It turns out you cannot write.
e.printStackTrace();
}
}
property = System.getProperty(DUMP_PERFORMANCE_DATA_PROPERTY);
if (property != null) {
File output = new File(property);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(output, true))) {
oos.writeObject(fScenarioId);
oos.writeObject(variations);
oos.writeObject(sample);
} catch (IOException e) {
// It turns out you cannot write.
e.printStackTrace();
}
}
}
}
private void printSample(PrintStream ps, Sample sample) {
ps.print("Scenario '" + getScenarioName() + "' "); //$NON-NLS-1$ //$NON-NLS-2$
DataPoint[] dataPoints = sample.getDataPoints();
if (dataPoints.length > 0) {
StatisticsSession s = new StatisticsSession(dataPoints);
Dim[] dimensions = dataPoints[0].getDimensions();
Arrays.sort(dimensions, new DimensionComparator());
if (dimensions.length > 0) {
List<Dim> badDimensions = new ArrayList<>();
long n = s.getCount(dimensions[0]);
MessageFormat format = new MessageFormat("({0,number,percent} in [{1}, {2}])"); //$NON-NLS-1$
String spaces = " "; //$NON-NLS-1$
ps.println("(average over " + n + " samples):"); //$NON-NLS-1$ //$NON-NLS-2$
for (Dim dimension : dimensions) {
double mean = s.getAverage(dimension);
String nameString = " " + dimension.getName() + ":"; //$NON-NLS-1$ //$NON-NLS-2$
String meanString = dimension.getDisplayValue(mean);
int align = firstNonDigit(meanString);
int endIndex = 30 - align - nameString.length();
if (endIndex > 0)
meanString = spaces.substring(0, endIndex) + meanString;
align = nameString.length() + meanString.length();
Percentile percentile = StatisticsUtil.T95;
double[] confidenceInterval = s.getConfidenceInterval(dimension, percentile);
StringBuffer printBuffer;
if (n <= 2) {
printBuffer = new StringBuffer(" (no confidence)"); //$NON-NLS-1$
} else {
printBuffer = new StringBuffer();
int ns = align;
while (ns++ < 40)
printBuffer.append(' ');
printBuffer
.append(format.format(new Object[] { Double.valueOf(percentile.inside()),
dimension.getDisplayValue(confidenceInterval[0]),
dimension.getDisplayValue(confidenceInterval[1]) }));
}
align += printBuffer.length();
try {
while (align++ < 70)
printBuffer.append(' ');
printBuffer.append(checkSampleSize(s, sample, dimension));
}
catch (CoreException x) {
badDimensions.add(dimension);
continue;
}
ps.print(nameString);
ps.print(meanString);
ps.println(printBuffer);
}
if (!badDimensions.isEmpty()) {
ps.print(" Dimensions with unusable statistical properties: "); //$NON-NLS-1$
for (Iterator<Dim> iter = badDimensions.iterator(); iter.hasNext();) {
Dim dimension = iter.next();
ps.print(dimension.getName());
if (iter.hasNext())
ps.print(", "); //$NON-NLS-1$
}
ps.println();
}
}
}
ps.println();
}
private String checkSampleSize(StatisticsSession s, Sample sample, Dim dimension) throws CoreException {
long sampleSize = s.getCount(dimension);
double stdev = s.getStddev(dimension);
double mean = s.getAverage(dimension);
if (stdev == 0)
return ""; //$NON-NLS-1$
// measurable effect size
// sampleSize= 16 * stdev^2 / effect^2
double effectSize = 4 * Math.sqrt(stdev * stdev / sampleSize);
double base;
String baseName;
if (stdev > Math.abs(mean)) {
base = stdev;
baseName = "stdev"; //$NON-NLS-1$
} else {
base = Math.abs(mean);
baseName = "mean"; //$NON-NLS-1$
}
double fivePercentEffect = 0.05 * base;
long requiredSampleSizeForFivePercentEffect = Math.round(16 * stdev * stdev / fivePercentEffect / fivePercentEffect + 0.5);
// if (requiredSampleSizeForFivePercentEffect > 1000 || Double.isNaN(stdev))
// throw new CoreException(new Status(IStatus.OK, "org.eclipse.text.performance", IStatus.OK, "no message", null)); //$NON-NLS-1$ //$NON-NLS-2$
NumberFormat numberInstance = NumberFormat.getNumberInstance();
numberInstance.setMaximumFractionDigits(1);
numberInstance.setMinimumFractionDigits(1);
String measurableMsg = " Measurable effect: " + dimension.getDisplayValue(effectSize) + " (" + numberInstance.format(effectSize / stdev) + " SDs)"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (fivePercentEffect < effectSize)
measurableMsg += " (required sample size for an effect of 5% of " + baseName + ": " + requiredSampleSizeForFivePercentEffect + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return measurableMsg;
}
private int firstNonDigit(String string) {
int length = string.length();
for (int i = 0; i < length; i++)
if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '-' && string.charAt(i) != '.')
return i;
return length;
}
void printSampleCSV(PrintStream ps, Sample sample) {
final char SEPARATOR = '\t';
DataPoint[] dataPoints = sample.getDataPoints();
if (dataPoints.length > 0) {
Dim[] dimensions = dataPoints[0].getDimensions();
Arrays.sort(dimensions, new DimensionComparator());
if (dimensions.length > 0) {
/* print dimensions */
for (Dim dimension : dimensions) {
ps.print(dimension.getName());
ps.print(SEPARATOR);
}
ps.println("scenario"); //$NON-NLS-1$
for (int p = 0; p < dataPoints.length - 1; p += 2) {
DataPoint before = dataPoints[p];
DataPoint after = dataPoints[p + 1];
for (Dim dimension : dimensions) {
long valBefore = before.getScalar(dimension).getMagnitude();
long valAfter = after.getScalar(dimension).getMagnitude();
ps.print(valAfter - valBefore);
ps.print(SEPARATOR);
}
ps.print(sample.getShortname() != null ? sample.getShortname() : sample.getScenarioID());
ps.println();
}
ps.println();
}
}
}
public void tagAsSummary(boolean global, String shortName, Dimension[] dims) {
fSummaryIsGlobal = global;
fShortName = shortName;
fSummaryDimensions = dims;
}
public void setComment(int commentType, String comment) {
fCommentType = commentType;
fComment = comment;
}
}