aboutsummaryrefslogtreecommitdiff
path: root/basebuilder-3.6.2/org.eclipse.releng.basebuilder/plugins/org.eclipse.test.performance.ui/src/org/eclipse/test/internal/performance/results/utils/Util.java
blob: 5199381c3d9b7faf4864a2d76d21ed2197b9f063 (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.test.internal.performance.results.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.test.internal.performance.results.db.BuildResults;
import org.eclipse.test.internal.performance.results.db.DB_Results;

/**
 * Utility methods for statistics. Got from org.eclipse.test.performance
 * framework
 */
public final class Util implements IPerformancesConstants {

	// Percentages
	public static final NumberFormat PERCENTAGE_FORMAT = NumberFormat.getPercentInstance(Locale.US);
	static {
		PERCENTAGE_FORMAT.setMaximumFractionDigits(2);
	}
	public static final NumberFormat DOUBLE_FORMAT = NumberFormat.getNumberInstance(Locale.US);
	static {
		DOUBLE_FORMAT.setMaximumFractionDigits(2);
	}

	// Strings
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");

	// Build prefixes
	public static final List ALL_BUILD_PREFIXES = new ArrayList(3);
	static {
		ALL_BUILD_PREFIXES.add("I");
		ALL_BUILD_PREFIXES.add("N");
		ALL_BUILD_PREFIXES.add("M");
	}
	public static final List BUILD_PREFIXES = new ArrayList(2);
	static {
		BUILD_PREFIXES.add("I");
		BUILD_PREFIXES.add("N");
	}
	public static final List MAINTENANCE_BUILD_PREFIXES = new ArrayList(2);
	static {
		MAINTENANCE_BUILD_PREFIXES.add("I");
		MAINTENANCE_BUILD_PREFIXES.add("M");
	}
	public static final List BASELINE_BUILD_PREFIXES = new ArrayList(1);
	static {
		BASELINE_BUILD_PREFIXES.add(DB_Results.getDbBaselinePrefix());
	}

	// Milestones constants
	private static String[] MILESTONES;
	public static final BuildDateComparator BUILD_DATE_COMPARATOR = new BuildDateComparator();

static class BuildDateComparator implements Comparator {
	public int compare(Object o1, Object o2) {
		String s1 = (String) o1;
		String s2 = (String) o2;
		return getBuildDate(s1).compareTo(getBuildDate(s2));
	}
}

private static void initMilestones() {
	String version = DB_Results.getDbVersion();

	// Initialize reference version and database directory
	char mainVersion = version.charAt(1);
	char minorVersion = version.charAt(2);

	// Initialize milestones
	if (mainVersion == '3') {
		switch (minorVersion) {
			case '3':
			case '4':
				throw new RuntimeException("Version "+mainVersion+'.'+minorVersion+" is no longer supported!");
			case '5':
				MILESTONES = V35_MILESTONES;
				break;
			case '6':
				MILESTONES = V36_MILESTONES;
				break;
			default:
				throw new RuntimeException("Version "+mainVersion+'.'+minorVersion+" is not supported yet!");
		}
	} else {
		throw new RuntimeException("Version "+mainVersion+'.'+minorVersion+" is not supported yet!");
	}
}

// Static information for time and date
public static final int ONE_MINUTE = 60000;
public static final long ONE_HOUR = 3600000L;
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); //$NON-NLS-1$

/**
 * Compute the student t-test values.
 *
 * @see "http://en.wikipedia.org/wiki/Student's_t-test"
 *
 * @param baselineResults The baseline build
 * @param buildResults The current build
 * @return The student t-test value as a double.
 */
public static double computeTTest(BuildResults baselineResults, BuildResults buildResults) {

	double ref = baselineResults.getValue();
	double val = buildResults.getValue();

	double delta = ref - val;
	long dfRef = baselineResults.getCount() - 1;
	double sdRef = baselineResults.getDeviation();
	long dfVal = buildResults.getCount() - 1;
	double sdVal = buildResults.getDeviation();
	// TODO if the stdev's are not sufficiently similar, we have to take a
	// different approach

	if (!Double.isNaN(sdRef) && !Double.isNaN(sdVal) && dfRef > 0 && dfVal > 0) {
		long df = dfRef + dfVal;
		double sp_square = (dfRef * sdRef * sdRef + dfVal * sdVal * sdVal) / df;

		double se_diff = Math.sqrt(sp_square * (1.0 / (dfRef + 1) + 1.0 / (dfVal + 1)));
		double t = Math.abs(delta / se_diff);
		return t;
	}

	return -1;
}

/**
 * Copy a file to another location.
 *
 * @param src the source file.
 * @param dest the destination.
 * @return <code>true</code> if the file was successfully copied,
 * 	<code>false</code> otherwise.
 */
public static boolean copyFile(File src, File dest) {

	try {
		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dest);
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
		return false;
	} catch (IOException e) {
		e.printStackTrace();
		return false;
	}
	return true;
}

