001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.nativerdf.btree;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.util.Random;
011:
012: import junit.framework.TestCase;
013:
014: import info.aduna.io.ByteArrayUtil;
015:
016: /**
017: * @author Arjohn Kampman
018: */
019: public class BTreeBenchmark extends TestCase {
020:
021: private static final int VALUE_COUNT = 100 * 1000;
022:
023: /*-----------*
024: * Variables *
025: *-----------*/
026:
027: private File dataFile;
028:
029: private BTree btree;
030:
031: /*---------*
032: * Methods *
033: *---------*/
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: dataFile = File.createTempFile("btree", null);
039: btree = new BTree(dataFile, 4096, 8);
040: }
041:
042: @Override
043: protected void tearDown() throws Exception {
044: try {
045: btree.close();
046: } finally {
047: try {
048: dataFile.delete();
049: } finally {
050: super .tearDown();
051: }
052: }
053: }
054:
055: public void testAddAscending() throws Exception {
056: Thread.sleep(500L);
057: long startTime = System.currentTimeMillis();
058:
059: addAscending(0L, 1L, VALUE_COUNT);
060: btree.sync();
061:
062: long endTime = System.currentTimeMillis();
063: printTime(startTime, endTime, "testAddAscending");
064: }
065:
066: public void testAddRandom() throws Exception {
067: Thread.sleep(500L);
068: long startTime = System.currentTimeMillis();
069:
070: addRandom(VALUE_COUNT);
071: btree.sync();
072:
073: long endTime = System.currentTimeMillis();
074: printTime(startTime, endTime, "testAddRandom");
075: }
076:
077: public void testUpdate() throws Exception {
078: addAscending(0L, 2L, VALUE_COUNT);
079: btree.sync();
080:
081: Thread.sleep(500L);
082: long startTime = System.currentTimeMillis();
083:
084: update(0L, 8L, VALUE_COUNT / 4, 1L);
085: btree.sync();
086:
087: long endTime = System.currentTimeMillis();
088: printTime(startTime, endTime, "testUpdate");
089: }
090:
091: public void testFullScan() throws Exception {
092: addAscending(0L, 1L, VALUE_COUNT);
093: btree.sync();
094:
095: Thread.sleep(500L);
096: long startTime = System.currentTimeMillis();
097:
098: RecordIterator iter = btree.iterateAll();
099: try {
100: while (iter.next() != null) {
101: }
102: } finally {
103: iter.close();
104: }
105:
106: long endTime = System.currentTimeMillis();
107: printTime(startTime, endTime, "testFullScan");
108: }
109:
110: public void testRangeScan4() throws Exception {
111: testRangeScan(4L);
112: }
113:
114: public void testRangeScan20() throws Exception {
115: testRangeScan(20L);
116: }
117:
118: public void testRangeScan1000() throws Exception {
119: testRangeScan(1000L);
120: }
121:
122: private void testRangeScan(long rangeSize) throws Exception {
123: addAscending(0L, 1L, VALUE_COUNT);
124: btree.sync();
125:
126: byte[] minData = new byte[8];
127: byte[] maxData = new byte[8];
128:
129: Thread.sleep(500L);
130: long startTime = System.currentTimeMillis();
131:
132: for (long minValue = 0L; minValue < VALUE_COUNT; minValue += rangeSize) {
133: ByteArrayUtil.putLong(minValue, minData, 0);
134: ByteArrayUtil.putLong(minValue + rangeSize, maxData, 0);
135:
136: RecordIterator iter = btree.iterateRange(minData, maxData);
137: try {
138: while (iter.next() != null) {
139: }
140: } finally {
141: iter.close();
142: }
143: }
144:
145: long endTime = System.currentTimeMillis();
146: printTime(startTime, endTime, "testRangeScan" + rangeSize);
147: }
148:
149: private void addAscending(long startValue, long increment,
150: int valueCount) throws IOException {
151: long value = startValue;
152:
153: byte[] data = new byte[8];
154: for (int i = 0; i < valueCount; i++) {
155: ByteArrayUtil.putLong(value, data, 0);
156: btree.insert(data);
157: value += increment;
158: }
159: }
160:
161: private void addRandom(int valueCount) throws IOException {
162: Random random = new Random(0L);
163:
164: byte[] data = new byte[8];
165: for (int i = 0; i < valueCount; i++) {
166: ByteArrayUtil.putLong(random.nextLong(), data, 0);
167: btree.insert(data);
168: }
169: }
170:
171: private void update(long startValue, long increment,
172: int valueCount, long updateDelta) throws IOException {
173: long oldValue = startValue;
174: long newValue;
175:
176: byte[] oldData = new byte[8];
177: byte[] newData = new byte[8];
178:
179: for (int i = 0; i < valueCount; i++) {
180: newValue = oldValue += updateDelta;
181:
182: ByteArrayUtil.putLong(oldValue, oldData, 0);
183: ByteArrayUtil.putLong(newValue, newData, 0);
184:
185: btree.insert(newData);
186: btree.remove(oldData);
187:
188: oldValue += increment;
189: }
190: }
191:
192: private void printTime(long startTime, long endTime,
193: String methodName) {
194: System.out.println((endTime - startTime) + " ms for "
195: + methodName + "()");
196: }
197: }
|