001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.nativerdf.datastore;
007:
008: import info.aduna.io.FileUtil;
009:
010: import java.io.File;
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: /**
015: *
016: */
017: public class DataStorePerfTest {
018:
019: public static void main(String[] args) throws Exception {
020: System.out.println("DataStore performance test");
021: System.out.println("==========================");
022:
023: System.out.println("Warming up...");
024: for (int i = 0; i < 3; i++) {
025: runPerformanceTest(10000);
026: System.gc();
027: Thread.sleep(2000);
028: }
029:
030: System.out.println("Starting test...");
031:
032: List<long[]> timeDataList = new ArrayList<long[]>();
033:
034: for (int stringCount = 1000000; stringCount <= 3000000; stringCount += 1000000) {
035: timeDataList.add(runPerformanceTest(stringCount));
036: System.gc();
037: Thread.sleep(1000);
038: }
039:
040: System.out
041: .println("Performance test results, average times in micro seconds");
042: System.out.println("#str\tstore\tgetID\tgetData");
043: for (long[] timeData : timeDataList) {
044: System.out.printf("%d\t%d\t%d\t%d", timeData[0],
045: timeData[1] / 1000, timeData[2] / 1000,
046: timeData[3] / 1000);
047: System.out.println();
048: }
049: }
050:
051: private static long[] runPerformanceTest(int stringCount)
052: throws Exception {
053: System.out.println("Running performance test with "
054: + stringCount + " strings...");
055:
056: long[] timeData = new long[4];
057: timeData[0] = stringCount;
058:
059: File dataDir = FileUtil.createTempDir("datastoretest");
060:
061: try {
062: System.out.println("Initializing data store in directory "
063: + dataDir);
064: DataStore dataStore = new DataStore(dataDir, "strings");
065:
066: System.out.println("Storing strings...");
067: long startTime = System.nanoTime();
068:
069: for (int i = 1; i <= stringCount; i++) {
070: dataStore.storeData(String.valueOf(i).getBytes());
071: }
072:
073: dataStore.sync();
074: long endTime = System.nanoTime();
075: timeData[1] = (endTime - startTime) / stringCount;
076: System.out.println("Strings stored in "
077: + (endTime - startTime) / 1E6 + " ms");
078:
079: System.out.println("Fetching IDs for all strings...");
080: startTime = System.nanoTime();
081:
082: for (int i = 1; i <= stringCount; i++) {
083: int sID = dataStore.getID(String.valueOf(i).getBytes());
084: if (sID == -1) {
085: throw new RuntimeException(
086: "Failed to get ID for string \"" + i + "\"");
087: }
088: }
089:
090: endTime = System.nanoTime();
091: timeData[2] = (endTime - startTime) / stringCount;
092: System.out.println("All IDs fetched in "
093: + (endTime - startTime) / 1E6 + " ms");
094:
095: System.out.println("Fetching data for all IDs...");
096: startTime = System.nanoTime();
097:
098: for (int id = 1; id <= stringCount; id++) {
099: String s = new String(dataStore.getData(id));
100: if (s == null) {
101: throw new RuntimeException(
102: "Failed to get data for ID " + id);
103: }
104: }
105:
106: endTime = System.nanoTime();
107: timeData[3] = (endTime - startTime) / stringCount;
108: System.out.println("All data fetched in "
109: + (endTime - startTime) / 1E6 + " ms");
110:
111: System.out.println("Closing DataStore...");
112: dataStore.close();
113: System.out.println("Done.");
114:
115: return timeData;
116: } finally {
117: FileUtil.deleteDir(dataDir);
118: }
119: }
120: }
|