/**
 * Copy a file content to another location.
 *
 * @param in the input stream.
 * @param dest the destination.
 * @return <code>true</code> if the file was successfully copied,
 * 	<code>false</code> otherwise.
 */
public static boolean copyStream(InputStream in, File dest) {

	try {
		OutputStream out = new FileOutputStream(dest);
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
		return false;
	} catch (IOException e) {
		e.printStackTrace();
		return false;
	}
	return true;
}

/**
 * Return the build date as yyyyMMddHHmm.
 *
 * @param buildName The build name (e.g. I20090806-0100)
 * @return The date as a string.
 */
public static String getBuildDate(String buildName) {
	return getBuildDate(buildName, DB_Results.getDbBaselinePrefix());
}

/**
 * Return the build date as yyyyMMddHHmm.
 *
 * @param buildName The build name (e.g. I20090806-0100)
 * @param baselinePrefix The baseline prefix (e.g. {@link DB_Results#getDbBaselinePrefix()})
 * @return The date as a string.
 */
public static String getBuildDate(String buildName, String baselinePrefix) {

	// Baseline name
	if (baselinePrefix != null && buildName.startsWith(baselinePrefix)) {
		int length = buildName.length();
		return buildName.substring(length-12, length);
	}

	// Build name
	char first = buildName.charAt(0);
	if (first == 'N' || first == 'I' || first == 'M') { // TODO (frederic) should be buildIdPrefixes...
		return buildName.substring(1, 9)+buildName.substring(10, 14);
	}

	// Try with date format
	int length = buildName.length() - 12 /* length of date */;
	for (int i=0; i<=length; i++) {
		try {
			String substring = i == 0 ? buildName : buildName.substring(i);
			Util.DATE_FORMAT.parse(substring);
			return substring; // if no exception is raised then the substring has a correct date format => return it
		} catch(ParseException ex) {
			// skip
		}
	}
	return null;
}

/**
 * Returns the date of the milestone corresponding at the given index.
 *
 * @param index The index of the milestone
 * @return The date as a YYYYMMDD-hhmm string.
 */
public static String getMilestoneDate(int index) {
	int length = getMilestonesLength();
	if (index >= length) return null;
	int dash = MILESTONES[index].indexOf('-');
	return MILESTONES[index].substring(dash+1);
}

/**
 * Returns the milestone matching the given build name.
 *
 * @param buildName The name of the build
 * @return The milestone as a string (e.g. M1)
 */
public static String getMilestone(String buildName) {
	if (buildName != null && buildName.length() >= 12) {
		int length = getMilestonesLength();
		String buildDate = getBuildDate(buildName, DB_Results.getDbBaselinePrefix());
		for (int i=0; i<length; i++) {
			int start = MILESTONES[i].indexOf(buildDate);
			if (start > 0) {
				return MILESTONES[i];
			}
		}
	}
	return null;
}

/**
 * Returns the name the milestone matching the given build name.
 *
 * @param buildName The name of the build
 * @return The milestone name as a string (e.g. M1)
 */
public static String getMilestoneName(String buildName) {
	if (buildName != null && buildName.length() >= 12) {
		int length = getMilestonesLength();
		String buildDate = getBuildDate(buildName, DB_Results.getDbBaselinePrefix());
		for (int i=0; i<length; i++) {
			int start = MILESTONES[i].indexOf(buildDate);
			if (start > 0) {
				return MILESTONES[i].substring(0, start - 1);
			}
		}
	}
	return null;
}

