| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.HashMap; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.ByteFile1; |
| 9 |
import fileIO.ByteFile2; |
| 10 |
import fileIO.ByteStreamWriter; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import shared.Parse; |
| 14 |
import shared.Parser; |
| 15 |
import shared.PreParser; |
| 16 |
import shared.Shared; |
| 17 |
import shared.Timer; |
| 18 |
import shared.Tools; |
| 19 |
import stream.ConcurrentGenericReadInputStream; |
| 20 |
import stream.FastaReadInputStream; |
| 21 |
import structures.IntList; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class ProcessWebcheck { |
| 29 |
|
| 30 |
public static void main(String[] args){ |
| 31 |
Timer t=new Timer(); |
| 32 |
ProcessWebcheck x=new ProcessWebcheck(args); |
| 33 |
x.process(t); |
| 34 |
|
| 35 |
|
| 36 |
Shared.closeStream(x.outstream); |
| 37 |
} |
| 38 |
|
| 39 |
public ProcessWebcheck(String[] args){ |
| 40 |
|
| 41 |
{ |
| 42 |
PreParser pp=new PreParser(args, outstream, null, false); |
| 43 |
args=pp.args; |
| 44 |
outstream=pp.outstream; |
| 45 |
} |
| 46 |
|
| 47 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 48 |
ReadWrite.setZipThreads(Shared.threads()); |
| 49 |
|
| 50 |
Parser parser=new Parser(); |
| 51 |
for(int i=0; i<args.length; i++){ |
| 52 |
String arg=args[i]; |
| 53 |
String[] split=arg.split("="); |
| 54 |
String a=split[0].toLowerCase(); |
| 55 |
String b=split.length>1 ? split[1] : null; |
| 56 |
|
| 57 |
if(parser.parse(arg, a, b)){ |
| 58 |
|
| 59 |
}else if(a.equals("extendedstats") || a.equals("es")){ |
| 60 |
extendedStats=Parse.parseBoolean(b); |
| 61 |
}else if(a.equals("invalid") || a.equals("outinvalid")){ |
| 62 |
outInvalid=b; |
| 63 |
}else if(a.equals("fail") || a.equals("outfail")){ |
| 64 |
outFail=b; |
| 65 |
}else if(a.equals("ms") || a.equals("millis")){ |
| 66 |
boolean x=Parse.parseBoolean(b); |
| 67 |
ms=(x ? "ms" : ""); |
| 68 |
}else if(a.equals("lines")){ |
| 69 |
maxLines=Long.parseLong(b); |
| 70 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 71 |
}else if(a.equals("verbose")){ |
| 72 |
verbose=Parse.parseBoolean(b); |
| 73 |
ByteFile1.verbose=verbose; |
| 74 |
ByteFile2.verbose=verbose; |
| 75 |
stream.FastaReadInputStream.verbose=verbose; |
| 76 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 77 |
stream.FastqReadInputStream.verbose=verbose; |
| 78 |
ReadWrite.verbose=verbose; |
| 79 |
}else if(!arg.contains("=")){ |
| 80 |
for(String s : arg.split(",")){ |
| 81 |
in1.add(s); |
| 82 |
} |
| 83 |
}else{ |
| 84 |
outstream.println("Unknown parameter "+args[i]); |
| 85 |
assert(false) : "Unknown parameter "+args[i]; |
| 86 |
|
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
{ |
| 91 |
overwrite=parser.overwrite; |
| 92 |
append=parser.append; |
| 93 |
|
| 94 |
if(parser.in1!=null){ |
| 95 |
for(String s : parser.in1.split(",")){ |
| 96 |
in1.add(s); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
out1=parser.out1; |
| 101 |
} |
| 102 |
|
| 103 |
assert(FastaReadInputStream.settingsOK()); |
| 104 |
|
| 105 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 106 |
|
| 107 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 108 |
ByteFile.FORCE_MODE_BF2=false; |
| 109 |
ByteFile.FORCE_MODE_BF1=true; |
| 110 |
} |
| 111 |
|
| 112 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 113 |
|
| 114 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 115 |
outstream.println((out1==null)+", "+out1); |
| 116 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 117 |
} |
| 118 |
|
| 119 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false); |
| 120 |
ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false); |
| 121 |
ffoutFail=FileFormat.testOutput(outFail, FileFormat.TXT, null, true, overwrite, append, false); |
| 122 |
|
| 123 |
ffin1=new ArrayList<FileFormat>(in1.size()); |
| 124 |
for(String s : in1){ |
| 125 |
FileFormat ff=FileFormat.testInput(s, FileFormat.TXT, null, true, true); |
| 126 |
assert(ff!=null) : "Cannot read file "+s; |
| 127 |
ffin1.add(ff); |
| 128 |
} |
| 129 |
assert(ffin1.size()>0) : "No input files."; |
| 130 |
} |
| 131 |
|
| 132 |
void process(Timer t){ |
| 133 |
|
| 134 |
ByteStreamWriter bsw=null; |
| 135 |
if(ffout1!=null){ |
| 136 |
bsw=new ByteStreamWriter(ffout1); |
| 137 |
bsw.start(); |
| 138 |
} |
| 139 |
|
| 140 |
ByteStreamWriter bswInvalid=null; |
| 141 |
if(ffoutInvalid!=null){ |
| 142 |
bswInvalid=new ByteStreamWriter(ffoutInvalid); |
| 143 |
bswInvalid.start(); |
| 144 |
} |
| 145 |
|
| 146 |
ByteStreamWriter bswFail=null; |
| 147 |
if(ffoutFail!=null){ |
| 148 |
bswFail=new ByteStreamWriter(ffoutFail); |
| 149 |
bswFail.start(); |
| 150 |
} |
| 151 |
|
| 152 |
for(FileFormat ff : ffin1){ |
| 153 |
ByteFile bf=ByteFile.makeByteFile(ff); |
| 154 |
process2(bf, bswFail, bswInvalid); |
| 155 |
} |
| 156 |
|
| 157 |
passLatency.shrink(); |
| 158 |
failLatency.shrink(); |
| 159 |
failCode.sort(); |
| 160 |
failCode.shrinkToUnique(); |
| 161 |
|
| 162 |
StringBuilder sb=new StringBuilder(); |
| 163 |
|
| 164 |
ArrayList<String> list=new ArrayList<String>(); |
| 165 |
list.addAll(map.keySet()); |
| 166 |
Shared.sort(list); |
| 167 |
for(String s : list){ |
| 168 |
sb.append(s+"\t"+map.get(s)[0]+"\n"); |
| 169 |
} |
| 170 |
|
| 171 |
if(extendedStats){ |
| 172 |
sb.append('\n'); |
| 173 |
sb.append("Lines_Processed:\t" |
| 174 |
+ linesProcessed).append('\n'); |
| 175 |
sb.append("Invalid_Lines:\t" |
| 176 |
+ (linesProcessed-linesValid)).append('\n'); |
| 177 |
sb.append("Passing:\t" |
| 178 |
+ passLatency.size).append('\n'); |
| 179 |
sb.append("Failing:\t" |
| 180 |
+ failLatency.size).append('\n'); |
| 181 |
sb.append("Avg_Pass_Latency:\t" |
| 182 |
+ (passLatency.size>0 ? Tools.averageInt(passLatency.array) : 0)+ms).append('\n'); |
| 183 |
sb.append("Max_Pass_Latency:\t" |
| 184 |
+ (passLatency.size>0 ? Tools.max(passLatency.array) : 0)+ms).append('\n'); |
| 185 |
sb.append("Avg_Fail_Latency:\t" |
| 186 |
+ (failLatency.size>0 ? Tools.averageInt(failLatency.array) : 0)+ms).append('\n'); |
| 187 |
sb.append("Max_Fail_Latency:\t" |
| 188 |
+ (failLatency.size>0 ? Tools.max(failLatency.array) : 0)+ms).append('\n'); |
| 189 |
sb.append("Observed_Fail_Codes:"); |
| 190 |
for(int i=0; i<failCode.size; i++){ |
| 191 |
sb.append('\t').append(failCode.get(i)); |
| 192 |
} |
| 193 |
sb.append('\n'); |
| 194 |
} |
| 195 |
|
| 196 |
outstream.print(sb); |
| 197 |
|
| 198 |
if(bsw!=null){ |
| 199 |
bsw.print(sb); |
| 200 |
errorState|=bsw.poisonAndWait(); |
| 201 |
} |
| 202 |
if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();} |
| 203 |
if(bswFail!=null){errorState|=bswFail.poisonAndWait();} |
| 204 |
|
| 205 |
t.stop(); |
| 206 |
|
| 207 |
if(errorState){ |
| 208 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
private void process2(ByteFile bf, ByteStreamWriter bswFail, ByteStreamWriter bswInvalid){ |
| 213 |
|
| 214 |
byte[] line=bf.nextLine(); |
| 215 |
|
| 216 |
while(line!=null){ |
| 217 |
if(line.length>0){ |
| 218 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 219 |
linesProcessed++; |
| 220 |
bytesProcessed+=line.length; |
| 221 |
|
| 222 |
boolean valid=(line[0]!='#' && Tools.isDigit(line[line.length-1])); |
| 223 |
String[] split=null; |
| 224 |
if(valid){ |
| 225 |
split=new String(line).split("\\|"); |
| 226 |
valid=(split.length==4); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
int code=-1; |
| 231 |
float latency=-1; |
| 232 |
if(valid){ |
| 233 |
try { |
| 234 |
code=Integer.parseInt(split[2].substring(0, split[2].indexOf(' '))); |
| 235 |
latency=Float.parseFloat(split[3]); |
| 236 |
} catch (Exception e) { |
| 237 |
valid=false; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if(valid){ |
| 242 |
linesValid++; |
| 243 |
|
| 244 |
long[] cnt=map.get(split[2]); |
| 245 |
if(cnt==null){ |
| 246 |
cnt=new long[1]; |
| 247 |
map.put(split[2], cnt); |
| 248 |
} |
| 249 |
cnt[0]++; |
| 250 |
|
| 251 |
int latency2=(int)(latency*1000); |
| 252 |
|
| 253 |
if(code==200){ |
| 254 |
passLatency.add(latency2); |
| 255 |
}else{ |
| 256 |
failLatency.add(latency2); |
| 257 |
failCode.add(code); |
| 258 |
if(bswFail!=null){ |
| 259 |
bswFail.println(line); |
| 260 |
} |
| 261 |
} |
| 262 |
}else{ |
| 263 |
if(bswInvalid!=null){ |
| 264 |
bswInvalid.println(line); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
line=bf.nextLine(); |
| 269 |
} |
| 270 |
|
| 271 |
errorState|=bf.close(); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
private ArrayList<String> in1=new ArrayList<String>(); |
| 280 |
private String out1=null; |
| 281 |
private String outInvalid=null; |
| 282 |
private String outFail=null; |
| 283 |
|
| 284 |
private String ms="ms"; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private long linesProcessed=0; |
| 289 |
private long linesValid=0; |
| 290 |
private long bytesProcessed=0; |
| 291 |
|
| 292 |
private IntList passLatency=new IntList(); |
| 293 |
private IntList failLatency=new IntList(); |
| 294 |
private IntList failCode=new IntList(); |
| 295 |
|
| 296 |
private HashMap<String, long[]> map=new HashMap<String, long[]>(); |
| 297 |
|
| 298 |
private long maxLines=Long.MAX_VALUE; |
| 299 |
private boolean extendedStats=false; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
private final ArrayList<FileFormat> ffin1; |
| 304 |
private final FileFormat ffout1; |
| 305 |
private final FileFormat ffoutInvalid; |
| 306 |
private final FileFormat ffoutFail; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
private PrintStream outstream=System.out; |
| 311 |
public static boolean verbose=false; |
| 312 |
public boolean errorState=false; |
| 313 |
private boolean overwrite=true; |
| 314 |
private boolean append=false; |
| 315 |
|
| 316 |
} |