Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-test / org / gvsig / data / datastores / vectorial / file / shp / PerformanceTest.java @ 21927

History | View | Annotate | Download (6.36 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp;
2

    
3
import java.io.File;
4
import java.util.Date;
5

    
6
import junit.framework.TestCase;
7

    
8
import org.apache.log4j.FileAppender;
9
import org.apache.log4j.Level;
10
import org.apache.log4j.Logger;
11
import org.apache.log4j.PatternLayout;
12
import org.gvsig.compatible.StringUtil;
13
import org.gvsig.data.datastores.vectorial.file.Utils;
14
import org.gvsig.data.datastores.vectorial.file.shp_jni.JNISHPStore;
15
import org.gvsig.data.datastores.vectorial.file.shp_mem.MemSHPStore;
16

    
17
import es.prodevelop.gvsig.mobile.fmap.driver.vect.shp.ShpReader;
18

    
19
//import es.prodevelop.gvsig.mobile.fmap.layer.newmodel.FPerformanceTestVectorLayer;
20
//import es.prodevelop.gvsig.mobile.fmap.util.ResourceReader;
21
//import es.prodevelop.gvsig.mobile.fmap.util.Utils;
22
//import es.prodevelop.gvsig.mobile.fmap.util.string.StringUtilities;
23

    
24
/**
25
 * This class tests the performance of three SHP drivers implemented for gvSIG Mobile.
26
 * These drivers are compatible with the new data access model (june 2008)
27
 * and will share a lot of code with gvsig Desktop.
28
 * 
29
 * The right testdata folder is needed.
30
 * 
31
 *  
32
 * @author jldominguez
33
 *
34
 */
35
public class PerformanceTest extends TestCase {
36
        
37
        private static Logger logger;
38
        public static String SD_FOLDER = "Tarjeta SD";
39
        
40
        private static String resourceDir = "";
41
        
42
        static {
43
                String base = System.getProperty("java.class.path");
44
                int ind = base.indexOf(File.pathSeparator);
45
                if (ind != -1) base = base.substring(0, ind);
46
                ind = base.lastIndexOf(File.separator);
47
                base = base.substring(0, ind);
48
                //logger.debug("Base directory: " + base);
49
                ind = base.lastIndexOf(File.separator);
50
                resourceDir = base.substring(0, ind) + File.separator + "resources";
51
        }
52

    
53
        /**
54
         * Number of sample executions
55
         */
56
        public static final int REPEAT_TIMES = 5;
57
        
58
        public static final String[] SIZE_TEST_BASE_FILE_NAMES = {
59
                "point_", "polygon_" };
60
        
61
        public static final int[] SIZE_TEST_FILE_SIZES = {
62
                10,
63
                200,
64
                1000,
65
                8000,
66
                40000,
67
                200000 };
68
        
69
        public static final int[] SIZE_TEST_FILE_SIZES_JNI = {
70
                10,
71
                200,
72
                1000,
73
                8000,
74
                40000 };
75
        
76
        public static final int[] SIZE_TEST_FILE_SIZES_MEM = {
77
                10,
78
                200,
79
                1000,
80
                8000 };
81
        public static final String[] FEATURE_SIZE_TEST_FEATURE_TYPES = {
82
                "few", "medium", "many" };
83
        public static final String FEATURE_SIZE_TEST_BASE_FILE_NAME = "VerticesPerFeature.shp";
84
        
85
        public PerformanceTest() {
86
                ShpReader.touch();
87
                initLogger();
88
        }
89

    
90
        public static void main(String[] args) {
91
                junit.textui.TestRunner.run(PerformanceTest.class);
92
        }
93

    
94
        protected void setUp() throws Exception {
95
                super.setUp();
96
        }
97
        
98
        public void testInitialize() {
99

    
100
                new SHPStore();
101
                doit(SHPStore.DATASTORE_NAME);
102
                
103
                new MemSHPStore();
104
                doit(MemSHPStore.DATASTORE_NAME);
105
                
106
                new JNISHPStore();
107
                doit(JNISHPStore.DATASTORE_NAME);
108
                
109
                logger.debug("FIN.");
110
        }
111

    
112
        private void doit(String storename) {
113
                File shpFile = null;
114

    
115
                int[] sizes = SIZE_TEST_FILE_SIZES;
116
                if (storename.compareTo(JNISHPStore.DATASTORE_NAME) == 0) {
117
                        sizes = SIZE_TEST_FILE_SIZES_JNI;
118
                } else {
119
                        if (storename.compareTo(MemSHPStore.DATASTORE_NAME) == 0) {
120
                                sizes = SIZE_TEST_FILE_SIZES_MEM;
121
                        }
122
                }
123
                
124
                // ---------- size test
125
                for (int i=0; i<sizes.length; i++) {
126
                        int size = sizes[i];
127
                        for (int j=0; j<SIZE_TEST_BASE_FILE_NAMES.length; j++) {
128
                                String base_name = SIZE_TEST_BASE_FILE_NAMES[j];
129
                                String filename = base_name + size + ".shp";
130
                                
131
                                if (Utils.USING_PDA) {
132
                                        shpFile = getPdaTestFile(SD_FOLDER, "testdata", filename);
133
                                } else {
134
                                        shpFile = getResourceFile("testdata", filename);
135
                                }
136

    
137
                                performance(shpFile, storename);
138
                        }
139
                }
140

    
141
                // ---------- feature size test
142
                for (int i=0; i<FEATURE_SIZE_TEST_FEATURE_TYPES.length; i++) {
143
                        String feat_type = FEATURE_SIZE_TEST_FEATURE_TYPES[i];
144
                        String filename = feat_type + FEATURE_SIZE_TEST_BASE_FILE_NAME;
145
                        if (Utils.USING_PDA) {
146
                                shpFile = getPdaTestFile(SD_FOLDER, "testdata", filename);
147
                        } else {
148
                                shpFile = getResourceFile("testdata", filename);
149
                        }
150
                        performance(shpFile, storename);
151
                }
152
        }
153

    
154
        private void initLogger() {
155

    
156
                Date now = new Date(System.currentTimeMillis());
157
                String nowstr = now.toString();
158
                
159
                nowstr = StringUtil.replaceAllString(nowstr, " ", "_");
160
                nowstr = StringUtil.replaceAllString(nowstr, ":", "_") + ".txt";
161
                
162
                
163
                String outlogpath = "";
164
                
165
                if (Utils.USING_PDA) {
166
                        outlogpath = getPdaTestFile(SD_FOLDER, "testdata", "performance_test_" + nowstr).getAbsolutePath();
167
                } else {
168
                        outlogpath = getResourceFile("testdata", "performance_test_" + nowstr).getAbsolutePath();
169
                }
170

    
171
                System.out.println("Log file: " + outlogpath);
172

    
173
                try {
174
                    PatternLayout l = new PatternLayout("%5p [%t] - %m%n");
175
                    FileAppender fa = new FileAppender(l, outlogpath, false);
176
                    // ConsoleAppender ca = new ConsoleAppender(l);
177
                    Logger.getRootLogger().setLevel(Level.DEBUG);
178
                    Logger.getRootLogger().addAppender(fa);
179
                    // Logger.getRootLogger().addAppender(ca);
180
                } catch (Exception ex) {
181
                        System.err.println("Error while initializing log4j: " + ex.getMessage());
182
                }
183
                
184
                logger = Logger.getLogger(PerformanceTest.class);
185
                
186
        }
187

    
188
        private void performance(File file, String st_name) {
189

    
190
                if (!file.exists()) return;
191
                PerformanceMeasure lyr = null;
192
                try {
193
                        lyr = new PerformanceMeasure(file, logger, st_name);
194
                        for (int i=0; i<REPEAT_TIMES; i++) {
195
                                // just to see the progress bar
196
                                assertTrue(true);
197
                                lyr.drawStore();
198
                        }
199
                } catch (Exception e) {
200
                        logger.debug("PERFORMANCE_TEST LAYER " + file.getName() + " STORE " + st_name + " ERROR " + e.getMessage());
201
                }
202
                
203
                if (lyr != null) lyr.clean();
204
                System.gc();
205
                lyr = null;
206
                System.gc();
207
        }
208

    
209

    
210
        private boolean acceptablySimilar(double expec, double area, double tol) {
211
                double diff = Math.abs(expec - area);
212
                return ((diff / Math.abs((expec + area) / 2)) < tol);
213
        }
214
        
215
        /**
216
         * Gets a file from the resource folder 
217
         * @param dir
218
         * @param file_name
219
         * @return the file object
220
         */
221
        public static File getResourceFile(String dir, String file_name) {
222
                String filePath = resourceDir + File.separator + dir + File.separator + file_name;
223
                File resp = new File(filePath);
224
                return resp;
225
        }
226
        
227
        /**
228
         * Gets a file from the pda sd card folder 
229
         * @param dir
230
         * @param file_name
231
         * @return the file object
232
         */
233
        public static File getPdaTestFile(String sdfolder, String dir, String file_name) {
234
                String filePath = File.separator + sdfolder + File.separator + dir + File.separator + file_name;
235
                File resp = new File(filePath);
236
                return resp;
237
        }        
238
        
239
}