/**
 * Returns whether the given build name is a milestone or not.
 *
 * @param buildName The build name
 * @return <code>true</code> if the build name matches a milestone one,
 * 	<code>false</code> otherwise.
 */
public static boolean isMilestone(String buildName) {
	return getMilestoneName(buildName) != null;
}

/**
 * Returns the name of the milestone which run after the given build name
 * or <code>null</code> if there's no milestone since the build has run.
 *
 * @param buildName The build name
 * @return <code>true</code> if the build name matches a milestone one,
 * 	<code>false</code> otherwise.
 */
public static String getNextMilestone(String buildName) {
	int length = getMilestonesLength();
	String buildDate = getBuildDate(buildName);
	for (int i=0; i<length; i++) {
		String milestoneDate = MILESTONES[i].substring(MILESTONES[i].indexOf('-')+1);
		if (milestoneDate.compareTo(buildDate) > 0) {
			return milestoneDate;
		}
	}
	return null;
}

/**
 * Return the number of milestones.
 *
 * @return The number as an int
 */
public static int getMilestonesLength() {
	if (MILESTONES == null) initMilestones();
	int length = MILESTONES.length;
	return length;
}

/**
 * @deprecated
 */
public static boolean matchPattern(String name, String pattern) {
	if (pattern.equals("*")) return true; //$NON-NLS-1$
	if (pattern.indexOf('*') < 0 && pattern.indexOf('?') < 0) {
		pattern += "*"; //$NON-NLS-1$
	}
	StringTokenizer tokenizer = new StringTokenizer(pattern, "*?", true); //$NON-NLS-1$
	int start = 0;
	String previous = ""; //$NON-NLS-1$
	while (tokenizer.hasMoreTokens()) {
		String token = tokenizer.nextToken();
		if (!token.equals("*") && !token.equals("?")) { //$NON-NLS-1$ //$NON-NLS-2$
			if (previous.equals("*")) { //$NON-NLS-1$
				int idx = name.substring(start).indexOf(token);
				if (idx < 0) return false;
				start += idx;
			} else {
				if (previous.equals("?")) start++; //$NON-NLS-1$
				if (!name.substring(start).startsWith(token)) return false;
			}
			start += token.length();
		}
		previous = token;
	}
	if (previous.equals("*")) { //$NON-NLS-1$
		return true;
	} else if (previous.equals("?")) { //$NON-NLS-1$
		return name.length() == start;
	}
	return name.endsWith(previous);
}

/**
 * @deprecated
 */
public static double round(double value) {
	return Math.round(value * 10000) / 10000.0;
}

/**
 * @deprecated
 */
public static double round(double value, int precision) {
	if (precision < 0) {
		throw new IllegalArgumentException("Should have a precision at least greater than 0!");
	}
	if (precision == 0) return (long) Math.floor(value);
	double factor = 10;
	int n = 1;
	while (n++ < precision)
		factor *= 10;
	return Math.round(value * factor) / factor;
}

/**
 * Returns a string to display the given time as a duration
 * formatted as "hh:mm:ss".
 *
 * @param time The time to format as a long.
 * @return The formatted string.
 */
public static String timeChrono(long time) {
	if (time < 1000) { // less than 1s
		return "00:00:00"; //$NON-NLS-1$
	}
	StringBuffer buffer = new StringBuffer();
	int seconds = (int) (time / 1000);
	if (seconds < 60) {
		buffer.append("00:00:"); //$NON-NLS-1$
		if (seconds < 10) buffer.append('0');
		buffer.append(seconds);
	} else {
		int minutes = seconds / 60;
		if (minutes < 60) {
			buffer.append("00:"); //$NON-NLS-1$
			if (minutes < 10) buffer.append('0');
			buffer.append(minutes);
			buffer.append(':');
			seconds = seconds % 60;
			if (seconds < 10) buffer.append('0');
			buffer.append(seconds);
		} else {
			int hours = minutes / 60;
			if (hours < 10) buffer.append('0');
			buffer.append(hours);
			buffer.append(':');
			minutes = minutes % 60;
			if (minutes < 10) buffer.append('0');
			buffer.append(minutes);
			buffer.append(':');
			seconds = seconds % 60;
			if (seconds < 10) buffer.append('0');
			buffer.append(seconds);
		}
	}
	return buffer.toString();
}

