1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotSame;
|
---|
7 | import static org.junit.Assert.assertSame;
|
---|
8 | import static org.junit.Assert.assertTrue;
|
---|
9 |
|
---|
10 | import java.security.SecureRandom;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Random;
|
---|
13 |
|
---|
14 | import org.apache.commons.lang.RandomStringUtils;
|
---|
15 | import org.junit.Before;
|
---|
16 | import org.junit.Rule;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.junit.rules.Timeout;
|
---|
19 | import org.openstreetmap.josm.PerformanceTestUtils;
|
---|
20 | import org.openstreetmap.josm.PerformanceTestUtils.PerformanceTestTimer;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmDataGenerator.KeyValueDataGenerator;
|
---|
22 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
23 |
|
---|
24 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This test measures the performance of {@link OsmPrimitive#get(String)} and related.
|
---|
28 | * @author Michael Zangl
|
---|
29 | */
|
---|
30 | public class KeyValuePerformanceTest {
|
---|
31 | private static final int PUT_RUNS = 10000;
|
---|
32 | private static final int GET_RUNS = 100000;
|
---|
33 | private static final int TEST_STRING_COUNT = 10000;
|
---|
34 | private static final int STRING_INTERN_TESTS = 5000000;
|
---|
35 | private static final double[] TAG_NODE_RATIOS = new double[] {.05, .3, 3, 20, 200};
|
---|
36 | private ArrayList<String> testStrings = new ArrayList<>();
|
---|
37 | private Random random;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Global timeout applied to all test methods.
|
---|
41 | */
|
---|
42 | @Rule
|
---|
43 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
44 | public Timeout globalTimeout = Timeout.seconds(15*60);
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Prepare the test.
|
---|
48 | */
|
---|
49 | @Rule
|
---|
50 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
51 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * See if there is a big difference between Strings that are interned and those that are not.
|
---|
55 | */
|
---|
56 | @Test
|
---|
57 | @SuppressFBWarnings(value = "DM_STRING_CTOR", justification = "test Strings that are interned and those that are not")
|
---|
58 | public void testMeasureStringEqualsIntern() {
|
---|
59 | String str1Interned = "string1";
|
---|
60 | String str1InternedB = "string1";
|
---|
61 | String str1 = new String(str1Interned);
|
---|
62 | String str1B = new String(str1Interned);
|
---|
63 | String str2Interned = "string2";
|
---|
64 | String str2 = new String(str2Interned);
|
---|
65 |
|
---|
66 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
67 | // warm up
|
---|
68 | assertEquals(str1, str1B);
|
---|
69 | }
|
---|
70 |
|
---|
71 | PerformanceTestTimer timer = PerformanceTestUtils.startTimer("Assertion overhead.");
|
---|
72 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
73 | assertTrue(true);
|
---|
74 | }
|
---|
75 | timer.done();
|
---|
76 |
|
---|
77 | timer = PerformanceTestUtils.startTimer("str1.equals(str2) succeeds (without intern)");
|
---|
78 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
79 | assertEquals(str1, str1B);
|
---|
80 | }
|
---|
81 | timer.done();
|
---|
82 |
|
---|
83 | timer = PerformanceTestUtils.startTimer("str1 == str2 succeeds");
|
---|
84 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
85 | assertSame(str1Interned, str1InternedB);
|
---|
86 | }
|
---|
87 | timer.done();
|
---|
88 |
|
---|
89 | timer = PerformanceTestUtils.startTimer("str1 == str2.intern() succeeds");
|
---|
90 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
91 | assertSame(str1Interned, str1.intern());
|
---|
92 | }
|
---|
93 | timer.done();
|
---|
94 |
|
---|
95 | timer = PerformanceTestUtils.startTimer("str1 == str2.intern() succeeds for interned string");
|
---|
96 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
97 | assertSame(str1Interned, str1InternedB.intern());
|
---|
98 | }
|
---|
99 | timer.done();
|
---|
100 |
|
---|
101 | timer = PerformanceTestUtils.startTimer("str1.equals(str2) = fails (without intern)");
|
---|
102 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
103 | assertFalse(str1.equals(str2));
|
---|
104 | }
|
---|
105 | timer.done();
|
---|
106 |
|
---|
107 | timer = PerformanceTestUtils.startTimer("str1 == str2 fails");
|
---|
108 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
109 | assertNotSame(str1Interned, str2Interned);
|
---|
110 | }
|
---|
111 | timer.done();
|
---|
112 |
|
---|
113 | timer = PerformanceTestUtils.startTimer("str1 == str2.intern() fails");
|
---|
114 | for (int i = 0; i < STRING_INTERN_TESTS; i++) {
|
---|
115 | assertNotSame(str1Interned, str2.intern());
|
---|
116 | }
|
---|
117 | timer.done();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Generate an array of test strings.
|
---|
122 | */
|
---|
123 | @Before
|
---|
124 | public void generateTestStrings() {
|
---|
125 | testStrings.clear();
|
---|
126 | random = new SecureRandom();
|
---|
127 | for (int i = 0; i < TEST_STRING_COUNT; i++) {
|
---|
128 | testStrings.add(RandomStringUtils.random(10, 0, 0, true, true, null, random));
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Measure the speed of {@link OsmPrimitive#put(String, String)}
|
---|
134 | */
|
---|
135 | @Test
|
---|
136 | public void testKeyValuePut() {
|
---|
137 | for (double tagNodeRatio : TAG_NODE_RATIOS) {
|
---|
138 | int nodeCount = (int) (PUT_RUNS / tagNodeRatio);
|
---|
139 | KeyValueDataGenerator generator = OsmDataGenerator.getKeyValue(nodeCount, 0);
|
---|
140 | generator.generateDataSet();
|
---|
141 |
|
---|
142 | PerformanceTestTimer timer = PerformanceTestUtils
|
---|
143 | .startTimer("OsmPrimitive#put(String, String) with put/node ratio " + tagNodeRatio);
|
---|
144 |
|
---|
145 | for (int i = 0; i < PUT_RUNS; i++) {
|
---|
146 | String key = generator.randomKey();
|
---|
147 | String value = generator.randomValue();
|
---|
148 | generator.randomNode().put(key, value);
|
---|
149 | }
|
---|
150 |
|
---|
151 | timer.done();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Measure the speed of {@link OsmPrimitive#get(String)}
|
---|
157 | */
|
---|
158 | @Test
|
---|
159 | public void testKeyValueGet() {
|
---|
160 | for (double tagNodeRatio : TAG_NODE_RATIOS) {
|
---|
161 | KeyValueDataGenerator generator = OsmDataGenerator.getKeyValue(tagNodeRatio);
|
---|
162 | generator.generateDataSet();
|
---|
163 |
|
---|
164 | PerformanceTestTimer timer = PerformanceTestUtils
|
---|
165 | .startTimer("OsmPrimitive#get(String) with tag/node ratio " + tagNodeRatio);
|
---|
166 | for (int i = 0; i < GET_RUNS; i++) {
|
---|
167 | String key = generator.randomKey();
|
---|
168 | // to make comparison easier.
|
---|
169 | generator.randomValue();
|
---|
170 | generator.randomNode().get(key);
|
---|
171 | }
|
---|
172 | timer.done();
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Measure the speed of {@link OsmPrimitive#getKeys()}
|
---|
178 | */
|
---|
179 | @Test
|
---|
180 | public void testKeyValueGetKeys() {
|
---|
181 | for (double tagNodeRatio : TAG_NODE_RATIOS) {
|
---|
182 | KeyValueDataGenerator generator = OsmDataGenerator.getKeyValue(tagNodeRatio);
|
---|
183 | generator.generateDataSet();
|
---|
184 |
|
---|
185 | PerformanceTestTimer timer = PerformanceTestUtils.startTimer("OsmPrimitive#getKeys() with tag/node ratio "
|
---|
186 | + tagNodeRatio);
|
---|
187 |
|
---|
188 | for (int i = 0; i < GET_RUNS; i++) {
|
---|
189 | // to make comparison easier.
|
---|
190 | generator.randomKey();
|
---|
191 | generator.randomValue();
|
---|
192 | generator.randomNode().getKeys();
|
---|
193 | }
|
---|
194 | timer.done();
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Measure the speed of {@link OsmPrimitive#getKeys()}.get(key)
|
---|
200 | */
|
---|
201 | @Test
|
---|
202 | @SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
|
---|
203 | public void testKeyValueGetKeysGet() {
|
---|
204 | for (double tagNodeRatio : TAG_NODE_RATIOS) {
|
---|
205 | KeyValueDataGenerator generator = OsmDataGenerator.getKeyValue(tagNodeRatio);
|
---|
206 | generator.generateDataSet();
|
---|
207 |
|
---|
208 | PerformanceTestTimer timer = PerformanceTestUtils
|
---|
209 | .startTimer("OsmPrimitive#getKeys().get(key) with tag/node ratio " + tagNodeRatio);
|
---|
210 | for (int i = 0; i < GET_RUNS; i++) {
|
---|
211 | String key = generator.randomKey();
|
---|
212 | // to make comparison easier.
|
---|
213 | generator.randomValue();
|
---|
214 | generator.randomNode().getKeys().get(key);
|
---|
215 | }
|
---|
216 | timer.done();
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|