你好,游客 登录 注册 搜索
背景:
阅读新闻

无需安装配置,多操作系统支持数据库及性能测试

[日期:2013-03-26] 来源:oschina.net  作者:iboxdb [字体:大 中 小]

测试代码:

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();
            }
        }


    }
}

linux
相关资讯       iBoxDB 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款