/**
 * Returns a string to display the given time as the hour of the day
 * formatted as "hh:mm:ss".
 *
 * @param time The time to format as a long.
 * @return The formatted string.
 */
public static String timeEnd(long time) {
	GregorianCalendar calendar = new GregorianCalendar();
	calendar.add(Calendar.SECOND, (int)(time/1000));
	Date date = calendar.getTime();
	SimpleDateFormat dateFormat = new SimpleDateFormat("KK:mm:ss"); //$NON-NLS-1$
	return dateFormat.format(date);
}

/**
 * Returns a string to display the given time as a duration
 * formatted as:
 *	<ul>
 *	<li>"XXXms" if the duration is less than 0.1s (e.g. "543ms")</li>
 *	<li>"X.YYs" if the duration is less than 1s (e.g. "5.43s")</li>
 *	<li>"XX.Ys" if the duration is less than 1mn (e.g. "54.3s")</li>
 *	<li>"XXmn XXs" if the duration is less than 1h (e.g. "54mn 3s")</li>
 *	<li>"XXh XXmn XXs" if the duration is over than 1h (e.g. "5h 4mn 3s")</li>
 *	</ul>
 *
 * @param time The time to format as a long.
 * @return The formatted string.
 */
public static String timeString(long time) {
	NumberFormat format = NumberFormat.getInstance();
	format.setMaximumFractionDigits(1);
	StringBuffer buffer = new StringBuffer();
	if (time == 0) {
		// print nothing
	} if (time < 100) { // less than 0.1s
		buffer.append(time);
		buffer.append("ms"); //$NON-NLS-1$
	} else if (time < 1000) { // less than 1s
		if ((time%100) != 0) {
			format.setMaximumFractionDigits(2);
		}
		buffer.append(format.format(time/1000.0));
		buffer.append("s"); //$NON-NLS-1$
	} else if (time < Util.ONE_MINUTE) {  // less than 1mn
		if ((time%1000) == 0) {
			buffer.append(time/1000);
		} else {
			buffer.append(format.format(time/1000.0));
		}
		buffer.append("s"); //$NON-NLS-1$
	} else if (time < Util.ONE_HOUR) {  // less than 1h
		buffer.append(time/Util.ONE_MINUTE).append("mn "); //$NON-NLS-1$
		long seconds = time%Util.ONE_MINUTE;
		buffer.append(seconds/1000);
		buffer.append("s"); //$NON-NLS-1$
	} else {  // more than 1h
		long h = time / Util.ONE_HOUR;
		buffer.append(h).append("h "); //$NON-NLS-1$
		long m = (time % Util.ONE_HOUR) / Util.ONE_MINUTE;
		buffer.append(m).append("mn "); //$NON-NLS-1$
		long seconds = m%Util.ONE_MINUTE;
		buffer.append(seconds/1000);
		buffer.append("s"); //$NON-NLS-1$
	}
	return buffer.toString();
}

private Util() {
	// don't instantiate
}

/**
 * Set the milestones.
 *
 * @param items The milestones list (e.g. {@link IPerformancesConstants#V35_MILESTONES}).
 */
public static void setMilestones(String[] items) {
	MILESTONES = items;
}

/**
 * Init the milestones from preferences
 *
 * @param preferences The preferences from which got milestones list
 */
public static void initMilestones(IEclipsePreferences preferences) {
	int eclipseVersion = preferences.getInt(IPerformancesConstants.PRE_ECLIPSE_VERSION, IPerformancesConstants.DEFAULT_ECLIPSE_VERSION);
	String prefix = IPerformancesConstants.PRE_MILESTONE_BUILDS + "." + eclipseVersion;
	int index = 0;
	String milestone = preferences.get(prefix + index, null);
	String[] milestones = new String[20];
	while (milestone != null) {
		milestones[index] = milestone;
		index++;
		milestone = preferences.get(prefix + index, null);
	}
	int length = milestones.length;
	if (index < length) {
		System.arraycopy(milestones, 0, milestones = new String[index], 0, index);
	}
	MILESTONES = milestones;
}
}