1 package org.apache.lucene; 2 3 18 19 import org.apache.lucene.store.Directory; 20 import org.apache.lucene.store.InputStream; 21 import org.apache.lucene.store.OutputStream; 22 import org.apache.lucene.store.FSDirectory; 23 import org.apache.lucene.store.RAMDirectory; 24 25 import java.util.Date ; 26 import java.util.Random ; 27 28 class StoreTest { 29 public static void main(String [] args) { 30 try { 31 test(1000, true); 32 } catch (Exception e) { 33 System.out.println(" caught a " + e.getClass() + 34 "\n with message: " + e.getMessage()); 35 } 36 } 37 38 public static void test(int count, boolean ram) 39 throws Exception { 40 Random gen = new Random (1251971); 41 int i; 42 43 Date veryStart = new Date (); 44 Date start = new Date (); 45 46 Directory store; 47 if (ram) 48 store = new RAMDirectory(); 49 else 50 store = FSDirectory.getDirectory("test.store", true); 51 52 final int LENGTH_MASK = 0xFFF; 53 54 for (i = 0; i < count; i++) { 55 String name = i + ".dat"; 56 int length = gen.nextInt() & LENGTH_MASK; 57 byte b = (byte)(gen.nextInt() & 0x7F); 58 60 OutputStream file = store.createFile(name); 61 62 for (int j = 0; j < length; j++) 63 file.writeByte(b); 64 65 file.close(); 66 } 67 68 store.close(); 69 70 Date end = new Date (); 71 72 System.out.print(end.getTime() - start.getTime()); 73 System.out.println(" total milliseconds to create"); 74 75 gen = new Random (1251971); 76 start = new Date (); 77 78 if (!ram) 79 store = FSDirectory.getDirectory("test.store", false); 80 81 for (i = 0; i < count; i++) { 82 String name = i + ".dat"; 83 int length = gen.nextInt() & LENGTH_MASK; 84 byte b = (byte)(gen.nextInt() & 0x7F); 85 87 InputStream file = store.openFile(name); 88 89 if (file.length() != length) 90 throw new Exception ("length incorrect"); 91 92 for (int j = 0; j < length; j++) 93 if (file.readByte() != b) 94 throw new Exception ("contents incorrect"); 95 96 file.close(); 97 } 98 99 end = new Date (); 100 101 System.out.print(end.getTime() - start.getTime()); 102 System.out.println(" total milliseconds to read"); 103 104 gen = new Random (1251971); 105 start = new Date (); 106 107 for (i = 0; i < count; i++) { 108 String name = i + ".dat"; 109 store.deleteFile(name); 111 } 112 113 end = new Date (); 114 115 System.out.print(end.getTime() - start.getTime()); 116 System.out.println(" total milliseconds to delete"); 117 118 System.out.print(end.getTime() - veryStart.getTime()); 119 System.out.println(" total milliseconds"); 120 121 store.close(); 122 } 123 } 124 | Popular Tags |