source: josm/trunk/test/performance/org/openstreetmap/josm/gui/mappaint/MapRendererPerformanceTest.java@ 18888

Last change on this file since 18888 was 18888, checked in by taylor.smock, 7 months ago

See #18866: Remove Potlatch2 from the built-in styles

This fixes the MapRendererPerformanceTest that failed due to depending upon the
number of built-in paintstyles.

  • Property svn:eol-style set to native
File size: 13.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertNotNull;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.awt.Color;
9import java.awt.Graphics2D;
10import java.awt.Point;
11import java.awt.image.BufferedImage;
12import java.io.File;
13import java.io.IOException;
14import java.util.ArrayList;
15import java.util.Collections;
16import java.util.Comparator;
17import java.util.EnumMap;
18import java.util.List;
19import java.util.Locale;
20import java.util.Map;
21import java.util.concurrent.TimeUnit;
22import java.util.stream.Collectors;
23
24import javax.imageio.ImageIO;
25
26import org.junit.jupiter.api.AfterAll;
27import org.junit.jupiter.api.BeforeAll;
28import org.junit.jupiter.api.Test;
29import org.junit.jupiter.api.Timeout;
30import org.openstreetmap.josm.PerformanceTestUtils;
31import org.openstreetmap.josm.TestUtils;
32import org.openstreetmap.josm.data.Bounds;
33import org.openstreetmap.josm.data.coor.LatLon;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.data.osm.visitor.paint.RenderBenchmarkCollector.CapturingBenchmark;
36import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
37import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer.StyleRecord;
38import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
39import org.openstreetmap.josm.data.projection.ProjectionRegistry;
40import org.openstreetmap.josm.gui.NavigatableComponent;
41import org.openstreetmap.josm.gui.mappaint.StyleSetting.BooleanStyleSetting;
42import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
43import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
44import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
45import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
46import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
47import org.openstreetmap.josm.testutils.annotations.Main;
48import org.openstreetmap.josm.testutils.annotations.Projection;
49import org.openstreetmap.josm.testutils.annotations.Territories;
50
51import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
52
53/**
54 * Performance test of map renderer.
55 */
56@BasicPreferences
57@Main
58@Projection
59@Territories
60@Timeout(value = 15, unit = TimeUnit.MINUTES)
61public class MapRendererPerformanceTest {
62
63 private static final boolean DUMP_IMAGE = false; // dump images to file for debugging purpose
64
65 private static final int IMG_WIDTH = 2048;
66 private static final int IMG_HEIGHT = 1536;
67
68 private static Graphics2D g;
69 private static BufferedImage img;
70 private static NavigatableComponent nc;
71 private static DataSet dsCity;
72 private static final Bounds BOUNDS_CITY_ALL = new Bounds(53.4382, 13.1094, 53.6153, 13.4074, false);
73 private static final LatLon LL_CITY = new LatLon(53.5574458, 13.2602781);
74 private static final double SCALE_Z17 = 1.5;
75
76 private static int defaultStyleIdx;
77 private static BooleanStyleSetting hideIconsSetting;
78
79 private static int filterStyleIdx;
80 private static StyleSource filterStyle;
81
82 private enum Feature {
83 ICON, SYMBOL, NODE_TEXT, LINE, LINE_TEXT, AREA;
84 public String label() {
85 return name().toLowerCase(Locale.ENGLISH);
86 }
87 }
88
89 private static final EnumMap<Feature, BooleanStyleSetting> filters = new EnumMap<>(Feature.class);
90
91 /**
92 * Initializes test environment.
93 * @throws Exception if any error occurs
94 */
95 @BeforeAll
96 public static void load() throws Exception {
97 img = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
98 g = (Graphics2D) img.getGraphics();
99 g.setClip(0, 0, IMG_WIDTH, IMG_HEIGHT);
100 g.setColor(Color.BLACK);
101 g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
102
103 nc = new NavigatableComponent() {
104 {
105 setBounds(0, 0, IMG_WIDTH, IMG_HEIGHT);
106 updateLocationState();
107 }
108
109 @Override
110 protected boolean isVisibleOnScreen() {
111 return true;
112 }
113
114 @Override
115 public Point getLocationOnScreen() {
116 return new Point(0, 0);
117 }
118 };
119 nc.zoomTo(BOUNDS_CITY_ALL);
120
121 MapPaintStyles.readFromPreferences();
122
123 SourceEntry se = new MapCSSStyleSource(TestUtils.getTestDataRoot() + "styles/filter.mapcss", "filter", "");
124 filterStyle = MapPaintStyles.addStyle(se);
125 List<StyleSource> sources = MapPaintStyles.getStyles().getStyleSources();
126 filterStyleIdx = sources.indexOf(filterStyle);
127 assertEquals(1, filterStyleIdx);
128
129 assertEquals(Feature.values().length, filterStyle.settings.size());
130 for (StyleSetting set : filterStyle.settings) {
131 BooleanStyleSetting bset = (BooleanStyleSetting) set;
132 String prefKey = bset.getKey();
133 boolean found = false;
134 for (Feature f : Feature.values()) {
135 if (prefKey.endsWith(":" + f.label() + "_off")) {
136 filters.put(f, bset);
137 found = true;
138 break;
139 }
140 }
141 assertTrue(found, prefKey);
142 }
143
144 MapCSSStyleSource defaultStyle = null;
145 for (int i = 0; i < sources.size(); i++) {
146 StyleSource s = sources.get(i);
147 if ("resource://styles/standard/elemstyles.mapcss".equals(s.url)) {
148 defaultStyle = (MapCSSStyleSource) s;
149 defaultStyleIdx = i;
150 break;
151 }
152 }
153 assertNotNull(defaultStyle);
154
155 for (StyleSetting set : defaultStyle.settings) {
156 if (set instanceof BooleanStyleSetting) {
157 BooleanStyleSetting bset = (BooleanStyleSetting) set;
158 if (bset.getKey().endsWith(":hide_icons")) {
159 hideIconsSetting = bset;
160 }
161 }
162 }
163 assertNotNull(hideIconsSetting);
164 hideIconsSetting.setValue(false);
165 MapPaintStyleLoader.reloadStyles(defaultStyleIdx);
166
167 dsCity = PerformanceTestUtils.getNeubrandenburgDataSet();
168 }
169
170 /**
171 * Cleanup test environment.
172 */
173 @AfterAll
174 public static void cleanUp() {
175 setFilterStyleActive(false);
176 if (hideIconsSetting != null) {
177 hideIconsSetting.setValue(true);
178 }
179 MapPaintStyleLoader.reloadStyles(defaultStyleIdx);
180 }
181
182 private static class PerformanceTester {
183 public double scale = 0;
184 public LatLon center = LL_CITY;
185 public Bounds bounds;
186 public int noWarmup = 20;
187 public int noIterations = 30;
188 public boolean dumpImage = DUMP_IMAGE;
189 public boolean clearStyleCache = true;
190 public String label = "";
191 public boolean mpGenerate = false;
192 public boolean mpSort = false;
193 public boolean mpDraw = false;
194 public boolean mpTotal = false;
195
196 private final List<Long> generateTimes = new ArrayList<>();
197 private final List<Long> sortTimes = new ArrayList<>();
198 private final List<Long> drawTimes = new ArrayList<>();
199 private final List<Long> totalTimes = new ArrayList<>();
200
201 @SuppressFBWarnings(value = "DM_GC")
202 public void run() throws IOException {
203 boolean checkScale = false;
204 if (scale == 0) {
205 checkScale = true;
206 scale = SCALE_Z17;
207 }
208 nc.zoomTo(ProjectionRegistry.getProjection().latlon2eastNorth(center), scale);
209 if (checkScale) {
210 int lvl = Selector.GeneralSelector.scale2level(nc.getDist100Pixel());
211 assertEquals(17, lvl);
212 }
213
214 if (bounds == null) {
215 bounds = nc.getLatLonBounds(g.getClipBounds());
216 }
217
218 StyledMapRenderer renderer = new StyledMapRenderer(g, nc, false);
219 assertEquals(IMG_WIDTH, (int) nc.getState().getViewWidth());
220 assertEquals(IMG_HEIGHT, (int) nc.getState().getViewHeight());
221
222 int noTotal = noWarmup + noIterations;
223 for (int i = 1; i <= noTotal; i++) {
224 g.setColor(Color.BLACK);
225 g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
226 if (clearStyleCache) {
227 MapPaintStyles.getStyles().clearCached();
228 dsCity.clearMappaintCache();
229 }
230 BenchmarkData data = new BenchmarkData();
231 renderer.setBenchmarkFactory(() -> data);
232 renderer.render(dsCity, false, bounds);
233
234 if (i > noWarmup) {
235 generateTimes.add(data.getGenerateTime());
236 sortTimes.add(data.getSortTime());
237 drawTimes.add(data.getDrawTime());
238 totalTimes.add(data.getGenerateTime() + data.getSortTime() + data.getDrawTime());
239 }
240 if (i == 1) {
241 data.dumpElementCount();
242 }
243 data.dumpTimes();
244 if (dumpImage && i == noTotal) {
245 dumpRenderedImage(label);
246 }
247 }
248
249 if (mpGenerate) {
250 processTimes(generateTimes, "generate");
251 }
252 if (mpSort) {
253 processTimes(sortTimes, "sort");
254 }
255 if (mpDraw) {
256 processTimes(drawTimes, "draw");
257 }
258 if (mpTotal) {
259 processTimes(totalTimes, "total");
260 }
261 }
262
263 private void processTimes(List<Long> times, String sublabel) {
264 Collections.sort(times);
265 // Take median instead of average. This should give a more stable
266 // result and avoids distortions by outliers.
267 long medianTime = times.get(times.size() / 2);
268 PerformanceTestUtils.measurementPlotsPluginOutput(label + " " + sublabel + " (ms)", medianTime);
269 }
270 }
271
272 /**
273 * Test phase 1, the calculation of {@link StyleElement}s.
274 * @throws IOException in case of an I/O error
275 */
276 @Test
277 void testPerformanceGenerate() throws IOException {
278 setFilterStyleActive(false);
279 PerformanceTester test = new PerformanceTester();
280 test.bounds = BOUNDS_CITY_ALL;
281 test.label = "big";
282 test.dumpImage = false;
283 test.mpGenerate = true;
284 test.clearStyleCache = true;
285 test.run();
286 }
287
288 private static void testDrawFeature(Feature feature) throws IOException {
289 PerformanceTester test = new PerformanceTester();
290 test.mpDraw = true;
291 test.clearStyleCache = false;
292 if (feature != null) {
293 BooleanStyleSetting filterSetting = filters.get(feature);
294 test.label = filterSetting.label;
295 setFilterStyleActive(true);
296 for (Feature f : Feature.values()) {
297 filters.get(f).setValue(true);
298 }
299 filterSetting.setValue(false);
300 } else {
301 test.label = "all";
302 setFilterStyleActive(false);
303 }
304 MapPaintStyleLoader.reloadStyles(filterStyleIdx);
305 dsCity.clearMappaintCache();
306 test.run();
307 }
308
309 /**
310 * Test phase 2, the actual drawing.
311 * Several runs: Icons, lines, etc. are tested separately (+ one run with
312 * all features activated)
313 * @throws IOException in case of an I/O error
314 */
315 @Test
316 void testPerformanceDrawFeatures() throws IOException {
317 testDrawFeature(null);
318 for (Feature f : Feature.values()) {
319 testDrawFeature(f);
320 }
321 }
322
323 /**
324 * Resets MapPaintStyles to a single source.
325 * @param source new map paint style source
326 */
327 public static void resetStylesToSingle(StyleSource source) {
328 MapPaintStyles.getStyles().clear();
329 MapPaintStyles.getStyles().add(source);
330 }
331
332 private static void setFilterStyleActive(boolean active) {
333 if (filterStyle != null) {
334 if (filterStyle.active != active) {
335 MapPaintStyles.toggleStyleActive(filterStyleIdx);
336 }
337 // Assert.assertEquals(active, filterStyle.active);
338 }
339 }
340
341 private static void dumpRenderedImage(String id) throws IOException {
342 File outputfile = new File("test-neubrandenburg-"+id+".png");
343 ImageIO.write(img, "png", outputfile);
344 }
345
346 static class BenchmarkData extends CapturingBenchmark {
347
348 private List<StyleRecord> allStyleElems;
349
350 @Override
351 public boolean renderDraw(List<StyleRecord> allStyleElems) {
352 this.allStyleElems = allStyleElems;
353 return super.renderDraw(allStyleElems);
354 }
355
356 private Map<Class<? extends StyleElement>, Long> recordElementStats() {
357 return allStyleElems.stream()
358 .collect(Collectors.groupingBy(r -> r.getStyle().getClass(), Collectors.counting()));
359 }
360
361 public void dumpTimes() {
362 System.out.printf("gen. %4d, sort %4d, draw %4d%n", getGenerateTime(), getSortTime(), getDrawTime());
363 }
364
365 public void dumpElementCount() {
366 System.out.println(recordElementStats().entrySet().stream()
367 .sorted(Comparator.comparing(e -> e.getKey().getSimpleName()))
368 .map(e -> e.getKey().getSimpleName().replace("Element", "") + ":" + e.getValue())
369 .collect(Collectors.joining(" ")));
370 }
371 }
372}
Note: See TracBrowser for help on using the repository browser.