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