My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package org.gbif.ecat.benchmark;

import org.gbif.utils.file.FileUtils;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.h2.jdbcx.JdbcDataSource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
* Testing the data layout
*/
public class ProfileKvpStores {
private static File data = FileUtils.getClasspathFile("scinames.txt");

private static Connection createDB(boolean inMemory) throws SQLException, IOException {
JdbcDataSource h2 = new JdbcDataSource();
// use data temp dir if disk h2 is required
if (inMemory) {
h2.setURL("jdbc:h2:mem:");
} else {
String dir = "/Users/markus/Desktop/test";
h2.setURL("jdbc:h2:" + dir + "/benchmark");
File df = new File(dir);
if (df.exists()) {
org.apache.commons.io.FileUtils.forceDelete(df);
}
}
h2.setUser("sa");
h2.setPassword("sa");

Connection cn = h2.getConnection();
Statement st = cn.createStatement();
st.execute("CREATE TABLE terms (key VARCHAR(255) PRIMARY KEY, value VARCHAR(64) )");
cn.commit();
return cn;
}

public static void main(String[] args) throws IOException, SQLException {
System.out.println("Reading data file...");
List<String> lookups = new ArrayList<String>(Arrays.asList(new String[]{
"Craspedacusta", "Amphisbaena borelli", "Trypanea mundella", "Aaptolasma triderma", "Ypzia", "Gampsocera hedini", "Heterorachis",
"Leptactina latifolia", "Nitella gracilis var. confervacea f. conformis", "Rhyncolus culinaris", "Coelotes italicus", "Rhyncolus gibts nicht"}));
Map<String, String> dataMap = FileUtils.streamToMap(new FileInputStream(data));
// Map<String,String> dataMap = new HashMap<String,String>();
// for (String l:lookups){
// dataMap.put(l, l);
// }
DataContainer obj = null;
System.gc();
System.out.println("done\n");

obj = testHashSet(dataMap);
testLookupPerformance(obj, lookups, 10000);
waitForSpace();

// obj=null;
// System.gc();
// obj = testTreeSet(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testHashMap(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testTreeMap(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testArrayList(dataMap);
// testLookupPerformance(obj, lookups, 100);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testLinkedList(dataMap);
// testLookupPerformance(obj, lookups, 100);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testPrimitiveArray(dataMap);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testCommonsHashMap(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();

// obj=null;
// System.gc();
// obj = testFastList(dataMap);
// testLookupPerformance(obj, lookups, 100);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testFastMap(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testFastTable(dataMap);
// testLookupPerformance(obj, lookups, 100);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testFastSet(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testTroveSet(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();
//
// obj=null;
// System.gc();
// obj = testTroveMap(dataMap);
// testLookupPerformance(obj, lookups, 10000);
// waitForSpace();

obj = null;
System.gc();
obj = testGoogleSet(dataMap);
testLookupPerformance(obj, lookups, 10000);
waitForSpace();

obj = null;
System.gc();
obj = testGoogleMap(dataMap);
testLookupPerformance(obj, lookups, 10000);
waitForSpace();

obj = null;
System.gc();
obj = testLuceneKVP(dataMap);
testLuceneLookupPerformance(obj, lookups, 10000);
waitForSpace();

obj = null;
System.gc();
obj = testLuceneMinimal(dataMap);
testLuceneLookupPerformance(obj, lookups, 10000);
waitForSpace();

obj = null;
System.gc();
obj = testH2(dataMap, true);
testH2LookupPerformance(obj, lookups, 10000);
waitForSpace();

obj = null;
System.gc();
obj = testH2(dataMap, false);
testH2LookupPerformance(obj, lookups, 10000);
waitForSpace();

System.out.println("Done.");
}

private static DataContainer testArrayList(Map<String, String> dataMap) throws IOException {
List<String> hs = new ArrayList<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into ArrayList");
return new DataContainer(hs);
}

private static DataContainer testCommonsHashMap(Map<String, String> dataMap) throws IOException {
// no generics
org.apache.commons.collections.map.HashedMap hs = new org.apache.commons.collections.map.HashedMap(dataMap);
System.out.println(" " + hs.size() + " records loaded into commons HashedMap");
return new DataContainer(hs);
}

private static DataContainer testFastList(Map<String, String> dataMap) throws IOException {
List<String> hs = new javolution.util.FastList<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into javolution FastList");
return new DataContainer(hs);
}

private static DataContainer testFastMap(Map<String, String> dataMap) throws IOException {
Map<String, String> hs = new javolution.util.FastMap<String, String>(dataMap);
System.out.println(" " + hs.size() + " records loaded into javolution FastMap");
return new DataContainer(hs);
}

private static DataContainer testFastSet(Map<String, String> dataMap) throws IOException {
Set<String> hs = new javolution.util.FastSet<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into javolution FastSet");
return new DataContainer(hs);
}

private static DataContainer testFastTable(Map<String, String> dataMap) throws IOException {
List<String> hs = new javolution.util.FastTable<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into javolution FastTable");
return new DataContainer(hs);
}

private static DataContainer testGoogleArray(Map<String, String> dataMap) throws IOException {
// com.google.common.collect.
// List<String> hs = new ArrayList<String>(FileUtils.fileToList(genera));
// System.out.println(" "+hs.size()+" records loaded into String[] array");
return new DataContainer(null);
}

private static DataContainer testGoogleMap(Map<String, String> dataMap) {
ImmutableMap<String, String> hs = ImmutableMap.copyOf(dataMap);
System.out.println(" " + hs.size() + " records loaded into com.google.common.collect.ImmutableMap");
return new DataContainer(hs);
}

private static DataContainer testGoogleSet(Map<String, String> dataMap) {
ImmutableSet<Set<String>> hs = ImmutableSet.of(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into com.google.common.collect.ImmutableSet");
return new DataContainer(hs);
}

private static DataContainer testH2() throws IOException {
int i = 0;
for (String g : FileUtils.streamToSet(new FileInputStream(data))) {
i++;
}
System.out.println(" " + i + " records loaded into H2");
return new DataContainer(null);
}

private static DataContainer testH2(Map<String, String> generaMap, boolean inMemory) throws SQLException, IOException {
long time = System.currentTimeMillis();

Connection cn = createDB(inMemory);
cn.setAutoCommit(false);
PreparedStatement pst = cn.prepareStatement("INSERT INTO terms (key, value) VALUES (?, ?)");
for (String key : generaMap.keySet()) {
pst.setString(1, key);
pst.setString(2, generaMap.get(key));
pst.execute();
}
cn.commit();
String dbType = "file";
if (inMemory) {
dbType = "in memory";
}
System.out.println(" " + generaMap.size() + " records loaded into H2 " + dbType + " database in " + (System.currentTimeMillis() - time) + " msecs");
return new DataContainer(cn);
}

private static void testH2LookupPerformance(DataContainer data, List<String> lookups, int loops) {
Connection cn = (Connection) data.getData();
int cnt = 0;
boolean contains;
long time = 0;

try {
ResultSet result;

// String sql2 = "select count(*) from terms";
// Statement st2 = cn.createStatement();
// result = st2.executeQuery(sql2);
// result.first();
// System.out.println("Term sql count: "+ result.getInt(1));

String sql = "select value from terms where key=?";
PreparedStatement st = cn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
st.setFetchSize(1);
st.setMaxRows(1);
time = System.currentTimeMillis();

while (cnt < loops) {
for (String l : lookups) {
st.setString(1, l);
result = st.executeQuery();
contains = result.first();
}
cnt++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(" sql equals test of " + loops + " x " + lookups.size() + " terms took " + (System.currentTimeMillis() - time) + " msecs");
}

private static DataContainer testHashMap(Map<String, String> dataMap) throws IOException {
HashMap hs = new HashMap<String, Integer>();
for (String g : dataMap.keySet()) {
hs.put(g, Integer.valueOf(dataMap.get(g)));
}
System.out.println(" " + hs.size() + " records loaded into HashMap");
return new DataContainer(hs);
}

private static DataContainer testHashSet(Map<String, String> dataMap) throws IOException {
Set<String> hs = new HashSet<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into HashSet");
return new DataContainer(hs);
}

private static DataContainer testLinkedList(Map<String, String> dataMap) throws IOException {
List<String> hs = new LinkedList<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into LinkedList");
return new DataContainer(hs);
}

// 276MB set
// 172MB table
private static void testLookupPerformance(DataContainer data, List<String> lookups, int loops) {
int cnt = 0;
boolean contains;
long time;
String keyAddon = "";
try {
Collection c = (Collection) data.getData();
time = System.currentTimeMillis();
while (cnt < loops) {
for (String l : lookups) {
contains = c.contains(l);
}
cnt++;
}
} catch (ClassCastException e) {
Map m = (Map) data.getData();
keyAddon = "(key) ";
time = System.currentTimeMillis();
while (cnt < loops) {
for (String l : lookups) {
contains = m.containsKey(l);
}
cnt++;
}
}
System.out.println(" contains " + keyAddon + "test of " + loops + " x " + lookups.size() + " terms took " + (System.currentTimeMillis() - time) + " msecs");
}

private static DataContainer testLuceneKVP(Map<String, String> dataMap) throws IOException {
long time = System.currentTimeMillis();
Directory memDir = new RAMDirectory();
LuceneUtils.buildKeyValueIndex(dataMap, memDir, true);
System.out.println(" " + dataMap.size() + " records loaded into Lucene KVP index in " + (System.currentTimeMillis() - time) + " msecs");
IndexReader reader = IndexReader.open(memDir, true);
IndexSearcher searcher = new IndexSearcher(reader);
return new DataContainer(searcher);
}

private static void testLuceneLookupPerformance(DataContainer data, List<String> lookups, int loops) {
IndexSearcher searcher = (IndexSearcher) data.getData();
int cnt = 0;
boolean contains;
TopDocs docs;
long time;
Term term = new Term(LuceneUtils.FIELD_KEY);
TermQuery query;
time = System.currentTimeMillis();
try {
while (cnt < loops) {
for (String l : lookups) {
docs = searcher.search(new TermQuery(term.createTerm(l)), 1);
contains = docs.totalHits > 0;
}
cnt++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(" lucene " + loops + " x " + lookups.size() + " TermQueries took " + (System.currentTimeMillis() - time) + " msecs");
}

private static DataContainer testLuceneMinimal(Map<String, String> generaMap) throws IOException {
long time = System.currentTimeMillis();
Directory memDir = new RAMDirectory();
LuceneUtils.buildSimpleIndex(generaMap.keySet(), memDir, true);
System.out.println(" " + generaMap.size() + " records loaded into simple Lucene term index in " + (System.currentTimeMillis() - time) + " msecs");
IndexReader reader = IndexReader.open(memDir, true);
IndexSearcher searcher = new IndexSearcher(reader);
return new DataContainer(searcher);
}

private static DataContainer testPrimitiveArray(Map<String, String> dataMap) throws IOException {
DataContainer dc = new DataContainer(dataMap.keySet().toArray());
System.out.println(" " + dataMap.size() + " records loaded into String[] array");
return dc;
}

private static DataContainer testTreeMap(Map<String, String> dataMap) throws IOException {
TreeMap<String, Integer> hs = new TreeMap<String, Integer>();
for (String g : dataMap.keySet()) {
hs.put(g, Integer.valueOf(dataMap.get(g)));
}
System.out.println(" " + hs.size() + " records loaded into TreeMap");
return new DataContainer(hs);
}

private static DataContainer testTreeSet(Map<String, String> dataMap) throws IOException {
Set<String> hs = new TreeSet<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into TreeSet");
return new DataContainer(hs);
}

private static DataContainer testTroveMap(Map<String, String> dataMap) throws IOException {
Map<String, String> hs = new gnu.trove.THashMap<String, String>(dataMap);
System.out.println(" " + hs.size() + " records loaded into gnu.trove.THashMap");
return new DataContainer(hs);
}

private static DataContainer testTroveSet(Map<String, String> dataMap) throws IOException {
Set<String> hs = new gnu.trove.THashSet<String>(dataMap.keySet());
System.out.println(" " + hs.size() + " records loaded into gnu.trove.THashSet");
return new DataContainer(hs);
}

private static void waitForSpace() {
String str = null;// " ";
System.out.println("Please press ENTER to continue \n");
while (str == null) {
try {
str = new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
// TODO do exception handling
}
}
System.out.println(" running next task...");
}
}

Change log

r3157 by wixner on Jan 12, 2011   Diff
lucene benchmarking
Go to: 
Project members, sign in to write a code review

Older revisions

r334 by mdoer...@gbif.org on Jul 2, 2009   Diff
added google immutable collections
r331 by wixner on Jul 2, 2009   Diff
[No log message]
All revisions of this file

File info

Size: 16003 bytes, 463 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting