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:
InsertPerformanceBenchmark.cs: RunCommitTest private void RunCommitTest() { ConfigureForCommitTest(); InitForCommitTest(); Clean(); System.Console.WriteLine("Storing objects as a bulk:"); Open(); Store(); Close(); Clean(); System.Console.WriteLine("Storing objects with commit after each " + _commitInterval + " objects:"); Open(); StoreWithCommit(); Close(); }
InsertPerformanceBenchmark.cs: ConfigureForCommitTest private void ConfigureForCommitTest() { IConfiguration config = Db4oFactory.Configure(); config.LockDatabaseFile(false); config.WeakReferences(false); // FlushFileBuffers should be set to true to ensure that // the commit information is physically written // and in the correct order config.FlushFileBuffers(false); }
InsertPerformanceBenchmark.cs: InitForCommitTest private void InitForCommitTest() { _count = 100000; _commitInterval = 10000; _depth = 3; _isClientServer = false; }
InsertPerformanceBenchmark.cs: Store private void Store() { StartTimer(); for (int i = 0; i < _count; i++) { Item item = new Item("load", null); for (int j = 1; j < _depth; j++) { item = new Item("load", item); } objectContainer.Store(item); } objectContainer.Commit(); StopTimer("Store " + TotalObjects() + " objects"); }
InsertPerformanceBenchmark.cs: StoreWithCommit private void StoreWithCommit() { StartTimer(); int k = 0; while (k < _count) { for (int i = 0; i < _commitInterval; i++) { Item item = new Item("load", null); k++; for (int j = 1; j < _depth; j++) { item = new Item("load", item); } objectContainer.Store(item); } objectContainer.Commit(); } objectContainer.Commit(); StopTimer("Store " + TotalObjects() + " objects"); }
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
Download example code: