1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import org.junit.BeforeClass;
|
---|
5 | import org.junit.Rule;
|
---|
6 | import org.junit.Test;
|
---|
7 | import org.junit.rules.Timeout;
|
---|
8 | import org.openstreetmap.josm.JOSMFixture;
|
---|
9 | import org.openstreetmap.josm.PerformanceTestUtils;
|
---|
10 | import org.openstreetmap.josm.PerformanceTestUtils.PerformanceTestTimer;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmDataGenerator;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmDataGenerator.KeyValueDataGenerator;
|
---|
13 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
14 |
|
---|
15 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Tests how fast {@link MapCSSStyleSource} finds the right style candidates for one object.
|
---|
19 | * @author Michael Zangl
|
---|
20 | */
|
---|
21 | public class MapCSSStyleSourceFilterTest {
|
---|
22 |
|
---|
23 | private static final int TEST_RULE_COUNT = 10000;
|
---|
24 |
|
---|
25 | private static class CssGenerator {
|
---|
26 | StringBuilder sb = new StringBuilder();
|
---|
27 | private KeyValueDataGenerator generator;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Create a new CSS generator.
|
---|
31 | * @param generator A generator to get the keys from.
|
---|
32 | */
|
---|
33 | CssGenerator(KeyValueDataGenerator generator) {
|
---|
34 | this.generator = generator;
|
---|
35 | }
|
---|
36 |
|
---|
37 | private CssGenerator addKeyValueRules(int count) {
|
---|
38 | for (int i = 0; i < count; i++) {
|
---|
39 | String key = generator.randomKey();
|
---|
40 | String value = generator.randomValue();
|
---|
41 | addRule("node[\"" + key + "\"=\"" + value + "\"]");
|
---|
42 | }
|
---|
43 | return this;
|
---|
44 | }
|
---|
45 |
|
---|
46 | private CssGenerator addKeyRegexpRules(int count) {
|
---|
47 | for (int i = 0; i < count; i++) {
|
---|
48 | String key = generator.randomKey();
|
---|
49 | String value = generator.randomValue();
|
---|
50 | value = value.substring(i % value.length());
|
---|
51 | addRule("node[\"" + key + "\"=~/.*" + value + ".*/]");
|
---|
52 | }
|
---|
53 | return this;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public CssGenerator addHasKeyRules(int count) {
|
---|
57 | for (int i = 0; i < count; i++) {
|
---|
58 | String key = generator.randomKey();
|
---|
59 | addRule("node[\"" + key + "\"]");
|
---|
60 | }
|
---|
61 | return this;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public CssGenerator addIsTrueRules(int count) {
|
---|
65 | for (int i = 0; i < count; i++) {
|
---|
66 | String key = generator.randomKey();
|
---|
67 | addRule("node[\"" + key + "\"?]");
|
---|
68 | }
|
---|
69 | return this;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void addRule(String selector) {
|
---|
73 | sb.append(selector + " {}\n");
|
---|
74 | }
|
---|
75 |
|
---|
76 | public String getCss() {
|
---|
77 | return sb.toString();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static final int APPLY_CALLS = 100000;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Global timeout applied to all test methods.
|
---|
85 | */
|
---|
86 | @Rule
|
---|
87 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
88 | public Timeout globalTimeout = Timeout.seconds(15*60);
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Prepare the test.
|
---|
92 | */
|
---|
93 | @BeforeClass
|
---|
94 | public static void createJOSMFixture() {
|
---|
95 | JOSMFixture.createPerformanceTestFixture().init(true);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Time how long it takes to evaluate [key=value] rules
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | public void testKeyValueRules() {
|
---|
103 | KeyValueDataGenerator data = OsmDataGenerator.getKeyValue();
|
---|
104 | data.generateDataSet();
|
---|
105 | CssGenerator css = new CssGenerator(data).addKeyValueRules(TEST_RULE_COUNT);
|
---|
106 | runTest(data, css, "only key=value rules");
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Time how long it takes to evaluate [key] rules
|
---|
111 | */
|
---|
112 | @Test
|
---|
113 | public void testKeyOnlyRules() {
|
---|
114 | KeyValueDataGenerator data = OsmDataGenerator.getKeyValue();
|
---|
115 | data.generateDataSet();
|
---|
116 | CssGenerator css = new CssGenerator(data).addHasKeyRules(TEST_RULE_COUNT);
|
---|
117 | runTest(data, css, "only has key rules");
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Time how long it takes to evaluate [key=~...] rules
|
---|
122 | */
|
---|
123 | @Test
|
---|
124 | public void testRegularExpressionRules() {
|
---|
125 | KeyValueDataGenerator data = OsmDataGenerator.getKeyValue();
|
---|
126 | data.generateDataSet();
|
---|
127 | CssGenerator css = new CssGenerator(data).addKeyRegexpRules(TEST_RULE_COUNT);
|
---|
128 | runTest(data, css, "regular expressions");
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Time how long it takes to evaluate [key?] rules
|
---|
133 | */
|
---|
134 | @Test
|
---|
135 | public void testIsTrueRules() {
|
---|
136 | KeyValueDataGenerator data = OsmDataGenerator.getKeyValue();
|
---|
137 | data.generateDataSet();
|
---|
138 | CssGenerator css = new CssGenerator(data).addIsTrueRules(TEST_RULE_COUNT);
|
---|
139 | runTest(data, css, "is true");
|
---|
140 | }
|
---|
141 |
|
---|
142 | private void runTest(KeyValueDataGenerator data, CssGenerator css, String description) {
|
---|
143 | MapCSSStyleSource source = new MapCSSStyleSource(css.getCss());
|
---|
144 | PerformanceTestTimer timer = PerformanceTestUtils.startTimer("MapCSSStyleSource#loadStyleSource(...) for " + description);
|
---|
145 | source.loadStyleSource();
|
---|
146 | timer.done();
|
---|
147 |
|
---|
148 | timer = PerformanceTestUtils.startTimer(APPLY_CALLS + "x MapCSSStyleSource#apply(...) for " + description);
|
---|
149 | for (int i = 0; i < APPLY_CALLS; i++) {
|
---|
150 | MultiCascade mc = new MultiCascade();
|
---|
151 | source.apply(mc, data.randomNode(), 1, false);
|
---|
152 | }
|
---|
153 | timer.done();
|
---|
154 | }
|
---|
155 | }
|
---|