Object Structure naturally has a major influence on insert performance: inserting one object, which is a linked list of 1000 members, is much slower than inserting an object with a couple of primitive fields.
The following test compares storing time of similar objects with one different field:
01private void runDifferentObjectsTest(){ 02
03
configure(); 04
init(); 05
System.out.println("Storing " + _count + " objects with " + _depth + " levels of embedded objects:"); 06
07
clean(); 08
System.out.println(" - primitive object with int field"); 09
open(); 10
storeSimplest(); 11
close(); 12
13
open(); 14
System.out.println(" - object with String field"); 15
store(); 16
close(); 17
18
clean(); 19
open(); 20
System.out.println(" - object with StringBuffer field"); 21
storeWithStringBuffer(); 22
close(); 23
24
clean(); 25
open(); 26
System.out.println(" - object with int array field"); 27
storeWithArray(); 28
close(); 29
30
clean(); 31
open(); 32
System.out.println(" - object with ArrayList field"); 33
storeWithArrayList(); 34
close(); 35
36
}
1private void configure(){ 2
Configuration config = Db4o.configure(); 3
config.lockDatabaseFile(false); 4
config.weakReferences(false); 5
config.io(new MemoryIoAdapter()); 6
config.flushFileBuffers(false); 7
}
1private void init(){ 2
_count = 10000; 3
_depth = 3; 4
_isClientServer = false; 5
6
}
01private void storeSimplest(){ 02
startTimer(); 03
for (int i = 0; i < _count ;i++) { 04
SimplestItem item = new SimplestItem(i, null); 05
for (int j = 1; j < _depth; j++) { 06
item = new SimplestItem(i, item); 07
} 08
objectContainer.set(item); 09
} 10
objectContainer.commit(); 11
stopTimer("Store "+ totalObjects() + " objects"); 12
}
01private void store(){ 02
startTimer(); 03
for (int i = 0; i < _count ;i++) { 04
Item item = new Item("load", null); 05
for (int j = 1; j < _depth; j++) { 06
item = new Item("load", item); 07
} 08
objectContainer.set(item); 09
} 10
objectContainer.commit(); 11
stopTimer("Store "+ totalObjects() + " objects"); 12
}
01private void storeWithStringBuffer(){ 02
startTimer(); 03
for (int i = 0; i < _count ;i++) { 04
ItemWithStringBuffer item = new ItemWithStringBuffer(new StringBuffer("load"), null); 05
for (int j = 1; j < _depth; j++) { 06
item = new ItemWithStringBuffer(new StringBuffer("load"), item); 07
} 08
objectContainer.set(item); 09
} 10
objectContainer.commit(); 11
stopTimer("Store "+ totalObjects() + " objects"); 12
}
01private void storeWithArray(){ 02
startTimer(); 03
int[] array = new int[]{1,2,3,4}; 04
for (int i = 0; i < _count ;i++) { 05
int[] id = new int[]{1,2,3,4}; 06
ItemWithArray item = new ItemWithArray(id, null); 07
for (int j = 1; j < _depth; j++) { 08
int[] id1 = new int[]{1,2,3,4}; 09
item = new ItemWithArray(id1, item); 10
} 11
objectContainer.set(item); 12
} 13
objectContainer.commit(); 14
stopTimer("Store "+ totalObjects() + " objects"); 15
}
01private void storeWithArrayList(){ 02
startTimer(); 03
ArrayList idList = new ArrayList(); 04
idList.add(1); 05
idList.add(2); 06
idList.add(3); 07
idList.add(4); 08
for (int i = 0; i < _count ;i++) { 09
ArrayList ids = new ArrayList(); 10
ids.addAll(idList); 11
ItemWithArrayList item = new ItemWithArrayList(ids, null); 12
for (int j = 1; j < _depth; j++) { 13
ArrayList ids1 = new ArrayList(); 14
ids1.addAll(idList); 15
item = new ItemWithArrayList(ids1, item); 16
} 17
objectContainer.set(item); 18
} 19
objectContainer.commit(); 20
stopTimer("Store "+ totalObjects() + " objects"); 21
}
01public static class SimplestItem { 02
03
public int _id; 04
public SimplestItem _child; 05
06
public SimplestItem(){ 07
} 08
09
public SimplestItem(int id, SimplestItem child){ 10
_id = id; 11
_child = child; 12
} 13
}
01public static class ItemWithArray { 02
03
public int[] _id; 04
public ItemWithArray _child; 05
06
public ItemWithArray(){ 07
} 08
09
public ItemWithArray(int[] id, ItemWithArray child){ 10
_id = id; 11
_child = child; 12
} 13
}
01public static class ItemWithArrayList { 02
03
public ArrayList _ids; 04
public ItemWithArrayList _child; 05
06
public ItemWithArrayList(){ 07
} 08
09
public ItemWithArrayList(ArrayList ids, ItemWithArrayList child){ 10
_ids = ids; 11
_child = child; 12
} 13
}
01public static class ItemWithStringBuffer { 02
03
public StringBuffer _name; 04
public ItemWithStringBuffer _child; 05
06
public ItemWithStringBuffer(){ 07
} 08
09
public ItemWithStringBuffer(StringBuffer name, ItemWithStringBuffer child){ 10
_name = name; 11
_child = child; 12
} 13
}
The following results were achieved for the testing configuration:
Java:
Storing 10000 objects with 3 levels of embedded objects:
- primitive object with int field
Store 30000 objects: 820ms
- object with String field
Store 30000 objects: 803ms
- object with StringBuffer field
Store 30000 objects: 2182ms
- object with int array field
Store 30000 objects: 810ms
- object with ArrayList field
Store 30000 objects: 2178ms