001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DiskHashTest.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
008:
009: package org.ozoneDB.DxLib.test;
010:
011: import java.lang.*;
012: import java.util.*;
013: import org.ozoneDB.DxLib.*;
014:
015: class DiskHashTest extends AbstractTest {
016:
017: public static void main(String[] args) throws Exception {
018: int n = Integer.valueOf(args[0]).intValue();
019: int cacheBits = 8;
020: int bufferSize = 5;
021: System.out.println(n + " elements");
022:
023: DxDiskHashMap map = new DxDiskHashMap("/tmp/diskhash/map",
024: bufferSize, cacheBits, 8);
025:
026: map.re_use();
027:
028: // System.out.println( "creating..." );
029: // for (int i=0; i<n; i++) {
030: // map.addForKey( new Integer( i ), new Integer( i ) );
031: // }
032:
033: System.out.println("accessing...");
034: for (int i = 0; i < n; i += 100) {
035: Integer value = (Integer) map.elementForKey(new Integer(i));
036: if (value.intValue() != i) {
037: throw new RuntimeException("");
038: }
039: }
040:
041: System.out.println("iterating...");
042: DxIterator it = map.iterator();
043: while (it.next() != null) {
044: System.out.print(((Integer) it.key()).intValue() + " ");
045: }
046: System.out.flush();
047:
048: System.out.println("iterating...");
049: it = map.iterator();
050: while (it.next() != null) {
051: System.out.print(((Integer) it.object()).intValue() + " ");
052: }
053: System.out.flush();
054:
055: System.out.println("accessing...");
056: for (int i = 0; i < n; i += 100) {
057: Integer value = (Integer) map.elementForKey(new Integer(i));
058: if (value.intValue() != i) {
059: throw new RuntimeException("");
060: }
061: }
062:
063: map.close();
064:
065: // testAll( map, n, bufferSize );
066: }
067:
068: public static void testAll(DxDiskHashMap map, int n, int bufferSize)
069: throws Exception {
070: // add
071: long start = System.currentTimeMillis();
072: for (int i = 0; i < n; i++) {
073: map.addForKey(new Integer(i), new Integer(i));
074: }
075: System.out.println("time (add): "
076: + (System.currentTimeMillis() - start));
077:
078: // re-read (buffered)
079: int nn = Math.min((bufferSize - 2) * 256, n);
080: start = System.currentTimeMillis();
081: for (int i = 0; i < nn; i++) {
082: Integer si = (Integer) map.elementForKey(new Integer(i));
083: if (!si.equals(new Integer(i))) {
084: throw new RuntimeException("corrupted");
085: }
086: }
087: System.out.println("time (re-read buffered: " + nn + "): "
088: + (System.currentTimeMillis() - start));
089:
090: // re-read (buffered)
091: start = System.currentTimeMillis();
092: for (int i = 0; i < nn; i++) {
093: Integer si = (Integer) map.elementForKey(new Integer(i));
094: if (!si.equals(new Integer(i))) {
095: throw new RuntimeException("corrupted");
096: }
097: }
098: System.out.println("time (re-read buffered: " + nn + "): "
099: + (System.currentTimeMillis() - start));
100:
101: // re-read
102: start = System.currentTimeMillis();
103: for (int i = 0; i < n; i++) {
104: Integer si = (Integer) map.elementForKey(new Integer(i));
105: if (!si.equals(new Integer(i))) {
106: throw new RuntimeException("corrupted");
107: }
108: }
109: System.out.println("time (re-read): "
110: + (System.currentTimeMillis() - start));
111:
112: // re-read
113: start = System.currentTimeMillis();
114: for (int i = 0; i < n; i++) {
115: Integer si = (Integer) map.elementForKey(new Integer(i));
116: if (!si.equals(new Integer(i))) {
117: throw new RuntimeException("corrupted");
118: }
119: }
120: System.out.println("time (re-read): "
121: + (System.currentTimeMillis() - start));
122:
123: // close
124: start = System.currentTimeMillis();
125: map.close();
126: System.out.println("time (close): "
127: + (System.currentTimeMillis() - start));
128:
129: map.printStatistics();
130: // map.cleanFiles();
131: }
132:
133: public void stress(DxMap map) {
134: Random rand = new Random(System.currentTimeMillis());
135: for (int j = 0; j < 10; j++) {
136: double gaussian = rand.nextGaussian();
137: int EX = (int) (Math.abs(gaussian) * 10000);
138: System.out.println("EX: " + EX);
139: int VAR = 1000;
140: for (int i = 0; i < 100; i++) {
141: int index = (int) (Math.abs(rand.nextGaussian()) * VAR + EX);
142: // System.out.println ("add: " + index);
143: map.addForKey(new Integer(index), new Integer(index));
144: }
145: int hitCount = 0;
146: for (int i = 0; i < 500; i++) {
147: int index = (int) (Math.abs(rand.nextGaussian()) * VAR + EX);
148: // System.out.println ("add: " + index);
149: if (map.elementForKey(new Integer(index)) != null) {
150: hitCount++;
151: }
152: }
153: System.out.println("hit count:" + hitCount);
154: for (int i = 0; i < 500; i++) {
155: int index = (int) (Math.abs(rand.nextGaussian()) * VAR + EX);
156: // System.out.println ("remove: " + index);
157: map.removeForKey(new Integer(index));
158: }
159: System.out.println("count:" + map.count());
160: }
161:
162: if (map instanceof DxDiskHashMap) {
163: ((DxDiskHashMap) map).printStatistics();
164: ((DxDiskHashMap) map).cleanFiles();
165: }
166: }
167: }
|