Statistics
| Revision:

root / trunk / libraries / libGPE / src-test / org / gvsig / xmlschema / performance / MemoryUsageTest.java @ 12175

History | View | Annotate | Download (4.78 KB)

1
package org.gvsig.xmlschema.performance;
2

    
3
import java.io.File;
4

    
5
import junit.framework.TestCase;
6

    
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
/* CVS MESSAGES:
48
 *
49
 * $Id: MemoryUsageTest.java 12175 2007-06-14 16:15:05Z jorpiell $
50
 * $Log$
51
 * Revision 1.1  2007-06-14 16:15:03  jorpiell
52
 * builds to create the jars generated and add the schema code to the libGPEProject
53
 *
54
 * Revision 1.1  2007/06/14 13:50:07  jorpiell
55
 * The schema jar name has been changed
56
 *
57
 * Revision 1.1  2007/05/30 12:25:48  jorpiell
58
 * Add the element collection
59
 *
60
 * Revision 1.1  2007/05/25 11:55:00  jorpiell
61
 * First update
62
 *
63
 *
64
 */
65
/**
66
 * This test parses some XSD files and calculates the
67
 * used memory for each of them. The parsing process
68
 * must to be implemented by the classes tha inherit
69
 * from this.
70
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
71
 */
72
public abstract class MemoryUsageTest extends TestCase {
73
        private static final int TIMES_TO_LOOP = 5;
74
        private static final float KB = 1024.0f;
75
        private static final float MB = 1048576.0f;
76
        private static final String SKB="KB";
77
        private static final String SMB="MB";
78
                        
79
        private String file1 = "testdata/schemas/localidad.xsd";        
80
        private String file2 = "testdata/schemas/municipios.xsd";
81
        private String file3 = "testdata/schemas/WithSchemaLocationLink.xsd";
82
        private String file4 = "testdata/schemas/WFSDescribeFeatureType.xsd";
83
        private String file5 = "testdata/schemas/CityGML.xsd";
84
        
85
        
86
        public void test1() throws Exception{
87
                parseSchemaXTimes(file1);
88
        }
89
        
90
        public void test2() throws Exception{
91
                parseSchemaXTimes(file2);
92
        }
93
        
94
        public void test3() throws Exception{
95
                parseSchemaXTimes(file3);
96
        }
97
        
98
        public void test4() throws Exception{
99
                parseSchemaXTimes(file4);
100
        }
101
        
102
        public void test5() throws Exception{
103
                parseSchemaXTimes(file5);
104
        }
105

    
106
        
107
        /**
108
         * Parser the same XSD file the number of times 
109
         * specified by TIMES_TO_LOOP
110
         * @param file
111
         * XSD file to parse
112
         * @throws Exception 
113
         */
114
        private void parseSchemaXTimes(String file) throws Exception{
115
                float usedMemory = 0;
116
                float usedTime = 0;
117
                for (int i=0 ; i<TIMES_TO_LOOP ; i++){
118
                        usedMemory = usedMemory + parseSchemaMemory(file);
119
                        usedTime = usedTime + parseSchemaTime(file);
120
                }
121
                usedMemory = usedMemory / TIMES_TO_LOOP;
122
                usedTime = usedTime / TIMES_TO_LOOP;
123
                float divisor = (usedMemory<MB) ? KB : MB;
124
                String sufix = (usedMemory<MB) ? SKB : SMB;        
125
                usedMemory = usedMemory/divisor;                
126
                System.out.println(usedMemory + " " + sufix + " used to parse " + file + " wich size is " 
127
                                + new File(file).length()/KB + " KB in " + usedTime + " milis");
128
        }
129
        
130
        /**
131
         * Parses one schema and returns the used memory
132
         * @param file
133
         * XSD file
134
         * @return
135
         * The memory
136
         * @throws Exception 
137
         */
138
        public float parseSchemaMemory(String file) throws Exception{
139
                //Call the garbage collector
140
                Runtime.getRuntime().gc();
141
                float totalBefore = Runtime.getRuntime().totalMemory();
142
            float freeBefore = Runtime.getRuntime().freeMemory();
143
                //Parse the file
144
            parse(file);
145
                float freeAfter = Runtime.getRuntime().freeMemory();
146
                float totalAfter = Runtime.getRuntime().totalMemory();
147
                float usedMemory = (totalAfter - freeAfter) - (totalBefore - freeBefore);                         
148
                //Call the garbage collector
149
                Runtime.getRuntime().gc();
150
                return usedMemory;                
151
        }
152
        
153
        /**
154
         * Parses one schema and returns the used time
155
         * @param file
156
         * XSD file
157
         * @return
158
         * The memory
159
         * @throws Exception 
160
         */
161
        public long parseSchemaTime(String file) throws Exception{
162
                long t1 = System.currentTimeMillis();
163
                //Parse the file
164
            parse(file);
165
            long t2 = System.currentTimeMillis();
166
                return t2 - t1;                
167
        }
168
        
169
        /**
170
         * Parse the schema
171
         * @param file
172
         * XSD to parse
173
         * @throws Exception 
174
         */
175
        protected abstract void parse(String file) throws Exception;
176
        
177
}