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