source: josm/trunk/test/functional/mapcss/performance/PerformanceTest.groovy@ 4126

Last change on this file since 4126 was 4087, checked in by bastiK, 14 years ago

PaintVisitor refactoring, includes hook for external MapRenderers (author: Gubaer)

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1
2// License: GPL. For details, see LICENSE file.
3package mapcss.performance;
4
5import static org.junit.Assert.*
6
7import java.awt.Graphics2D
8import java.awt.image.BufferedImage
9
10import org.junit.*
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.layer.OsmDataLayer
17import org.openstreetmap.josm.gui.mappaint.MapPaintStyles
18import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource
19import org.openstreetmap.josm.gui.preferences.SourceEntry
20import org.openstreetmap.josm.io.OsmReader
21
22/**
23 * This performance tests measures the time for a full run of MapPaintVisitor.visitAll()
24 * against a test data set using a test style.
25 *
26 */
27class PerformanceTest {
28
29 /* ------------------------ configuration section ---------------------------- */
30 /**
31 * The path to the JOSM home environment
32 */
33 def static JOSM_HOME="/my/josm/home/dir"
34
35 /**
36 * The path to the style file used for rendering.
37 */
38 def static STYLE_FILE="/my/test-style.mapcss"
39
40 /**
41 * The data file to be rendered
42 */
43 def static DATA_FILE = "/my/test-data.osm"
44 /* ------------------------ / configuration section ---------------------------- */
45
46 def DataSet ds
47
48 def static boolean checkTestEnvironment() {
49 File f = new File(JOSM_HOME)
50 if (!f.isDirectory() || !f.exists()) {
51 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.")
52 }
53
54 f = new File(STYLE_FILE);
55 if ( !f.isFile() || ! f.exists()) {
56 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.")
57 }
58
59 f = new File(DATA_FILE);
60 if ( !f.isFile() || ! f.exists()) {
61 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.")
62 }
63 }
64
65 @BeforeClass
66 public static void createJOSMFixture(){
67 checkTestEnvironment()
68 System.setProperty("josm.home", JOSM_HOME)
69 MainApplication.main(new String[0])
70 }
71
72 def timed(Closure c){
73 long before = System.currentTimeMillis()
74 c()
75 long after = System.currentTimeMillis()
76 return after - before
77 }
78
79 def loadStyle() {
80 print "Loading style '$STYLE_FILE' ..."
81 MapCSSStyleSource source = new MapCSSStyleSource(
82 new SourceEntry(
83 new File(STYLE_FILE).toURI().toURL().toString(),
84 "test style",
85 "a test style",
86 true // active
87 )
88 )
89 source.loadStyleSource()
90 if (!source.errors.isEmpty()) {
91 fail("Failed to load style file ''${STYLE_FILE}''. Errors: ${source.errors}")
92 }
93 MapPaintStyles.getStyles().clear()
94 MapPaintStyles.getStyles().add(source)
95 println "DONE"
96 }
97
98 def loadData() {
99 print "Loading data file '$DATA_FILE' ..."
100 new File(DATA_FILE).withInputStream {
101 InputStream is ->
102 ds = OsmReader.parseDataSet(is,null)
103 }
104 Main.main.addLayer(new OsmDataLayer(ds,"test layer",null /* no file */));
105 println "DONE"
106 }
107
108 @Test
109 public void measureTimeForStylePreparation() {
110 loadStyle()
111 loadData()
112
113 def mv = Main.map.mapView
114
115 BufferedImage img = mv.createImage(mv.getWidth(), mv.getHeight())
116 Graphics2D g = img.createGraphics()
117 g.setClip(0,0, mv.getWidth(), mv.getHeight())
118 def visitor = new StyledMapRenderer()
119 visitor.setNavigatableComponent(Main.map.mapView)
120 visitor.setGraphics(g)
121
122 print "Rendering ..."
123 long time = timed {
124 visitor.render(ds, false, new Bounds(-90,-180,90,180))
125 }
126 println "DONE"
127 println "data file : ${DATA_FILE}"
128 println "style file: ${STYLE_FILE}"
129 println ""
130 println "Rendering took $time ms."
131 }
132}
Note: See TracBrowser for help on using the repository browser.