Oracle Berkeley DB 基础知识:http://www.oracle.com/technology/PRoducts/berkeley-db/index.html
Berkeley DB的中国官方博客: Oracle Berkeley DB 中国研发团队的博客 » BDB C# API初探
BDB性能测试程序C#版本
官网说在XP sp3上可以达到45,748条/s,不过那不是用C#写的,估计快一些, 以下是我测的结果:
本机上Intel 单核2.8G, 2G, 大约15337条/S,
服务器至强四核的, 大约47,746条/S,
网上几乎找不到C#的性能测试程序,就是样例也极少,建议大家看看Berkeley DB的中国官方博客
地址:Oracle Berkeley DB 中国研发团队的博客 » BDB C# API初探
以下是小弟写的BDB C#版测试核心程序,大家可以参考
代码
using System;
using System.Collections.Generic;
using System.Text;
using BerkeleyDB;
public class BRBDBDefault : IDisposable
{
BTreeDatabaseConfig btreeDBConfig;
BTreeDatabase btreeDB;
public BRBDBDefault() : this("bdb.db") { }
public BRBDBDefault(string dbName)
{
btreeDBConfig = new BTreeDatabaseConfig();
btreeDBConfig.Duplicates = DuplicatesPolicy.SORTED;
btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; //如果不存在数据库就创建
btreeDBConfig.PageSize = 4 * 1024;//页大小
btreeDBConfig.CacheSize = new CacheInfo(0, 1024 * 1024 * 80, 0);//缓存大小
btreeDB = BTreeDatabase.Open(dbName, btreeDBConfig);
}
public void SetData(int id, string data)
{
DatabaseEntry k = new DatabaseEntry();
DatabaseEntry d = new DatabaseEntry();
k.Data = BitConverter.GetBytes(id);
d.Data = ASCIIEncoding.ASCII.GetBytes(data);
try
{
btreeDB.Put(k, d);
}
catch (System.accessViolationException av)
{
}
catch { }
}
public void SetData(string id, string data)
{
DatabaseEntry k = new DatabaseEntry();
DatabaseEntry d = new DatabaseEntry();
k.Data = ASCIIEncoding.ASCII.GetBytes(id);
d.Data = ASCIIEncoding.ASCII.GetBytes(data);
try
{
btreeDB.Put(k, d);
}
catch (System.AccessViolationException av)
{
}
catch { }
}
public string GetData(int id)
{
DatabaseEntry de = new DatabaseEntry();
de.Data = BitConverter.GetBytes(id);
KeyValuePair<DatabaseEntry, DatabaseEntry> pair = btreeDB.Get(de);
if (pair.Value != null)
return ASCIIEncoding.ASCII.GetString(pair.Value.Data);
return string.Empty;
}
public string GetData(string id)
{
DatabaseEntry de = new DatabaseEntry();
de.Data = ASCIIEncoding.ASCII.GetBytes(id);
KeyValuePair<DatabaseEntry, DatabaseEntry> pair = btreeDB.Get(de);
if (pair.Value != null)
return ASCIIEncoding.ASCII.GetString(pair.Value.Data);
return string.Empty;
}
public string GetDBPath()
{
return btreeDB.FileName + btreeDB.DatabaseName;
}
#region IDisposable 成员
public void Close()
{
btreeDB.Close();
}
public void Dispose()
{
if (btreeDB != null) btreeDB.Dispose();
}
#endregion
}