测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using iBoxDB.LocalServer;
using iBoxDB.LocalServer.IO;
namespace iTesting
{
class Program
{
static int threadCount = 20;
static int batchCount = 100;
static string sdbFile = "test1.db3";
static string idbFile = "test1.box";
static void Main(string[] args)
{
Console.WriteLine("输入线程数目");
var sc = Console.ReadLine();
if (!int.TryParse(sc, out threadCount))
{
threadCount = 1;
}
if (threadCount == 0)
{
threadCount = 1;
batchCount = 10000;
}
Console.WriteLine("线程总数 " + threadCount);
TestiBoxDB();
TestSQLite();
//TestSQLite2();
}
public static void TestSQLite()
{
File.Delete(sdbFile);
SQLiteConnection.CreateFile(sdbFile);
DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
conn.ConnectionString = "Data Source=" + sdbFile;
conn.Open();
string sql = "create table [T1] ([ID] INTEGER PRIMARY KEY, [S] TEXT)";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
Stopwatch watch = new Stopwatch();
watch.Start();
int count = 0;
Parallel.For(0, threadCount, (p) =>
{
using (DbConnection conn = factory.CreateConnection())
{
conn.ConnectionString = "Data Source=" + sdbFile;
conn.Open();
var cmd = conn.CreateCommand();
cmd.Parameters.Add(cmd.CreateParameter());
cmd.Parameters.Add(cmd.CreateParameter());
cmd.CommandText = "insert into [T1] ([ID],[S]) values (?,?)";
DbTransaction trans = conn.BeginTransaction();
for (int i = 0; i < batchCount; i++)
{
var id = (p * batchCount) + i;
cmd.Parameters[0].Value = id;
cmd.Parameters[1].Value = id.ToString();
cmd.ExecuteNonQuery();
}
trans.Commit();
var minId = p * batchCount + 0;
var maxId = p * batchCount + batchCount;
cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from [T1] where ID>=" + minId + " and ID<" + maxId;
var reader = cmd.ExecuteReader();
var ti = minId;
while (reader.Read())
{
var iv = reader.GetInt32(0);
if (ti != iv)
{
throw new Exception(ti + " " + iv);
}
ti++;
Interlocked.Add(ref count, 1);
}
if (ti != maxId)
{
throw new Exception();
}
}
}
);
watch.Stop();
if (count != (batchCount * threadCount)) { throw new Exception(); }
Console.WriteLine("SQLite 测试总数:" + count.ToString("N0") + " 用时:" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
}
public static void TestSQLite2()
{
File.Delete(sdbFile);
SQLiteConnection.CreateFile(sdbFile);
DbProviderFactory factory = SQLiteFactory.Instance;
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = "Data Source=" + sdbFile;
conn.Open();
string sql = "create table [T1] ([ID] INTEGER PRIMARY KEY, [S] TEXT)";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
Stopwatch watch = new Stopwatch();
watch.Start();
int count = 0;
Parallel.For(0, threadCount, (p) =>
{
lock ( conn )
{
cmd = conn.CreateCommand();
cmd.Parameters.Add(cmd.CreateParameter());
cmd.Parameters.Add(cmd.CreateParameter());
cmd.CommandText = "insert into [T1] ([ID],[S]) values (?,?)";
DbTransaction trans = conn.BeginTransaction();
for (int i = 0; i < batchCount; i++)
{
var id = (p * batchCount) + i;
cmd.Parameters[0].Value = id;
cmd.Parameters[1].Value = id.ToString();
cmd.ExecuteNonQuery();
}
trans.Commit();
var minId = p * batchCount + 0;
var maxId = p * batchCount + batchCount;
cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from [T1] where ID>=" + minId + " and ID<" + maxId;
var reader = cmd.ExecuteReader();
var ti = minId;
while (reader.Read())
{
var iv = reader.GetInt32(0);
if (ti != iv)
{
throw new Exception(ti + " " + iv);
}
ti++;
Interlocked.Add(ref count, 1);
}
if (ti != maxId)
{
throw new Exception();
}
}
}
);
watch.Stop();
if (count != (batchCount * threadCount)) { throw new Exception(); }
Console.WriteLine("SQLite 测试总数:" + count.ToString("N0") + " 用时:" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
}
public static void TestiBoxDB()
{
iBoxDB.DBDebug.DDebug.DeleteDBFiles( idbFile );
iBoxDBServer server = new iBoxDBServer();
IDataBaseInstance db = server.GetInstance(idbFile);
Stopwatch watch = new Stopwatch();
watch.Start();
int count = 0;
Parallel.For(0, threadCount, (p) =>
{
using (var box = db.Cube() )
{
for (int i = 0; i < batchCount; i++)
{
var id = (p * batchCount) + i;
box.Insert("T1", new T1 { ID=id, S=id.ToString()});
}
box.Commit().Assert();
}
using (var box = db.Cube())
{
var minId = p * batchCount + 0;
var maxId = p * batchCount + batchCount;
var reader = box.BSelect<T1>("from T1 where ID>=? & ID<? order by ID", minId, maxId).GetEnumerator();
var ti = minId;
while (reader.MoveNext())
{
var iv = reader.Current.ID;
if (ti != iv)
{
throw new Exception(ti + " " + iv);
}
ti++;
Interlocked.Add(ref count, 1);
}
if (ti != maxId)
{
throw new Exception();
}
}
}
);
watch.Stop();
if (count != (batchCount * threadCount)) { throw new Exception(); }
Console.WriteLine("iBoxDB 测试总数:" + count.ToString("N0") + " 用时:" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
}
class T1
{
public T1()
{
}
public int ID;
public string S;
}
class iBoxDBServer : LocalDataBaseServer
{
public class C1Config : BoxFileStreamConfig
{
public C1Config()
: base()
{
this.EnsureTable<T1>("T1", "ID");
}
}
protected override DataBaseConfig BuildDataBaseConfig(string name)
{
return new C1Config();
}
}
}
}
