Skip to main content
summaryrefslogtreecommitdiffstats
blob: b59c1c074448b33d5bdb26f0912f44f6c28f0fdf (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009 Cloudsmith Inc. 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:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.p2.tests.omniVersion;

import junit.framework.TestCase;
import org.eclipse.equinox.p2.metadata.Version;

/**
 * Simple performance comparison between OSGi version implementation and Omni Version.
 * Tests performance of creating version instances using 4 values, as well as string parsing.
 * Tests comparison of versions.
 * 
 * Aprox 10000 instances are created.
 * Comparison compares all instances against all other (i.e. about 10 milj).
 * 
 */
public class PerformanceTest extends TestCase {
	static final int MUL = 24;

	static final String qualifierTemplate = "r20090112-12345-abcdefghijklmnopqrstuvwxyz"; // longer than MUL chars

	public void testStringCreationPerformance() {
		// Ensure that classes are loaded etc.
		Version.MAX_VERSION.compareTo(Version.emptyVersion);
		org.osgi.framework.Version.emptyVersion.compareTo(org.osgi.framework.Version.emptyVersion);

		// Create all versions in string format
		String[] strings = createStrings();

		long start = System.currentTimeMillis();
		for (int idx = 0; idx < 100; ++idx)
			osgiVersionCreateFromString(strings);
		long osgiTime = System.currentTimeMillis() - start;

		start = System.currentTimeMillis();
		for (int idx = 0; idx < 100; ++idx)
			omniVersionCreateFromString(strings);
		long omniTime = System.currentTimeMillis() - start;
		outputResult("String creation", 100 * MUL * MUL * MUL, osgiTime, omniTime);
		// System.out.printf("String creation: osgi=%d, omni=%d, factor=%.2f\n", osgiTime, omniTime, factor(omniTime, osgiTime));
	}

	public void testCreationPerformance() {
		// Ensure that classes are loaded etc.
		Version.MAX_VERSION.compareTo(Version.emptyVersion);
		org.osgi.framework.Version.emptyVersion.compareTo(org.osgi.framework.Version.emptyVersion);

		long start = System.currentTimeMillis();
		for (int idx = 0; idx < 100; ++idx)
			osgiVersionCreate();
		long osgiTime = System.currentTimeMillis() - start;

		start = System.currentTimeMillis();
		for (int idx = 0; idx < 100; ++idx)
			omniVersionCreate();
		long omniTime = System.currentTimeMillis() - start;
		outputResult("Creation", 100 * MUL * MUL * MUL, osgiTime, omniTime);

		// System.out.printf("Creation: osgi=%d, omni=%d, factor=%f2\n", osgiTime, omniTime, factor(omniTime, osgiTime));
	}

	public void testComparePerformance() {
		Version[] omniVersions = createOmniVersions();
		org.osgi.framework.Version osgiVersions[] = createOsgiVersions();

		long start = System.currentTimeMillis();
		osgiVersionCompare(osgiVersions);
		long osgiTime = System.currentTimeMillis() - start;

		start = System.currentTimeMillis();
		omniVersionCompare(omniVersions);
		long omniTime = System.currentTimeMillis() - start;
		long units = MUL * MUL * MUL * MUL * MUL * MUL;
		outputResult("Compare", units, osgiTime, omniTime);

		//System.out.printf("Compare (%d comparisons): osgi=%d, omni=%d\n, factor=%d", units, osgiTime, omniTime, omniTime / osgiTime);
	}

	public void testEqualsPerformance() {
		Version[] omniVersions = createOmniVersions();
		org.osgi.framework.Version osgiVersions[] = createOsgiVersions();

		long start = System.currentTimeMillis();
		osgiVersionEquals(osgiVersions);
		long osgiTime = System.currentTimeMillis() - start;

		start = System.currentTimeMillis();
		omniVersionEquals(omniVersions);
		long omniTime = System.currentTimeMillis() - start;
		long units = MUL * MUL * MUL * MUL * MUL * MUL;
		outputResult("Equals", units, osgiTime, omniTime);

		//System.out.printf("Equals (%d comparisons): osgi=%d, omni=%d, factor=%d\n", units, osgiTime, omniTime, omniTime / osgiTime);
	}

	public void testToStringPerformance() {
		Version[] omniVersions = createOmniVersions();
		org.osgi.framework.Version osgiVersions[] = createOsgiVersions();

		long start = System.currentTimeMillis();
		for (int idx = 0; idx < 100; ++idx)
			osgiVersionToString(osgiVersions);
		long osgiTime = System.currentTimeMillis() - start;

		start = System.currentTimeMillis();
		for (int idx = 0; idx < 100; ++idx)
			omniVersionToString(omniVersions);
		long omniTime = System.currentTimeMillis() - start;
		long units = 100 * MUL * MUL * MUL;
		outputResult("To String", units, osgiTime, omniTime);

		//System.out.printf("toString (%d versions): osgi=%d, omni=%d\n", units, osgiTime, omniTime);
	}

	public static void osgiVersionToString(org.osgi.framework.Version versions[]) {
		// compare every version against all other versions
		for (int i = 0; i < MUL * MUL * MUL; i++)
			versions[i].toString();
	}

	public static void omniVersionToString(Version versions[]) {
		// compare every version against all other versions
		for (int i = 0; i < MUL * MUL * MUL; i++)
			versions[i].toString();
	}

	public static void omniVersionCreate() {
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++)
					Version.createOSGi(i, j, k, qualifierTemplate);
	}

	public static void omniVersionCompare(Version versions[]) {
		//compare every version against all other versions
		for (int i = 0; i < MUL * MUL * MUL; i++)
			for (int j = 0; j < MUL * MUL * MUL; j++)
				versions[i].compareTo(versions[j]);
	}

	public static void omniVersionEquals(Version versions[]) {
		//compare every version against all other versions
		for (int i = 0; i < MUL * MUL * MUL; i++)
			for (int j = 0; j < MUL * MUL * MUL; j++)
				versions[i].equals(versions[j]);
	}

	public static void omniVersionCreateFromString(String[] strings) {
		int x = 0;
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++)
					Version.create(strings[x++]);
	}

	public static void osgiVersionCompare(org.osgi.framework.Version versions[]) {
		// compare every version against all other versions
		for (int i = 0; i < MUL * MUL * MUL; i++)
			for (int j = 0; j < MUL * MUL * MUL; j++)
				versions[i].compareTo(versions[j]);
	}

	public static void osgiVersionEquals(org.osgi.framework.Version versions[]) {
		// compare every version against all other versions
		for (int i = 0; i < MUL * MUL * MUL; i++)
			for (int j = 0; j < MUL * MUL * MUL; j++)
				versions[i].equals(versions[j]);
	}

	public static void osgiVersionCreate() {
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++)
					new org.osgi.framework.Version(i, j, k, qualifierTemplate);
	}

	public static void osgiVersionCreateFromString(String[] strings) {
		int x = 0;
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++)
					org.osgi.framework.Version.parseVersion(strings[x++]);
	}

	/**
	 * Create a set of different versions. The execution of this method does not take part
	 * in the time measurement
	 */
	private static Version[] createOmniVersions() {
		Version versions[] = new Version[MUL * MUL * MUL];
		int x = 0;
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++)
					versions[x++] = Version.createOSGi(i, j, k, qualifierTemplate.substring(0, k + 1));
		return versions;
	}

	/**
	 * Create a set of different versions. The execution of this method does not take part
	 * in the time measurement
	 */
	private static org.osgi.framework.Version[] createOsgiVersions() {
		org.osgi.framework.Version versions[] = new org.osgi.framework.Version[MUL * MUL * MUL];
		int x = 0;
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++)
					versions[x++] = new org.osgi.framework.Version(i, j, k, qualifierTemplate.substring(0, k + 1));
		return versions;
	}

	/**
	 * Create a set of different version strings. The execution of this method does not take part
	 * in the time measurement
	 */
	private static String[] createStrings() {
		String[] strings = new String[MUL * MUL * MUL];
		StringBuffer buf = new StringBuffer(100);
		int x = 0;
		for (int i = 0; i < MUL; i++)
			for (int j = 0; j < MUL; j++)
				for (int k = 0; k < MUL; k++) {
					buf.setLength(0);
					buf.append(i);
					buf.append(".");
					buf.append(j);
					buf.append(".");
					buf.append(k);
					buf.append(".");
					buf.append(qualifierTemplate.substring(0, k + 1));
					strings[x++] = buf.toString();
				}
		return strings;
	}

	private static double factor(long osgiTime, long omniTime) {
		double osgi = osgiTime;
		double omni = omniTime;
		return osgi / omni;
	}

	private static void outputResult(String message, long units, long osgiTime, long omniTime) {
		System.out.printf("%s (units %d): osgi=%d [%.2fus/unit], omni=%d [%.2fus/unit], factor=%.2f\n", message, units, osgiTime, perUnit(osgiTime, units), omniTime, perUnit(omniTime, units), factor(omniTime, osgiTime));
	}

	private static double perUnit(long timeMillisec, long units) {
		double time = (timeMillisec * 1000);
		double u = units;
		return time / u;
	}
}

Back to the top