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