| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.io.OutputStream; |
| 7 |
import java.util.Date; |
| 8 |
import java.util.concurrent.ArrayBlockingQueue; |
| 9 |
|
| 10 |
import fileIO.ReadWrite; |
| 11 |
import fun.DiskBench; |
| 12 |
import shared.Timer; |
| 13 |
import shared.Tools; |
| 14 |
import structures.ByteBuilder; |
| 15 |
|
| 16 |
public class TestFilesystem { |
| 17 |
|
| 18 |
public static void main(String[] args) throws InterruptedException{ |
| 19 |
if(args.length>0){in=args[0];} |
| 20 |
if(args.length>1){out=args[1];} |
| 21 |
if(args.length>2){log=args[2];} |
| 22 |
if(args.length>3){size=Long.parseLong(args[3]);} |
| 23 |
if(args.length>4){ways=Integer.parseInt(args[4]);} |
| 24 |
if(args.length>5){interval_seconds=Long.parseLong(args[5]);} |
| 25 |
assert(args.length<=6); |
| 26 |
assert(interval_seconds>=0); |
| 27 |
assert(ways>0); |
| 28 |
|
| 29 |
ReadWrite.ZIPLEVEL=2; |
| 30 |
if("null".equalsIgnoreCase(out)){out=null;} |
| 31 |
if("null".equalsIgnoreCase(log)){log="stdout";} |
| 32 |
|
| 33 |
millis=interval_seconds*1000; |
| 34 |
|
| 35 |
Timer t=new Timer(); |
| 36 |
for(int i=0; i<ways; i++){ |
| 37 |
final String fname=in.replaceFirst("#", ""+i); |
| 38 |
File f=new File(fname); |
| 39 |
if(f.exists()){ |
| 40 |
if(Math.abs(f.length()-size)>0.05*size){ |
| 41 |
System.err.println("Existing file "+fname+" is the wrong size ("+f.length()+"); deleting."); |
| 42 |
f.delete(); |
| 43 |
} |
| 44 |
} |
| 45 |
if(!f.exists()){ |
| 46 |
|
| 47 |
long bytes=DiskBench.writeRandomData(fname, size, t, true); |
| 48 |
System.err.println("Wrote "+fname+"; "+bytes+" bytes in "+t.timeInSeconds(3)+" seconds."); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
readQueue.put(new ByteBuilder(65536)); |
| 53 |
readQueue.put(new ByteBuilder(65536)); |
| 54 |
readQueue.put(new ByteBuilder(65536)); |
| 55 |
|
| 56 |
runLoop(); |
| 57 |
} |
| 58 |
|
| 59 |
private static void runLoop(){ |
| 60 |
long nextTime=System.currentTimeMillis(); |
| 61 |
final Timer t=new Timer(); |
| 62 |
|
| 63 |
printLine(header()); |
| 64 |
|
| 65 |
while(true){ |
| 66 |
waitUntil(nextTime); |
| 67 |
final long time=System.currentTimeMillis(); |
| 68 |
t.start(); |
| 69 |
copy(in, out, iteration); |
| 70 |
t.stop(); |
| 71 |
final long copyTime=t.elapsed; |
| 72 |
t.start(); |
| 73 |
testMetadata(); |
| 74 |
t.stop(); |
| 75 |
final long metaTime=t.elapsed; |
| 76 |
t.start(); |
| 77 |
testLs(); |
| 78 |
t.stop(); |
| 79 |
final long lsTime=t.elapsed; |
| 80 |
log(time, copyTime, metaTime, lsTime); |
| 81 |
nextTime+=millis; |
| 82 |
iteration=(iteration+1)%ways; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private static void testMetadata(){ |
| 87 |
{ |
| 88 |
File f=new File("meta"); |
| 89 |
if(!f.exists()){f.mkdir();} |
| 90 |
} |
| 91 |
if(fnames==null){ |
| 92 |
fnames=new String[filesToCreate]; |
| 93 |
for(int i=0; i<filesToCreate; i++){ |
| 94 |
String fname="meta/"+i+".txt"; |
| 95 |
fnames[i]=fname; |
| 96 |
} |
| 97 |
} |
| 98 |
for(String s : fnames){ |
| 99 |
ReadWrite.write(s, s, false); |
| 100 |
} |
| 101 |
for(String s : fnames){ |
| 102 |
InputStream is=ReadWrite.getRawInputStream(s, false); |
| 103 |
try { |
| 104 |
is.read(buffer); |
| 105 |
} catch (IOException e) { |
| 106 |
|
| 107 |
e.printStackTrace(); |
| 108 |
} |
| 109 |
} |
| 110 |
for(String s : fnames){ |
| 111 |
new File(s).delete(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private static int testLs(){ |
| 116 |
|
| 117 |
File dir=new File(System.getProperty("user.dir")); |
| 118 |
|
| 119 |
|
| 120 |
File[] array=dir.listFiles(); |
| 121 |
assert(array!=null) : array; |
| 122 |
assert(array.length>0) : array.length; |
| 123 |
|
| 124 |
int count=0; |
| 125 |
for(File f : array){ |
| 126 |
assert(f.canRead()); |
| 127 |
count++; |
| 128 |
} |
| 129 |
return count; |
| 130 |
} |
| 131 |
|
| 132 |
private static void copy(String from0, final String to, int iter){ |
| 133 |
final String from=from0.replaceFirst("#", ""+iter); |
| 134 |
{ |
| 135 |
File f=new File(out); |
| 136 |
if(f.exists()){f.delete();} |
| 137 |
} |
| 138 |
final InputStream is=ReadWrite.getRawInputStream(from, false); |
| 139 |
WriteThread wt=new WriteThread(to); |
| 140 |
wt.start(); |
| 141 |
for(int len=fillBuffer(is); len>0; len=fillBuffer(is)){ |
| 142 |
ByteBuilder bb=readFetch(); |
| 143 |
bb.append(buffer, 0, len); |
| 144 |
put(bb, wt.writeQueue); |
| 145 |
} |
| 146 |
put(poison, wt.writeQueue); |
| 147 |
try { |
| 148 |
is.close(); |
| 149 |
} catch (IOException e1) { |
| 150 |
|
| 151 |
e1.printStackTrace(); |
| 152 |
} |
| 153 |
while(wt.getState()!=Thread.State.TERMINATED){ |
| 154 |
try { |
| 155 |
wt.join(); |
| 156 |
} catch (InterruptedException e) { |
| 157 |
e.printStackTrace(); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private static int fillBuffer(final InputStream is){ |
| 163 |
int len=0; |
| 164 |
while(len==0){ |
| 165 |
try { |
| 166 |
len = is.read(buffer); |
| 167 |
} catch (IOException e) { |
| 168 |
|
| 169 |
e.printStackTrace(); |
| 170 |
} |
| 171 |
} |
| 172 |
return len; |
| 173 |
} |
| 174 |
|
| 175 |
private static ByteBuilder readFetch(){ |
| 176 |
ByteBuilder bb=null; |
| 177 |
while(bb==null){ |
| 178 |
try { |
| 179 |
bb = readQueue.take(); |
| 180 |
} catch (InterruptedException e) { |
| 181 |
|
| 182 |
e.printStackTrace(); |
| 183 |
} |
| 184 |
} |
| 185 |
assert(bb.length()==0); |
| 186 |
assert(bb!=poison); |
| 187 |
return bb; |
| 188 |
} |
| 189 |
|
| 190 |
private static String header(){ |
| 191 |
return "#time\tsize\tcopyTime\tMB/s\tmetaOps\tmetaTime\tops/s\tlsTime\tdate\n"; |
| 192 |
} |
| 193 |
|
| 194 |
private static void log(final long time, final long copyTime, final long metaTime, final long lsTime){ |
| 195 |
StringBuilder sb=new StringBuilder(); |
| 196 |
sb.append(time).append('\t'); |
| 197 |
sb.append(size).append('\t'); |
| 198 |
sb.append(copyTime/1000000).append('\t'); |
| 199 |
sb.append(Tools.format("%.2f", size*1000/Tools.max(1, (double)copyTime))).append('\t'); |
| 200 |
sb.append(filesToCreate*3).append('\t'); |
| 201 |
sb.append(metaTime/1000000).append('\t'); |
| 202 |
sb.append(Tools.format("%.2f", (3*filesToCreate)*1000000000L/Tools.max(1, (double)metaTime))).append('\t'); |
| 203 |
sb.append(Tools.format("%.2f", lsTime/1000000.0)).append('\t'); |
| 204 |
sb.append(new Date(time)).append('\n'); |
| 205 |
printLine(sb.toString()); |
| 206 |
} |
| 207 |
|
| 208 |
private static void printLine(String line){ |
| 209 |
if(log.equalsIgnoreCase("stdout") || log.equalsIgnoreCase("stdout.txt")){ |
| 210 |
System.out.print(line); |
| 211 |
}else{ |
| 212 |
ReadWrite.writeString(line, log, true); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
private static void waitUntil(final long time){ |
| 217 |
|
| 218 |
synchronized(waiter){ |
| 219 |
long remaining=time-System.currentTimeMillis(); |
| 220 |
while(remaining>9){ |
| 221 |
try { |
| 222 |
waiter.wait(remaining); |
| 223 |
} catch (InterruptedException e) { |
| 224 |
|
| 225 |
e.printStackTrace(); |
| 226 |
} |
| 227 |
remaining=time-System.currentTimeMillis(); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private static void put(ByteBuilder bb, ArrayBlockingQueue<ByteBuilder> q) { |
| 233 |
while(bb!=null){ |
| 234 |
try { |
| 235 |
q.put(bb); |
| 236 |
bb=null; |
| 237 |
} catch (InterruptedException e) { |
| 238 |
|
| 239 |
e.printStackTrace(); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private static final class WriteThread extends Thread { |
| 245 |
|
| 246 |
WriteThread(String fname_){ |
| 247 |
fname=fname_; |
| 248 |
outstream=ReadWrite.getRawOutputStream(fname, false, false); |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public void run(){ |
| 253 |
ByteBuilder bb=writeFetch(); |
| 254 |
while(bb!=poison){ |
| 255 |
boolean success=write(bb); |
| 256 |
bb.setLength(0); |
| 257 |
put(bb, readQueue); |
| 258 |
bb=writeFetch(); |
| 259 |
} |
| 260 |
try { |
| 261 |
outstream.close(); |
| 262 |
} catch (IOException e) { |
| 263 |
|
| 264 |
e.printStackTrace(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
private boolean write(ByteBuilder bb){ |
| 269 |
assert(bb.length()>0); |
| 270 |
try { |
| 271 |
outstream.write(bb.array, 0, bb.length()); |
| 272 |
} catch (IOException e) { |
| 273 |
|
| 274 |
e.printStackTrace(); |
| 275 |
return false; |
| 276 |
} |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
private ByteBuilder writeFetch(){ |
| 281 |
ByteBuilder bb=null; |
| 282 |
while(bb==null){ |
| 283 |
try { |
| 284 |
bb = writeQueue.take(); |
| 285 |
} catch (InterruptedException e) { |
| 286 |
|
| 287 |
e.printStackTrace(); |
| 288 |
} |
| 289 |
} |
| 290 |
return bb; |
| 291 |
} |
| 292 |
|
| 293 |
private final String fname; |
| 294 |
private final OutputStream outstream; |
| 295 |
|
| 296 |
public ArrayBlockingQueue<ByteBuilder> writeQueue=new ArrayBlockingQueue<ByteBuilder>(4); |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
private static int iteration; |
| 302 |
private static String in="foo#.txt"; |
| 303 |
private static String out="bar.txt"; |
| 304 |
private static String log="log.txt"; |
| 305 |
private static int ways=40; |
| 306 |
private static int filesToCreate=1000; |
| 307 |
private static String[] fnames; |
| 308 |
private static long size=5000000000L; |
| 309 |
private static long interval_seconds=3600; |
| 310 |
private static long millis=interval_seconds*1000; |
| 311 |
private static final String waiter="waiter"; |
| 312 |
private static final byte[] buffer=new byte[65536]; |
| 313 |
private static final ByteBuilder poison=new ByteBuilder(); |
| 314 |
private static ArrayBlockingQueue<ByteBuilder> readQueue=new ArrayBlockingQueue<ByteBuilder>(4); |
| 315 |
|
| 316 |
} |