📄 testdbserver.cs
字号:
using System;
using System.Threading;
using Perst;
using System.Diagnostics;
public class TestDbServer
{
class Record : Persistent
{
public string key;
}
const int nThreads = 10;
const int nIndices = 10;
const int nRecords = 10000;
static string toStr(int i)
{
string s = "000000" + i;
return s.Substring(s.Length-6);
}
class ClientThread
{
public Database db;
public int id;
public ClientThread(Database db, int id)
{
this.db = db;
this.id = id;
}
public void run() {
int i;
string tid = "Thread" + id + ":";
Storage storage = db.Storage;
for (i = 0; i < nRecords; i++)
{
storage.BeginThreadTransaction(TransactionMode.Serializable);
Record rec = new Record();
rec.key = tid + toStr(i);
db.AddRecord(rec);
storage.EndThreadTransaction();
}
storage.BeginThreadTransaction(TransactionMode.Serializable);
i = 0;
foreach (Record rec in db.Select(typeof(Record), "key like '" + tid + "%'"))
{
Debug.Assert(rec.key.Equals(tid + toStr(i)));
i += 1;
}
Debug.Assert(i == nRecords);
storage.EndThreadTransaction();
for (i = 0; i < nRecords; i++)
{
storage.BeginThreadTransaction(TransactionMode.Serializable);
string key = tid + toStr(i);
int n = 0;
foreach (Record rec in db.Select(typeof(Record), "key='" + key + "'"))
{
Debug.Assert(rec.key.Equals(key));
n += 1;
}
Debug.Assert(n == 1);
storage.EndThreadTransaction();
}
for (i = 0; i < nRecords; i++)
{
storage.BeginThreadTransaction(TransactionMode.Serializable);
string key = tid + toStr(i);
int n = 0;
foreach (Record rec in db.Select(typeof(Record), "key='" + key + "'", true))
{
Debug.Assert(rec.key.Equals(key));
db.DeleteRecord(rec);
n += 1;
break;
}
Debug.Assert(n == 1);
storage.EndThreadTransaction();
}
}
}
static public void Main(string[] args)
{
Storage storage = StorageFactory.Instance.CreateStorage();
storage.SetProperty("perst.alternative.btree", true);
storage.Open(new NullFile(), 0);
Database db = new Database(storage, true);
storage.BeginThreadTransaction(TransactionMode.Serializable);
db.CreateTable(typeof(Record));
db.CreateIndex(typeof(Record), "key", true);
storage.EndThreadTransaction();
DateTime start = DateTime.Now;
Thread[] threads = new Thread[nThreads];
for (int i = 0; i < nThreads; i++)
{
ClientThread client = new ClientThread(db, i);
threads[i] = new Thread(new ThreadStart(client.run));
threads[i].Start();
}
#if !COMPACT_NET_FRAMEWORK
for (int i = 0; i < nThreads; i++)
{
threads[i].Join();
}
storage.Close();
Console.WriteLine("Elapsed time: " + (DateTime.Now - start));
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -