Debian Codegraph @

current/fun/DiskBench.java

raw symbols SCIP
1 package fun;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.PrintStream;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Random;
10
11 import fileIO.ByteFile;
12 import fileIO.ByteStreamWriter;
13 import fileIO.FileFormat;
14 import fileIO.QuickFile;
15 import fileIO.ReadWrite;
16 import fileIO.TextFile;
17 import shared.Parse;
18 import shared.Parser;
19 import shared.PreParser;
20 import shared.Shared;
21 import shared.Timer;
22 import shared.Tools;
23 import stream.FastaReadInputStream;
24 import structures.ByteBuilder;
25
26 /**
27 * @author Brian Bushnell
28 * @date December 6, 2017
29 *
30 */
31 public class DiskBench {
32
33 public static void main(String[] args){
34 //Start a timer immediately upon code entrance.
35 Timer t=new Timer();
36
37 //Create an instance of this class
38 DiskBench x=new DiskBench(args);
39
40 //Run the object
41 x.process(t);
42
43 //Close the print stream if it was redirected
44 Shared.closeStream(x.outstream);
45 }
46
47 public DiskBench(String[] args){
48
49 {//Preparse block for help, config files, and outstream
50 PreParser pp=new PreParser(args, getClass(), false);
51 args=pp.args;
52 outstream=pp.outstream;
53 }
54
55 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
56 ReadWrite.setZipThreads(Shared.threads());
57
58 Parser parser=new Parser();
59 parser.overwrite=true;
60 for(int i=0; i<args.length; i++){
61 String arg=args[i];
62 String[] split=arg.split("=");
63 String a=split[0].toLowerCase();
64 String b=split.length>1 ? split[1] : null;
65
66 if(a.equals("path")){
67 path=b;
68 if(path==null){path="";}
69 else if(!path.endsWith("/")){path=path+"/";}
70 }else if(a.equals("lines")){
71 maxLines=Long.parseLong(b);
72 if(maxLines<0){maxLines=Long.MAX_VALUE;}
73 }else if(a.equals("data") || a.equals("size")){
74 data=Parse.parseKMG(b);
75 }else if(a.equals("passes")){
76 passes=Integer.parseInt(b);
77 }else if(a.equals("verbose")){
78 verbose=Parse.parseBoolean(b);
79 }else if(a.equals("gzip")){
80 verbose=Parse.parseBoolean(b);
81 }else if(a.equals("mode")){
82 assert(b!=null) : "Bad parameter: "+arg;
83 if(Tools.isDigit(b.charAt(0))){mode=Integer.parseInt(b);}
84 else if("read".equalsIgnoreCase(b) || "r".equalsIgnoreCase(b)){mode=READ;}
85 else if("write".equalsIgnoreCase(b) || "w".equalsIgnoreCase(b)){mode=WRITE;}
86 else if("readwrite".equalsIgnoreCase(b) || "rw".equalsIgnoreCase(b)){mode=READWRITE;}
87 else{assert(false) : "Bad mode: "+arg;}
88 }else if(a.equals("read") || a.equals("r")){
89 mode=READ;
90 }else if(a.equals("write") || a.equals("w")){
91 mode=WRITE;
92 }else if(a.equals("readwrite") || a.equals("rw")){
93 mode=READWRITE;
94 }else if(a.equals("printtid")){
95 printTid=Parse.parseBoolean(b);
96 }else if(a.equals("processbis")){
97 processBis=Parse.parseBoolean(b);
98 }else if(a.equals("preread")){
99 preRead=Parse.parseBoolean(b);
100 }
101
102 else if(a.equals("method")){
103 assert(b!=null) : "Bad parameter: "+arg;
104 if(Tools.isDigit(b.charAt(0))){method=Integer.parseInt(b);}
105 else if("BYTEFILE".equalsIgnoreCase(b) || "bf".equalsIgnoreCase(b)){method=BYTEFILE;}
106 else if("TEXTFILE".equalsIgnoreCase(b) || "tf".equalsIgnoreCase(b)){method=TEXTFILE;}
107 else if("QUICKFILE".equalsIgnoreCase(b) || "qf".equalsIgnoreCase(b)){method=QUICKFILE;}
108 else if("BUFFEREDINPUTSTREAM".equalsIgnoreCase(b) || "bis".equalsIgnoreCase(b)){method=BUFFEREDINPUTSTREAM;}
109 else if("FILEINPUTSTREAM".equalsIgnoreCase(b) || "fis".equalsIgnoreCase(b)){method=FILEINPUTSTREAM;}
110 else if("BUFFEREDINPUTSTREAM2".equalsIgnoreCase(b) || "bis2".equalsIgnoreCase(b)){method=BUFFEREDINPUTSTREAM2;}
111 else if("FILEINPUTSTREAM2".equalsIgnoreCase(b) || "fis2".equalsIgnoreCase(b)){method=FILEINPUTSTREAM2;}
112 else{assert(false) : "Bad mode: "+arg;}
113 }
114 else if("BYTEFILE".equalsIgnoreCase(a) || "bf".equalsIgnoreCase(a)){method=BYTEFILE;}
115 else if("TEXTFILE".equalsIgnoreCase(a) || "tf".equalsIgnoreCase(a)){method=TEXTFILE;}
116 else if("QUICKFILE".equalsIgnoreCase(a) || "qf".equalsIgnoreCase(a)){method=QUICKFILE;}
117 else if("BUFFEREDINPUTSTREAM".equalsIgnoreCase(a) || "bis".equalsIgnoreCase(a)){method=BUFFEREDINPUTSTREAM;}
118 else if("FILEINPUTSTREAM".equalsIgnoreCase(a) || "fis".equalsIgnoreCase(a)){method=FILEINPUTSTREAM;}
119 else if("BUFFEREDINPUTSTREAM2".equalsIgnoreCase(a) || "bis2".equalsIgnoreCase(a)){method=BUFFEREDINPUTSTREAM2;}
120 else if("FILEINPUTSTREAM2".equalsIgnoreCase(a) || "fis2".equalsIgnoreCase(a)){method=FILEINPUTSTREAM2;}
121 else if("buffer".equalsIgnoreCase(a) || "bufferlen".equalsIgnoreCase(a)){
122 bufferlen=(int)Parse.parseKMGBinary(b);
123 }
124
125 else if(parser.parse(arg, a, b)){
126 //do nothing
127 }else{
128 outstream.println("Unknown parameter "+args[i]);
129 assert(false) : "Unknown parameter "+args[i];
130 // throw new RuntimeException("Unknown parameter "+args[i]);
131 }
132 }
133
134 {//Process parser fields
135 overwrite=parser.overwrite;
136 threads=Shared.threads();
137 }
138
139 assert(FastaReadInputStream.settingsOK());
140
141 if(!ByteFile.FORCE_MODE_BF2){
142 ByteFile.FORCE_MODE_BF2=false;
143 ByteFile.FORCE_MODE_BF1=true;
144 }
145
146 File pfile=new File(path);
147 if(!pfile.exists()){pfile.mkdirs();}
148 }
149
150 class WriteThread extends Thread{
151
152 public WriteThread(String fname_, long size_){
153 fname=fname_;
154 size=size_;
155 }
156
157 @Override
158 public void run(){
159 t=new Timer();
160 written=writeRandomData(fname, size, t, overwrite);
161 }
162
163 String fname;
164 long size;
165 long written=0;
166 Timer t;
167
168 }
169
170 public static long writeRandomData(final String fname, final long size, final Timer t, final boolean overwrite){
171 if(t!=null){t.start();}
172 long written=0;
173 final Random randy=Shared.threadLocalRandom();
174 FileFormat ffout=FileFormat.testOutput(fname, FileFormat.TEXT, null, true, overwrite, false, false);
175 ByteStreamWriter bsw=new ByteStreamWriter(ffout);
176 bsw.start();
177 final ByteBuilder bb=new ByteBuilder(66000);
178 final int shift=6;
179 final int shiftsPerRand=32/shift;
180 assert(shiftsPerRand>0);
181 final long limit=size-20-shiftsPerRand*1000;
182 while(written<limit){
183 for(int i=0; i<1000; i+=shiftsPerRand){
184 int x=randy.nextInt();
185 for(int j=0; j<shiftsPerRand; j++){
186 byte b=(byte)(33+x&63);
187 bb.append(b);
188 x>>=shift;
189 }
190 }
191 // for(int i=0; i<1000; i+=shiftsPerRand){
192 // long x=randy.nextLong();
193 // for(int j=0; j<shiftsPerRand; j++){
194 // byte b=(byte)(33+x&63);
195 // bb.append(b);
196 // x>>=shift;
197 // }
198 // }
199 bb.nl();
200 written+=bb.length;
201 bsw.print(bb);
202 bb.clear();
203 }
204 while(written<size-1){
205 bb.append((byte)(33+(randy.nextInt()&63)));
206 written++;
207 }
208 bb.nl();
209 written+=bb.length;
210 bsw.print(bb);
211 bb.clear();
212 bsw.poisonAndWait();
213 File f=new File(fname);
214 long diskSize=(f.length());
215 if(t!=null){t.stop();}
216 return diskSize;
217 }
218
219 class ReadThread extends Thread{
220
221 public ReadThread(String fname_, int tid_){
222 fname=fname_;
223 tid=tid_;
224 }
225
226 @Override
227 public void run(){
228 t=new Timer();
229 FileFormat ffin=FileFormat.testInput(fname, FileFormat.TEXT, null, false, false, false);
230
231 if(method==BYTEFILE){
232 runBf(ffin);
233 }else if(method==QUICKFILE){
234 runQf(ffin);
235 }else if(method==TEXTFILE){
236 runTf(ffin);
237 }else if(method==BUFFEREDINPUTSTREAM){
238 runBis(ffin, true);
239 }else if(method==FILEINPUTSTREAM){
240 runBis(ffin, false);
241 }else if(method==BUFFEREDINPUTSTREAM2){
242 runBis2(ffin, true);
243 }else if(method==FILEINPUTSTREAM2){
244 runBis2(ffin, false);
245 }
246 if(printTid){System.err.print(tid+",");}
247 t.stop();
248 }
249
250 private void runBf(FileFormat ffin){
251 ByteFile bf=ByteFile.makeByteFile(ffin);
252 for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){
253 read+=line.length+1;
254 }
255 }
256
257 private void runQf(FileFormat ffin){
258 QuickFile qf=new QuickFile(ffin);
259 for(byte[] line=qf.nextLine(); line!=null; line=qf.nextLine()){
260 read+=line.length+1;
261 }
262 }
263
264 private void runTf(FileFormat ffin){
265 TextFile tf=new TextFile(ffin);
266 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
267 read+=line.length()+1;
268 }
269 }
270
271 private void runBis(FileFormat ffin, boolean bufferedStream){
272
273 final byte[] buffer=new byte[bufferlen];
274 InputStream is=ReadWrite.getInputStream(ffin.name(), bufferedStream, false);
275
276 for(int r=1; r>0; ){
277 r=0;
278 try {
279 r=is.read(buffer);
280 if(r>0){read+=r;}
281
282 if(processBis){
283 int last=0;
284 for(int i=1; i<r; i++){
285 byte b=buffer[i];
286 if(b=='\n'){
287 cache=Arrays.copyOfRange(buffer, last, i);
288 last=i+1;
289 }
290 }
291 }
292
293 } catch (IOException e) {
294 e.printStackTrace();
295 }
296 }
297
298 try {
299 is.close();
300 } catch (IOException e) {
301 // TODO Auto-generated catch block
302 e.printStackTrace();
303 }
304 }
305
306 private void runBis2(FileFormat ffin, boolean bufferedStream){
307
308 final byte[] buffer=new byte[bufferlen];
309 InputStream is=ReadWrite.getInputStream(ffin.name(), bufferedStream, false);
310
311 list=new ArrayList<byte[]>(800);
312 for(int r=1; r>0; ){
313 r=0;
314 try {
315 r=is.read(buffer);
316 if(r>0){read+=r;}
317
318 if(processBis){
319 int last=0;
320 for(int i=1; i<r; i++){
321 byte b=buffer[i];
322 if(b=='\n'){
323 byte[] line=Arrays.copyOfRange(buffer, last, i);
324 list.add(line);
325 if(list.size()>=800){
326 list=new ArrayList<byte[]>(800);
327 }
328 last=i+1;
329 }
330 }
331 }
332
333 } catch (IOException e) {
334 e.printStackTrace();
335 }
336 }
337
338 try {
339 is.close();
340 } catch (IOException e) {
341 // TODO Auto-generated catch block
342 e.printStackTrace();
343 }
344 }
345
346 byte[] cache;
347 ArrayList<byte[]> list;
348 String fname;
349 long read=0;
350 long lines=0;
351 Timer t;
352 final int tid;
353
354 }
355
356 String[] makeFnames(int pass){
357 String[] fnames=new String[threads];
358 Random randy=new Random();
359 for(int i=0; i<threads; i++){
360 fnames[i]=path+pass+"_"+i+"_"+(System.nanoTime()&0xFFFF)+"_"+randy.nextInt(4096);
361 }
362 return fnames;
363 }
364
365 Timer readWrite(String[] fnamesW, String[] fnamesR){
366 Timer t=new Timer();
367
368 WriteThread[] wta=new WriteThread[threads];
369 long size=data/threads;
370 for(int i=0; i<threads; i++){
371 wta[i]=new WriteThread(fnamesW[i], size);
372 }
373 for(int i=0; i<threads; i++){
374 wta[i].start();
375 }
376
377 ReadThread[] rta=new ReadThread[threads];
378 for(int i=0; i<threads; i++){
379 rta[i]=new ReadThread(fnamesR[i], i);
380 }
381 for(int i=0; i<threads; i++){
382 rta[i].start();
383 }
384
385 for(int i=0; i<threads; i++){
386 while(wta[i].getState()!=Thread.State.TERMINATED){
387 try {
388 wta[i].join();
389 } catch (InterruptedException e) {
390 // TODO Auto-generated catch block
391 e.printStackTrace();
392 }
393 }
394 }
395
396 for(int i=0; i<threads; i++){
397 while(rta[i].getState()!=Thread.State.TERMINATED){
398 try {
399 rta[i].join();
400 } catch (InterruptedException e) {
401 // TODO Auto-generated catch block
402 e.printStackTrace();
403 }
404 }
405 }
406
407 t.stop();
408 return t;
409 }
410
411 Timer write(String[] fnames){
412 Timer t=new Timer();
413 WriteThread[] wta=new WriteThread[threads];
414 long size=data/threads;
415 for(int i=0; i<threads; i++){
416 wta[i]=new WriteThread(fnames[i], size);
417 }
418 for(int i=0; i<threads; i++){
419 wta[i].start();
420 }
421 for(int i=0; i<threads; i++){
422 while(wta[i].getState()!=Thread.State.TERMINATED){
423 try {
424 wta[i].join();
425 } catch (InterruptedException e) {
426 // TODO Auto-generated catch block
427 e.printStackTrace();
428 }
429 }
430 }
431 t.stop();
432 return t;
433 }
434
435 Timer read(String[] fnames){
436
437 Timer t=new Timer();
438
439 if(preRead){
440 ReadThread rt=new ReadThread(fnames[0], 0);
441 rt.start();
442 while(rt.getState()!=Thread.State.TERMINATED){
443 try {
444 rt.join();
445 } catch (InterruptedException e) {
446 // TODO Auto-generated catch block
447 e.printStackTrace();
448 }
449 }
450 }
451
452 ReadThread[] rta=new ReadThread[threads];
453 for(int i=0; i<threads; i++){
454 rta[i]=new ReadThread(fnames[i], i);
455 }
456 for(int i=0; i<threads; i++){
457 rta[i].start();
458 }
459 for(int i=0; i<threads; i++){
460 while(rta[i].getState()!=Thread.State.TERMINATED){
461 try {
462 rta[i].join();
463 } catch (InterruptedException e) {
464 // TODO Auto-generated catch block
465 e.printStackTrace();
466 }
467 }
468 linesInternal+=(rta[i].list==null ? 0 : rta[i].list.size());
469 }
470 t.stop();
471 return t;
472 }
473
474 void delete(String[] fnames){
475 for(String s : fnames){
476 File f=new File(s);
477 if(f.exists()){
478 f.delete();
479 }
480 }
481 }
482
483 void process(Timer t0){
484
485 t0.start();
486 String[] fnamesW=makeFnames(0);
487
488 Timer t=write(fnamesW);
489 String[] fnamesR=fnamesW;
490 fnamesW=null;
491
492 final long initialWriteElapsed=t.elapsed;
493
494 System.err.println("Initial write: \t"+t.toString()+" \t"+Tools.format("%.3f MB/s", (1000.0*data)/t.elapsed));
495
496 for(int pass=0; pass<passes; pass++){
497 if(mode==READWRITE){
498 fnamesW=makeFnames(pass);
499 t=readWrite(fnamesW, fnamesR);
500 delete(fnamesR);
501 fnamesR=fnamesW;
502 fnamesW=null;
503 }else if(mode==READ){
504 t=read(fnamesR);
505 }else{
506 delete(fnamesR);
507 fnamesW=makeFnames(pass);
508 t=write(fnamesW);
509 fnamesR=fnamesW;
510 fnamesW=null;
511 }
512 System.err.println("Pass "+pass+": \t"+t.toString()+" \t"+Tools.format("%.3f MB/s", (1000.0*data)/t.elapsed));
513 }
514 delete(fnamesR);
515
516 t0.stop();
517 System.err.println("Overall: \t"+t0.toString()+" \t"+Tools.format("%.3f MB/s", (1000.0*(data*passes))/(t0.elapsed-initialWriteElapsed)));
518
519 if(errorState){
520 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
521 }
522 }
523
524 /*--------------------------------------------------------------*/
525
526
527 /*--------------------------------------------------------------*/
528
529 private String path="";
530
531 /*--------------------------------------------------------------*/
532
533 private int bufferlen=4096;
534 private long data=8000000000L;
535 private int passes=2;
536
537 public int linesInternal;
538
539 private int threads;
540
541 private long maxLines=Long.MAX_VALUE;
542
543 int mode=READWRITE;
544 static final int READWRITE=1, READ=2, WRITE=3;
545
546 boolean printTid=false;
547 boolean processBis=false;
548 boolean preRead=false;
549
550 int method=BYTEFILE;
551 static final int BYTEFILE=1;
552 static final int TEXTFILE=2;
553 static final int BUFFEREDINPUTSTREAM=3;
554 static final int FILEINPUTSTREAM=4;
555 static final int BUFFEREDINPUTSTREAM2=5;
556 static final int FILEINPUTSTREAM2=6;
557 static final int QUICKFILE=7;
558
559 /*--------------------------------------------------------------*/
560
561 /*--------------------------------------------------------------*/
562
563 private PrintStream outstream=System.err;
564 public static boolean verbose=false;
565 public boolean errorState=false;
566 private boolean overwrite=true;
567
568 }