001: package org.apache.lucene;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.store.Directory;
021: import org.apache.lucene.store.IndexInput;
022: import org.apache.lucene.store.IndexOutput;
023: import org.apache.lucene.store.FSDirectory;
024: import org.apache.lucene.store.RAMDirectory;
025: import org.apache.lucene.util._TestUtil;
026:
027: import java.util.Date;
028: import java.util.Random;
029:
030: class StoreTest {
031: public static void main(String[] args) {
032: try {
033: test(1000, true, true);
034: } catch (Exception e) {
035: e.printStackTrace();
036: }
037: }
038:
039: public static void test(int count, boolean ram, boolean buffered)
040: throws Exception {
041: Random gen = new Random(1251971);
042: int i;
043:
044: Date veryStart = new Date();
045: Date start = new Date();
046:
047: Directory store;
048: if (ram)
049: store = new RAMDirectory();
050: else {
051: String dirName = "test.store";
052: _TestUtil.rmDir(dirName);
053: store = FSDirectory.getDirectory(dirName);
054: }
055:
056: final int LENGTH_MASK = 0xFFF;
057:
058: final byte[] buffer = new byte[LENGTH_MASK];
059:
060: for (i = 0; i < count; i++) {
061: String name = i + ".dat";
062: int length = gen.nextInt() & LENGTH_MASK;
063: byte b = (byte) (gen.nextInt() & 0x7F);
064: //System.out.println("filling " + name + " with " + length + " of " + b);
065:
066: IndexOutput file = store.createOutput(name);
067:
068: if (buffered) {
069: for (int j = 0; j < length; j++)
070: buffer[j] = b;
071: file.writeBytes(buffer, length);
072: } else {
073: for (int j = 0; j < length; j++)
074: file.writeByte(b);
075: }
076:
077: file.close();
078: }
079:
080: store.close();
081:
082: Date end = new Date();
083:
084: System.out.print(end.getTime() - start.getTime());
085: System.out.println(" total milliseconds to create");
086:
087: gen = new Random(1251971);
088: start = new Date();
089:
090: if (!ram)
091: store = FSDirectory.getDirectory("test.store");
092:
093: for (i = 0; i < count; i++) {
094: String name = i + ".dat";
095: int length = gen.nextInt() & LENGTH_MASK;
096: byte b = (byte) (gen.nextInt() & 0x7F);
097: //System.out.println("reading " + name + " with " + length + " of " + b);
098:
099: IndexInput file = store.openInput(name);
100:
101: if (file.length() != length)
102: throw new Exception("length incorrect");
103:
104: byte[] content = new byte[length];
105: if (buffered) {
106: file.readBytes(content, 0, length);
107: // check the buffer
108: for (int j = 0; j < length; j++)
109: if (content[j] != b)
110: throw new Exception("contents incorrect");
111: } else {
112: for (int j = 0; j < length; j++)
113: if (file.readByte() != b)
114: throw new Exception("contents incorrect");
115: }
116:
117: file.close();
118: }
119:
120: end = new Date();
121:
122: System.out.print(end.getTime() - start.getTime());
123: System.out.println(" total milliseconds to read");
124:
125: gen = new Random(1251971);
126: start = new Date();
127:
128: for (i = 0; i < count; i++) {
129: String name = i + ".dat";
130: //System.out.println("deleting " + name);
131: store.deleteFile(name);
132: }
133:
134: end = new Date();
135:
136: System.out.print(end.getTime() - start.getTime());
137: System.out.println(" total milliseconds to delete");
138:
139: System.out.print(end.getTime() - veryStart.getTime());
140: System.out.println(" total milliseconds");
141:
142: store.close();
143: }
144: }
|