svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / bigfile / TestBigByteBuffer.java @ 40561
History | View | Annotate | Download (7.33 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/* CVS MESSAGES:
|
25 |
*
|
26 |
* $Id: TestBigByteBuffer.java 29631 2009-06-29 16:56:19Z jpiera $
|
27 |
* $Log$
|
28 |
* Revision 1.3 2006-11-06 07:29:59 jaume
|
29 |
* organize imports
|
30 |
*
|
31 |
* Revision 1.2 2006/05/12 12:40:29 fjp
|
32 |
* Driver thread-safe. Se evitan errores aleatorios que aparec?an a veces.
|
33 |
*
|
34 |
* Revision 1.1 2006/01/18 13:01:08 fjp
|
35 |
* BigByteBuffer2 que usa solo 8K y es adecuado para los ?ndices espaciales
|
36 |
*
|
37 |
*
|
38 |
*/
|
39 |
package org.gvsig.utils.bigfile; |
40 |
|
41 |
import java.io.File; |
42 |
import java.io.FileInputStream; |
43 |
import java.io.FileNotFoundException; |
44 |
import java.io.IOException; |
45 |
import java.nio.ByteBuffer; |
46 |
import java.nio.channels.FileChannel; |
47 |
import java.util.Random; |
48 |
|
49 |
/**
|
50 |
* @author fjp
|
51 |
*
|
52 |
*/
|
53 |
public class TestBigByteBuffer { |
54 |
File f = new File("D:/Fjp/chiara/plano/vias.shp"); |
55 |
FileInputStream fin;
|
56 |
FileInputStream fin2;
|
57 |
FileChannel channel2;
|
58 |
|
59 |
BigByteBuffer2 bb; |
60 |
// Hacemos varias pruebas para ver si funciona
|
61 |
// exactamente igual que el original
|
62 |
static int numPruebas = 50000; |
63 |
|
64 |
public class MyThread extends Thread |
65 |
{ |
66 |
|
67 |
String name;
|
68 |
public MyThread(String string) { |
69 |
name = string; |
70 |
|
71 |
} |
72 |
|
73 |
public void run() { |
74 |
try {
|
75 |
prueba2(name, f, numPruebas); |
76 |
} catch (FileNotFoundException e) { |
77 |
// TODO Auto-generated catch block
|
78 |
e.printStackTrace(); |
79 |
} catch (IOException e) { |
80 |
// TODO Auto-generated catch block
|
81 |
e.printStackTrace(); |
82 |
} catch (Exception e) { |
83 |
e.printStackTrace(); |
84 |
} |
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* @param args
|
91 |
* @throws IOException
|
92 |
*/
|
93 |
public static void main(String[] args) throws IOException { |
94 |
// TODO Auto-generated method stub
|
95 |
|
96 |
// prueba1(f, numPruebas);
|
97 |
TestBigByteBuffer test = new TestBigByteBuffer();
|
98 |
test.test(); |
99 |
|
100 |
} |
101 |
|
102 |
public void test() throws IOException |
103 |
{ |
104 |
fin2 = new FileInputStream(f); |
105 |
channel2 = fin2.getChannel(); |
106 |
bb = new BigByteBuffer2(channel2,
|
107 |
FileChannel.MapMode.READ_ONLY, 1024*8); |
108 |
MyThread th = new MyThread("T1:"); |
109 |
th.start(); |
110 |
MyThread th2 = new MyThread("T2: "); |
111 |
th2.start(); |
112 |
|
113 |
|
114 |
System.out.println("Fin de la prueba. " + numPruebas + " iteraciones."); |
115 |
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* @param name
|
120 |
* @param f
|
121 |
* @param numPruebas
|
122 |
* @throws Exception
|
123 |
*/
|
124 |
private void prueba2(String name, File f, int numPruebas) throws Exception { |
125 |
FileInputStream fin;
|
126 |
fin = new FileInputStream(f); |
127 |
// Open the file and then get a channel from the stream
|
128 |
FileChannel channel = fin.getChannel();
|
129 |
int size = (int) channel.size(); |
130 |
// Get the file's size and then map it into memory
|
131 |
ByteBuffer bbCorrect = channel.map(FileChannel.MapMode.READ_ONLY, 0, size); |
132 |
// Chunkeo a 1KB
|
133 |
Random rnd = new Random(); |
134 |
long t1 = System.currentTimeMillis(); |
135 |
|
136 |
for (int i=0; i < numPruebas; i++) |
137 |
{ |
138 |
int pos = rnd.nextInt(size-10); |
139 |
// pos = 0;
|
140 |
bbCorrect.position(pos); |
141 |
int bCorrect = bbCorrect.getInt();
|
142 |
// bb.position(pos);
|
143 |
int bPrueba = bb.getInt(pos);
|
144 |
if (bCorrect != bPrueba)
|
145 |
{ |
146 |
System.err.println(name + "Error de lectura. " + bCorrect + " " + bPrueba); |
147 |
throw new Exception("Error con pos=" + pos); |
148 |
} |
149 |
else
|
150 |
{ |
151 |
System.out.println(name + "Correcto: pos=" + pos + " byte= " + bPrueba); |
152 |
} |
153 |
|
154 |
} |
155 |
close(channel2, fin2, bb); |
156 |
long t2 = System.currentTimeMillis(); |
157 |
System.out.println("T=" + (t2-t1) + "mseconds"); |
158 |
} |
159 |
|
160 |
/**
|
161 |
* @param f
|
162 |
* @param numPruebas
|
163 |
* @throws FileNotFoundException
|
164 |
* @throws IOException
|
165 |
*/
|
166 |
private static void prueba1(File f, int numPruebas) throws FileNotFoundException, IOException { |
167 |
FileInputStream fin;
|
168 |
fin = new FileInputStream(f); |
169 |
// Open the file and then get a channel from the stream
|
170 |
FileChannel channel = fin.getChannel();
|
171 |
int size = (int) channel.size(); |
172 |
// Get the file's size and then map it into memory
|
173 |
ByteBuffer bbCorrect = channel.map(FileChannel.MapMode.READ_ONLY, 0, size); |
174 |
// Chunkeo a 1KB
|
175 |
Random rnd = new Random(); |
176 |
long t1 = System.currentTimeMillis(); |
177 |
FileInputStream fin2 = new FileInputStream(f); |
178 |
FileChannel channel2 = fin2.getChannel();
|
179 |
BigByteBuffer bb = new BigByteBuffer(channel2,
|
180 |
FileChannel.MapMode.READ_ONLY, 1024*1024); |
181 |
for (int i=0; i < numPruebas; i++) |
182 |
{ |
183 |
int pos = rnd.nextInt(size-10); |
184 |
// pos = 2;
|
185 |
|
186 |
byte bCorrect = bbCorrect.get(pos);
|
187 |
byte bPrueba = bb.get(pos);
|
188 |
if (bCorrect != bPrueba)
|
189 |
{ |
190 |
System.err.println("Error de lectura. " + bCorrect + " " + bPrueba); |
191 |
} |
192 |
else
|
193 |
{ |
194 |
// System.out.println("Correcto: pos=" + pos + " byte= " + bPrueba);
|
195 |
} |
196 |
|
197 |
} |
198 |
close(channel2, fin2, bb); |
199 |
System.gc();
|
200 |
long t2 = System.currentTimeMillis(); |
201 |
System.out.println("T=" + (t2-t1) + "mseconds"); |
202 |
} |
203 |
|
204 |
static public synchronized void close(FileChannel channel, FileInputStream fin, BigByteBuffer2 bb) throws IOException { |
205 |
IOException ret = null; |
206 |
|
207 |
try {
|
208 |
channel.close(); |
209 |
} catch (IOException e) { |
210 |
ret = e; |
211 |
} finally {
|
212 |
try {
|
213 |
fin.close(); |
214 |
} catch (IOException e1) { |
215 |
ret = e1; |
216 |
} |
217 |
} |
218 |
|
219 |
if (ret != null) { |
220 |
throw ret;
|
221 |
} |
222 |
// else // Si todo ha ido bien, preparamos para liberar memoria.
|
223 |
// bb = null;
|
224 |
} |
225 |
static public synchronized void close(FileChannel channel, FileInputStream fin, BigByteBuffer bb) throws IOException { |
226 |
IOException ret = null; |
227 |
|
228 |
try {
|
229 |
channel.close(); |
230 |
} catch (IOException e) { |
231 |
ret = e; |
232 |
} finally {
|
233 |
try {
|
234 |
fin.close(); |
235 |
} catch (IOException e1) { |
236 |
ret = e1; |
237 |
} |
238 |
} |
239 |
|
240 |
if (ret != null) { |
241 |
throw ret;
|
242 |
} |
243 |
else // Si todo ha ido bien, preparamos para liberar memoria. |
244 |
bb = null;
|
245 |
} |
246 |
|
247 |
} |