source: josm/trunk/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSPerformanceTest.java @ 12650

Last change on this file since 12650 was 12650, checked in by Don-vip, 4 months ago

see #15182 - convert MapCSS performance test to Java + update import

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.Assert.fail;
5
6import java.awt.Graphics2D;
7import java.awt.image.BufferedImage;
8import java.io.File;
9import java.io.IOException;
10import java.util.Collection;
11
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.openstreetmap.josm.JOSMFixture;
15import org.openstreetmap.josm.data.Bounds;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
18import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
19import org.openstreetmap.josm.gui.NavigatableComponent;
20import org.openstreetmap.josm.gui.mappaint.MapRendererPerformanceTest;
21import org.openstreetmap.josm.io.Compression;
22import org.openstreetmap.josm.io.IllegalDataException;
23import 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 */
30public 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}
Note: See TracBrowser for help on using the repository browser.