source: josm/trunk/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSPerformanceTest.groovy @ 7142

Last change on this file since 7142 was 7142, checked in by Don-vip, 7 years ago

see #9691 - make unit test run in headless mode for continuous integration

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.Assert.*
5
6import java.awt.Graphics2D
7import java.awt.image.BufferedImage
8
9import org.junit.*
10import org.openstreetmap.josm.JOSMFixture
11import org.openstreetmap.josm.Main
12import org.openstreetmap.josm.data.Bounds
13import org.openstreetmap.josm.data.osm.DataSet
14import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer
15import org.openstreetmap.josm.gui.MainApplication
16import org.openstreetmap.josm.gui.NavigatableComponent
17import org.openstreetmap.josm.gui.mappaint.MapPaintStyles
18import org.openstreetmap.josm.gui.preferences.SourceEntry
19import org.openstreetmap.josm.gui.preferences.ToolbarPreferences
20import org.openstreetmap.josm.io.Compression
21import org.openstreetmap.josm.io.OsmReader
22
23/**
24 * This performance test measures the time for a full run of MapPaintVisitor.visitAll()
25 * against a test data set using a test style.
26 *
27 */
28class MapCSSPerformanceTest {
29
30    /* ------------------------ configuration section  ---------------------------- */
31    /**
32     * The path to the style file used for rendering.
33     */
34    def static STYLE_FILE="styles/standard/elemstyles.mapcss"
35
36    /**
37     * The data file to be rendered
38     */
39    def static DATA_FILE = "data_nodist/neubrandenburg.osm.bz2"
40    /* ------------------------ / configuration section  ---------------------------- */
41
42    def DataSet ds
43
44    def static boolean checkTestEnvironment() {
45          File f = new File(STYLE_FILE);
46          if ( !f.isFile() || ! f.exists()) {
47              fail("STYLE_FILE refers to '${STYLE_FILE}. This is either not a file or doesn't exist.\nPlease update configuration settings in the unit test file.")
48          }
49    }
50
51    @BeforeClass
52    public static void createJOSMFixture() {
53        JOSMFixture.createPerformanceTestFixture().init();
54        Main.toolbar = new ToolbarPreferences();
55        new MainApplication();
56    }
57
58    def timed(Closure c){
59        long before = System.currentTimeMillis()
60        c()
61        long after = System.currentTimeMillis()
62        return after - before
63    }
64
65    def loadStyle() {
66        print "Loading style '$STYLE_FILE' ..."
67        MapCSSStyleSource source = new MapCSSStyleSource(
68            new SourceEntry(
69                STYLE_FILE,
70                "test style",
71                "a test style",
72                true // active
73            )
74        )
75        source.loadStyleSource()
76        if (!source.errors.isEmpty()) {
77            fail("Failed to load style file ''${STYLE_FILE}''. Errors: ${source.errors}")
78        }
79        MapPaintStyles.getStyles().clear()
80        MapPaintStyles.getStyles().add(source)
81        println "DONE"
82    }
83
84    def loadData() {
85        print "Loading data file '$DATA_FILE' ..."
86        ds = OsmReader.parseDataSet(Compression.getUncompressedFileInputStream(new File(DATA_FILE)), null);
87        println "DONE"
88    }
89
90    @Test
91    public void measureTimeForStylePreparation() {
92        loadStyle()
93        loadData()
94
95        NavigatableComponent mv = new NavigatableComponent();
96        mv.setBounds(0, 0, 1024, 768)
97        BufferedImage img = new BufferedImage(mv.getWidth(), mv.getHeight(), BufferedImage.TYPE_3BYTE_BGR)
98        Graphics2D g = img.createGraphics()
99        g.setClip(0,0, mv.getWidth(), mv.getHeight())
100        StyledMapRenderer visitor = new StyledMapRenderer(g, mv, false)
101
102        print "Rendering ..."
103        long time = timed {
104            visitor.render(ds, false, new Bounds(-90,-180,90,180))
105        }
106        println "DONE"
107        println "data file : ${DATA_FILE}"
108        println "style file: ${STYLE_FILE}"
109        println ""
110        println "Rendering took $time ms."
111    }
112}
Note: See TracBrowser for help on using the repository browser.