1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
5 |
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 | import java.awt.image.BufferedImage;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Collection;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.Test;
|
---|
13 | import org.openstreetmap.josm.PerformanceTestUtils;
|
---|
14 | import org.openstreetmap.josm.data.Bounds;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
17 | import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
|
---|
18 | import org.openstreetmap.josm.data.preferences.sources.SourceType;
|
---|
19 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.MapRendererPerformanceTest;
|
---|
21 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
22 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This performance test measures the time for a full run of MapPaintVisitor.visitAll()
|
---|
26 | * against a test data set using a test style.
|
---|
27 | *
|
---|
28 | */
|
---|
29 | @Projection
|
---|
30 | class MapCSSPerformanceTest {
|
---|
31 |
|
---|
32 | /* ------------------------ configuration section ---------------------------- */
|
---|
33 | /**
|
---|
34 | * The path to the style file used for rendering.
|
---|
35 | */
|
---|
36 | static final String STYLE_FILE = "resources/styles/standard/elemstyles.mapcss";
|
---|
37 |
|
---|
38 | /* ------------------------ / configuration section ---------------------------- */
|
---|
39 |
|
---|
40 | DataSet ds;
|
---|
41 |
|
---|
42 | static void checkTestEnvironment() {
|
---|
43 | File f = new File(STYLE_FILE);
|
---|
44 | if (!f.isFile() || !f.exists()) {
|
---|
45 | fail("STYLE_FILE refers to '"+STYLE_FILE+"'. This is either not a file or doesn't exist.\n" +
|
---|
46 | "Please update configuration settings in the unit test file.");
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | long timed(Runnable callable) {
|
---|
51 | long before = System.currentTimeMillis();
|
---|
52 | callable.run();
|
---|
53 | long after = System.currentTimeMillis();
|
---|
54 | return after - before;
|
---|
55 | }
|
---|
56 |
|
---|
57 | void loadStyle() {
|
---|
58 | System.out.print("Loading style '"+STYLE_FILE+"' ...");
|
---|
59 | MapCSSStyleSource source = new MapCSSStyleSource(
|
---|
60 | new SourceEntry(
|
---|
61 | SourceType.MAP_PAINT_STYLE,
|
---|
62 | STYLE_FILE,
|
---|
63 | "test style",
|
---|
64 | "a test style",
|
---|
65 | true // active
|
---|
66 | )
|
---|
67 | );
|
---|
68 | source.loadStyleSource();
|
---|
69 | Collection<Throwable> errors = source.getErrors();
|
---|
70 | if (!errors.isEmpty()) {
|
---|
71 | fail("Failed to load style file ''"+STYLE_FILE+"''. Errors: "+errors);
|
---|
72 | }
|
---|
73 | MapRendererPerformanceTest.resetStylesToSingle(source);
|
---|
74 | System.out.println("DONE");
|
---|
75 | }
|
---|
76 |
|
---|
77 | void loadData() throws IllegalDataException, IOException {
|
---|
78 | System.out.print("Loading data file '"+PerformanceTestUtils.DATA_FILE+"' ...");
|
---|
79 | ds = PerformanceTestUtils.getNeubrandenburgDataSet();
|
---|
80 | System.out.println("DONE");
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Measures time for style preparation.
|
---|
85 | * @throws IOException if any I/O error occurs
|
---|
86 | * @throws IllegalDataException if any invalid data is found
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | public void measureTimeForStylePreparation() throws IllegalDataException, IOException {
|
---|
90 | loadStyle();
|
---|
91 | loadData();
|
---|
92 |
|
---|
93 | NavigatableComponent mv = new NavigatableComponent();
|
---|
94 | mv.setBounds(0, 0, 1024, 768);
|
---|
95 | BufferedImage img = new BufferedImage(mv.getWidth(), mv.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
|
---|
96 | Graphics2D g = img.createGraphics();
|
---|
97 | g.setClip(0, 0, mv.getWidth(), mv.getHeight());
|
---|
98 | StyledMapRenderer visitor = new StyledMapRenderer(g, mv, false);
|
---|
99 |
|
---|
100 | System.out.print("Rendering ...");
|
---|
101 | long time = timed(
|
---|
102 | () -> visitor.render(ds, false, new Bounds(-90, -180, 90, 180))
|
---|
103 | );
|
---|
104 | System.out.println("DONE");
|
---|
105 | System.out.println("data file : "+PerformanceTestUtils.DATA_FILE);
|
---|
106 | System.out.println("style file: "+STYLE_FILE);
|
---|
107 | System.out.println("");
|
---|
108 | System.out.println("Rendering took "+time+" ms.");
|
---|
109 | }
|
---|
110 | }
|
---|