Commit is an expensive operation as it needs to physically access hard drive several times and write changes. However, only commit can ensure that the objects are actually stored in the database and won't be lost.
The following test compares different commit frequencies (one commit for all objects or several commits after a specified amount of objects). The test runs against a hard drive:
01private void RunCommitTest() 02
{ 03
04
ConfigureForCommitTest(); 05
InitForCommitTest(); 06
07
Clean(); 08
System.Console.WriteLine("Storing objects as a bulk:"); 09
Open(); 10
Store(); 11
Close(); 12
13
Clean(); 14
System.Console.WriteLine("Storing objects with commit after each " + _commitInterval + " objects:"); 15
Open(); 16
StoreWithCommit(); 17
Close(); 18
}
01private void ConfigureForCommitTest() 02
{ 03
IConfiguration config = Db4oFactory.Configure(); 04
config.LockDatabaseFile(false); 05
config.WeakReferences(false); 06
// FlushFileBuffers should be set to true to ensure that 07
// the commit information is physically written 08
// and in the correct order 09
config.FlushFileBuffers(false); 10
}
1private void InitForCommitTest() 2
{ 3
_count = 100000; 4
_commitInterval = 10000; 5
_depth = 3; 6
_isClientServer = false; 7
}
01private void Store() 02
{ 03
StartTimer(); 04
for (int i = 0; i < _count; i++) 05
{ 06
Item item = new Item("load", null); 07
for (int j = 1; j < _depth; j++) 08
{ 09
item = new Item("load", item); 10
} 11
objectContainer.Set(item); 12
} 13
objectContainer.Commit(); 14
StopTimer("Store " + TotalObjects() + " objects"); 15
}
01private void StoreWithCommit() 02
{ 03
StartTimer(); 04
int k = 0; 05
while (k < _count) 06
{ 07
for (int i = 0; i < _commitInterval; i++) 08
{ 09
Item item = new Item("load", null); 10
k++; 11
for (int j = 1; j < _depth; j++) 12
{ 13
item = new Item("load", item); 14
} 15
objectContainer.Set(item); 16
} 17
objectContainer.Commit(); 18
} 19
objectContainer.Commit(); 20
StopTimer("Store " + TotalObjects() + " objects"); 21
}
The following results were achieved for the testing configuration:
.NET:
Storing objects as a bulk:
Store 300000 objects: 17748ms
Storing objects with commit after each 10000 objects:
Store 300000 objects: 18163ms