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