Skip to content

Instantly share code, notes, and snippets.

@Bobris
Created June 13, 2011 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bobris/1023779 to your computer and use it in GitHub Desktop.
Save Bobris/1023779 to your computer and use it in GitHub Desktop.
Benchmark of BerkeleyDB
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using BerkeleyDB;
namespace SpeedTest
{
class Program
{
static void Main(string[] args)
{
try
{
String pwd = Environment.CurrentDirectory;
pwd = Path.Combine(pwd, "..");
pwd = Path.Combine(pwd, "..");
pwd = Path.Combine(pwd, "..");
pwd = Path.Combine(pwd, "build_windows");
if (IntPtr.Size == 4)
pwd = Path.Combine(pwd, "Win32");
else
pwd = Path.Combine(pwd, "x64");
#if DEBUG
pwd = Path.Combine(pwd, "Debug");
#else
pwd = Path.Combine(pwd, "Release");
#endif
pwd += ";" + Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", pwd);
}
catch (Exception e)
{
Console.WriteLine(
"Unable to set the PATH environment variable.");
Console.WriteLine(e.Message);
return;
}
Run(false);
}
static void Run(bool doAlsoReads)
{
var sw = new Stopwatch();
sw.Start();
var envCfg = new DatabaseEnvironmentConfig
{
Create = true,
UseMPool = true,
UseLocking = true,
UseLogging = true,
UseTxns = true,
TxnSnapshot = true,
FreeThreaded = true,
Private = true,
RunRecovery = true,
LockSystemCfg = new LockingConfig { DeadlockResolution = DeadlockPolicy.MIN_WRITE },
LogSystemCfg = new LogConfig { InMemory = false, BufferSize = 40 * 1024 * 1024, MaxFileSize = 20 * 1024 * 1024, AutoRemove = true },
MPoolSystemCfg = new MPoolConfig { CacheSize = new CacheInfo(0, 40 * 1024 * 1024, 1) }
};
var env = DatabaseEnvironment.Open("TESTDIR", envCfg);
var dbCfg = new BTreeDatabaseConfig
{
AutoCommit = false,
Creation = CreatePolicy.IF_NEEDED,
Duplicates = DuplicatesPolicy.NONE,
FreeThreaded = true,
ReadUncommitted = false,
UseMVCC = true,
DoChecksum = true,
Env = env
};
var tr = env.BeginTransaction();
var db = BTreeDatabase.Open("speedtest", dbCfg, tr);
tr.Commit();
long pureDataLength = 0;
for (int i = 0; i < 200; i++)
{
long pureDataLengthPrevTr = pureDataLength;
tr = env.BeginTransaction();
for (int j = 0; j < 200; j++)
{
var key = new DatabaseEntry(new[] { (byte)j, (byte)i });
var data = new DatabaseEntry(new byte[1 + i * j]);
db.Put(key, data, tr);
pureDataLength += 2 + 1 + i * j;
}
if (doAlsoReads)
{
var trCheck = env.BeginTransaction();
long pureDataLengthCheck = 0;
var cursor = db.Cursor(trCheck);
while (cursor.MoveNext())
{
var keyValuePair = cursor.Current;
pureDataLengthCheck += keyValuePair.Key.Data.Length + keyValuePair.Value.Data.Length;
}
if (pureDataLengthCheck != pureDataLengthPrevTr)
{
throw new Exception("Transactions are not in serializable mode");
}
trCheck.Commit();
}
tr.Commit(true);
}
db.Close();
env.Checkpoint();
env.Close();
sw.Stop();
Console.WriteLine("Pure data length: {0}", pureDataLength);
Console.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment