File: PerformanceTest.groovy

package info (click to toggle)
josm 0.0.svn5267%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 153,404 kB
  • sloc: java: 152,210; xml: 8,902; perl: 1,346; makefile: 64; sh: 35
file content (132 lines) | stat: -rw-r--r-- 4,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

// License: GPL. For details, see LICENSE file.
package mapcss.performance;

import static org.junit.Assert.*

import java.awt.Graphics2D
import java.awt.image.BufferedImage

import org.junit.*
import org.openstreetmap.josm.Main
import org.openstreetmap.josm.data.Bounds
import org.openstreetmap.josm.data.osm.DataSet
import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer
import org.openstreetmap.josm.gui.MainApplication
import org.openstreetmap.josm.gui.layer.OsmDataLayer
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource
import org.openstreetmap.josm.gui.preferences.SourceEntry
import org.openstreetmap.josm.io.OsmReader

/**
 * This performance tests measures the time for a full run of MapPaintVisitor.visitAll()
 * against a test data set using a test style.
 * 
 */
class PerformanceTest {

    /* ------------------------ configuration section  ---------------------------- */
    /**
    * The path to the JOSM home environment
    */
   def static JOSM_HOME="/my/josm/home/dir"
   
   /**
    * The path to the style file used for rendering.
    */
   def static STYLE_FILE="/my/test-style.mapcss"

   /**
    * The data file to be rendered
    */
   def static DATA_FILE = "/my/test-data.osm"
    /* ------------------------ / configuration section  ---------------------------- */     
    
    def DataSet ds
    
    def static boolean checkTestEnvironment() {
          File f = new File(JOSM_HOME)
          if  (!f.isDirectory() || !f.exists()) {
              fail("JOSM_HOME refers to '${JOSM_HOME}. This is either not a directory or doesn't exist.\nPlease update configuration settings in the unit test file.")              
          }
          
          f = new File(STYLE_FILE);
          if ( !f.isFile() || ! f.exists()) {
              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.")
          }
          
          f = new File(DATA_FILE);
          if ( !f.isFile() || ! f.exists()) {
              fail("DATA_FILE refers to '${DATA_FILE}. This is either not a file or doesn't exist.\nPlease update configuration settings in the unit test file.")
          }
    }
    
    @BeforeClass
    public static void createJOSMFixture(){
        checkTestEnvironment()
        System.setProperty("josm.home", JOSM_HOME)
        MainApplication.main(new String[0])
    }
    
    def timed(Closure c){
        long before = System.currentTimeMillis()
        c()
        long after = System.currentTimeMillis()
        return after - before
    }
    
    def  loadStyle() {
        print "Loading style '$STYLE_FILE' ..."
        MapCSSStyleSource source = new MapCSSStyleSource(
            new SourceEntry(
                new File(STYLE_FILE).toURI().toURL().toString(),
                "test style",
                "a test style",
                true // active
            )
        )
        source.loadStyleSource()
        if (!source.errors.isEmpty()) {
            fail("Failed to load style file ''${STYLE_FILE}''. Errors: ${source.errors}")
        }
        MapPaintStyles.getStyles().clear()
        MapPaintStyles.getStyles().add(source)
        println "DONE"
    }
    
    def loadData() {
        print "Loading data file '$DATA_FILE' ..."
        new File(DATA_FILE).withInputStream {
            InputStream is ->
            ds = OsmReader.parseDataSet(is,null)
        }
        Main.main.addLayer(new OsmDataLayer(ds,"test layer",null /* no file */));
        println "DONE"
    }
    
    @Test
    public void measureTimeForStylePreparation() {
        loadStyle()
        loadData()
        
        def mv = Main.map.mapView
        
        BufferedImage img = mv.createImage(mv.getWidth(), mv.getHeight())
        Graphics2D g = img.createGraphics()
        g.setClip(0,0, mv.getWidth(), mv.getHeight())
        def visitor = new StyledMapRenderer()
        visitor.setNavigatableComponent(Main.map.mapView)
        visitor.setGraphics(g)

        print "Rendering ..."
        long time = timed {
            visitor.render(ds, false, new Bounds(-90,-180,90,180))
        }
        println "DONE"
        println "data file : ${DATA_FILE}"
        println "style file: ${STYLE_FILE}"
        println ""
        println "Rendering took $time ms."       
    }
}