Skip to content

Commit c6a92ea

Browse files
committed
Add an example/test showing relative speed of loading and traversing the various implementations.
1 parent 38cba6b commit c6a92ea

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.linguafranca.pwdb.kdbx;
2+
3+
import org.linguafranca.pwdb.Database;
4+
import org.linguafranca.pwdb.Visitor;
5+
import org.linguafranca.pwdb.kdbx.dom.DomDatabaseWrapper;
6+
import org.linguafranca.pwdb.kdbx.jaxb.JaxbDatabase;
7+
import org.linguafranca.pwdb.kdbx.simple.SimpleDatabase;
8+
9+
import java.io.InputStream;
10+
11+
/**
12+
* Example and naive speed test for various implementations.
13+
*
14+
* @author jo
15+
*/
16+
public class OpenDbExample {
17+
18+
private interface DbLoader {
19+
Database load(KdbxCreds creds, InputStream inputStream) throws Exception;
20+
}
21+
22+
private static class SimpleDbLoader implements DbLoader {
23+
@Override
24+
public Database load(KdbxCreds creds, InputStream inputStream) throws Exception {
25+
return SimpleDatabase.load(creds, inputStream);
26+
}
27+
}
28+
29+
private static class DomDbLoader implements DbLoader {
30+
@Override
31+
public Database load(KdbxCreds creds, InputStream inputStream) throws Exception {
32+
return DomDatabaseWrapper.load(creds, inputStream);
33+
}
34+
}
35+
36+
private static class JaxbDbLoader implements DbLoader {
37+
@Override
38+
public Database load(KdbxCreds creds, InputStream inputStream) throws Exception {
39+
return JaxbDatabase.load(creds, inputStream);
40+
}
41+
}
42+
43+
public static void testDb (DbLoader loader, String label, int loads, int iterations) throws Exception {
44+
KdbxCreds creds = new KdbxCreds("123".getBytes());
45+
long start = System.currentTimeMillis();
46+
for (int i=0; i < loads; i++) {
47+
try (InputStream inputStream = OpenDbExample.class.getClassLoader().getResourceAsStream("test1.kdbx")) {
48+
Database database = loader.load(creds, inputStream);
49+
for (int j = 0; j < iterations; j++) {
50+
database.visit(new Visitor.Default() {
51+
});
52+
}
53+
}
54+
}
55+
System.out.printf("%s %d loads %d iterations %d millis%n", label, loads, iterations, System.currentTimeMillis()-start);
56+
}
57+
58+
59+
public static void main(String[] args) throws Exception {
60+
System.out.println("Warming up JVM");
61+
testDb(new SimpleDbLoader(), "Simple", 5, 20);
62+
testDb(new JaxbDbLoader(), "Jaxb", 5, 20);
63+
testDb(new DomDbLoader(), "Dom", 5, 20);
64+
65+
System.out.println("Sleeping");
66+
System.gc();
67+
Thread.sleep(2000);
68+
69+
testDb(new SimpleDbLoader(), "Simple", 5, 20);
70+
testDb(new JaxbDbLoader(), "Jaxb", 5, 20);
71+
testDb(new DomDbLoader(), "Dom", 5, 20);
72+
73+
System.out.println("Sleeping");
74+
System.gc();
75+
Thread.sleep(2000);
76+
77+
testDb(new SimpleDbLoader(), "Simple", 10, 1);
78+
testDb(new JaxbDbLoader(), "Jaxb", 10, 1);
79+
testDb(new DomDbLoader(), "Dom", 10, 1);
80+
81+
System.out.println("Sleeping");
82+
System.gc();
83+
Thread.sleep(2000);
84+
85+
testDb(new SimpleDbLoader(), "Simple", 1, 50);
86+
testDb(new JaxbDbLoader(), "Jaxb", 1, 50);
87+
testDb(new DomDbLoader(), "Dom", 1, 50);
88+
}
89+
}

test/src/main/resources/test1.kdbx

2.31 KB
Binary file not shown.

0 commit comments

Comments
 (0)