MongoDB(Default)
Database Transaction Test: None
MongoDB Insert&Counting:1,000,000 AVG: 32,702 objects/s

iBoxDB(File Mode)
Database Transaction Test: Succeeded
iBoxDB Insert&Counting:1,000,000 AVG: 33,368 objects/s
iBoxDB Select&Update:1,000,000 AVG: 16,939 objects/s
iBoxDB Select&UpdateNoIndex:1,000,000 AVG: 25,478 objects/s
iBoxDB Delete:1,000,000 AVG: 22,504 objects/s

iBoxDB(MemoryMappedFile Mode)
Database Transaction Test: Succeeded
iBoxDB Insert&Counting:1,000,000 AVG: 42,236 objects/s
iBoxDB Select&Update:1,000,000 AVG: 17,765 objects/s
iBoxDB Select&UpdateNoIndex:1,000,000 AVG: 26,688 objects/s
iBoxDB Delete:1,000,000 AVG: 23,147 objects/s

iBoxDB(InMemory Mode)
Database Transaction Test: Succeeded
iBoxDB Insert&Counting:1,000,000 AVG: 47,382 objects/s
iBoxDB Select&Update:1,000,000 AVG: 26,806 objects/s
iBoxDB Select&UpdateNoIndex:1,000,000 AVG: 30,969 objects/s
iBoxDB Delete:1,000,000 AVG: 35,092 objects/s


Code

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 iBoxDB.LocalServer;
using iBoxDB.LocalServer.IO;

using MongoDB.Driver;
using MongoDB.Bson;



namespace iTesting
{
    public class MongoDBProgram
    {
        // iBoxDB v1.5
        // MongoDB.Driver v1.8.2.34  Server: mongodb-win32-x86_64-2008plus-2.4.5

        static int threadCount = 100000;
        static int batchCount = 10;

        static long idbFile = 1;
        public static bool UseMMAP = false;
        public static bool UseMem = false;
        static void Main(string[] args)
        {

            Console.WriteLine("MongoDB(Default)");
            Console.WriteLine();
            TestMongoDB();

            System.GC.Collect();
            System.GC.WaitForFullGCComplete();
            Thread.Sleep(1000 * 10);

            UseMMAP = UseMem = false;
            Console.WriteLine("\r\niBoxDB(File Mode)");
            TestiBoxDB();

            System.GC.Collect();
            System.GC.WaitForFullGCComplete();
            Thread.Sleep(1000 * 10);
            UseMMAP = UseMem = false;
            UseMMAP = true;
            Console.WriteLine("\r\niBoxDB(MemoryMappedFile Mode)");
            TestiBoxDB();

            System.GC.Collect();
            System.GC.WaitForFullGCComplete();
            Thread.Sleep(1000 * 10);
            UseMMAP = UseMem = false;
            UseMem = true;
            Console.WriteLine("\r\niBoxDB(InMemory Mode)");
            TestiBoxDB();

            System.GC.Collect();
            System.GC.WaitForFullGCComplete();

            Console.ReadLine();
        }


        public static void TestiBoxDB()
        {
            iBoxDB.DBDebug.DDebug.DeleteDBFiles(idbFile);

            using (iBoxDBServer server = new iBoxDBServer())
            {
                var db = server.GetInstance(idbFile);
                Console.Write("Database Transaction Test: ");
                var box1 = db.Cube();
                box1.Bind("T1").Insert(new T1 { _id = -1, S = (-1).ToString() });

                var box2 = db.Cube();
                box2.Bind("T1").Insert(new T1 { _id = -2, S = (-2).ToString() });

                var transaction1 = box1.Select<T1>("from T1").ToArray();
                var transaction2 = box2.Select<T1>("from T1").ToArray();
                if (transaction1.Length == 1 && transaction1[0]._id == -1 &&
                     transaction2.Length == 1 && transaction2[0]._id == -2)
                {
                    Console.WriteLine("Succeeded");
                }
                else
                {
                    Console.WriteLine("Failed");
                }
                box1.Commit().Assert();
                box2.Commit().Assert();


                Stopwatch watch = new Stopwatch();
                watch.Start();
                int count = 0;
                Parallel.For(0, threadCount, (p) =>
                {
                    List<IOPEntity> list = new List<IOPEntity>();
                    var binder = db.Get().Bind("T1");
                    for (int i = 0; i < batchCount; i++)
                    {
                        var id = (p * batchCount) + i;
                        list.Add(binder.MakeInsert(new T1 { _id = id, S = id.ToString() }));
                    }
                    binder.Batch(list.ToArray()).Assert();

                    using (var box = db.Cube())
                    {
                        var minId = p * batchCount + 0;
                        var maxId = p * batchCount + batchCount;
                        var reader = box.Select<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);
                        }
                        reader.Dispose();
                        if (ti != maxId)
                        {
                            throw new Exception();
                        }
                    }
                }
                );
                watch.Stop();
                if (count != (batchCount * threadCount)) { throw new Exception(count + "  " + (batchCount * threadCount)); }
                var avg = (int)(count / watch.Elapsed.TotalSeconds);
                Console.WriteLine("iBoxDB Insert&Counting:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

                //---------Update-----------------
                watch = new Stopwatch();
                watch.Start();
                count = 0;
                Parallel.For(0, threadCount, (p) =>
                {
                    List<IOPEntity> list = new List<IOPEntity>();
                    using (var box = db.Cube())
                    {
                        for (int i = 0; i < batchCount; i++)
                        {
                            var id = (p * batchCount) + i;
                            var t = box.Bind("T1",id).Select<T1>();
                            t.S = t.S;
                            if (box.Bind("T1", id).Update(t))
                            {
                                Interlocked.Add(ref count, 1);
                            }
                            else
                            {
                                throw new Exception();
                            }
                        }
                        box.Commit().Assert();
                    }
                }
                );
                watch.Stop();
                if (count != (batchCount * threadCount)) { throw new Exception(count + "  " + (batchCount * threadCount)); }
                avg = (int)(count / watch.Elapsed.TotalSeconds);
                Console.WriteLine("iBoxDB Select&Update:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

                //-------------- UpdateNoIndex-------------
                watch = new Stopwatch();
                watch.Start();
                count = 0;
                Parallel.For(0, threadCount, (p) =>
                {
                    List<IOPEntity> list = new List<IOPEntity>();
                    using (var box = db.Cube())
                    {
                        for (int i = 0; i < batchCount; i++)
                        {
                            var id = (p * batchCount) + i;
                            var t = box.Bind("T1", id).Select<T1>();
                            t.S = t.S;
                            if (box.Bind("T1", id).UpdateNoIndex(t))
                            {
                                Interlocked.Add(ref count, 1);
                            }
                            else
                            {
                                throw new Exception();
                            }
                        }
                        box.Commit().Assert();
                    }
                }
                );
                watch.Stop();
                if (count != (batchCount * threadCount)) { throw new Exception(count + "  " + (batchCount * threadCount)); }
                avg = (int)(count / watch.Elapsed.TotalSeconds);
                Console.WriteLine("iBoxDB Select&UpdateNoIndex:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");


                //-------------- Delete ------------
                watch = new Stopwatch();
                watch.Start();
                count = 0;
                Parallel.For(0, threadCount, (p) =>
                {
                    List<IOPEntity> list = new List<IOPEntity>();
                    using (var box = db.Cube())
                    {
                        for (int i = 0; i < batchCount; i++)
                        {
                            var id = (p * batchCount) + i;
                            if (box.Bind("T1", id).Delete())
                            {
                                Interlocked.Add(ref count, 1);
                            }
                            else
                            {
                                throw new Exception();
                            }
                        }
                        box.Commit().Assert();
                    }
                }
                );
                watch.Stop();
                if (count != (batchCount * threadCount)) { throw new Exception(count + "  " + (batchCount * threadCount)); }
                avg = (int)(count / watch.Elapsed.TotalSeconds);
                Console.WriteLine("iBoxDB Delete:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

                if (db.Get().SelectCount("from T1") != 2)
                {
                    throw new Exception();
                }
            }

            iBoxDB.DBDebug.DDebug.DeleteDBFiles(idbFile);
        }


        public static void TestMongoDB()
        {
            var mongo = new MongoClient().GetServer();
            mongo.Connect();
            mongo.DropDatabase("test");
            var db = mongo.GetDatabase("test");
            Console.Write("Database Transaction Test: ");
            var coll1 = db.GetCollection<T1>("T1");
            coll1.Insert(new T1 { _id = -1, S = (-1).ToString() });

            var coll2 = db.GetCollection<T1>("T1");
            coll2.Insert(new T1 { _id = -2, S = (-2).ToString() });

            var transaction1 = coll1.FindAllAs<T1>().ToArray();
            var transaction2 = coll2.FindAllAs<T1>().ToArray();
            if (transaction1.Length == 1 && transaction1[0]._id == -1 &&
                 transaction2.Length == 1 && transaction2[0]._id == -2)
            {
                Console.WriteLine("Succeeded");
            }
            else
            {
                Console.WriteLine("None");
            }

            var coll = db.GetCollection<T1>("T1");
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int count = 0;
            Parallel.For(0, threadCount, (p) =>
            {
                List<T1> list = new List<T1>(batchCount);
                for (int i = 0; i < batchCount; i++)
                {
                    var id = (p * batchCount) + i;
                    list.Add(new T1 { _id = id, S = id.ToString() });
                }
                coll.InsertBatch(list);

                var minId = p * batchCount + 0;
                var maxId = p * batchCount + batchCount;
                BsonDocument bd = new BsonDocument();
                bd.Add("$gte", minId);
                bd.Add("$lt", maxId);
                var q = new QueryDocument();
                q.Add("_id", bd);

                var reader = coll.FindAs<T1>(q).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);
                }
                reader.Dispose();
                if (ti != maxId)
                {
                    throw new Exception();
                }
            }
            );
            watch.Stop();
            if (count != (batchCount * threadCount)) { throw new Exception(count + "  " + (batchCount * threadCount)); }
            var avg = (int)(count / watch.Elapsed.TotalSeconds);
            Console.WriteLine("MongoDB Insert&Counting:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

            mongo.Disconnect();
        }


        public class T1
        {
            public T1() { }
            public int _id { get; set; }
            //[BoxLength(typeof(char),5)]
            public string S;
        }

        class iBoxDBServer : LocalDatabaseServer
        {
            public class C1Config : BoxFileStreamConfig
            {
                public C1Config()
                    : base()
                {
                    this.EnsureTable<T1>("T1", "_id");
                }
            }
            public class C1ConfigInMemory : BoxMemoryStreamConfig
            {
                public C1ConfigInMemory()
                {
                    this.EnsureTable<T1>("T1", "_id");
                    this.FileIncSize = 1024 * 1024 * 256;
                    this.CachePageCount = 1024 * 512;
                }
            }

            public class C1ConfigMMAP : BoxMemoryMappedViewStreamConfig
            {
                public C1ConfigMMAP(string name)
                    : base(name)
                {
                    this.EnsureTable<T1>("T1", "_id");
                    this.FileIncSize = 1024 * 1024 * 256; 
                }
            }

            protected override DatabaseConfig BuildDatabaseConfig(long addr)
            {
                if (UseMem)
                {
                    return new C1ConfigInMemory();
                }
                else if (UseMMAP)
                {
                    return new C1ConfigMMAP(GetNameByAddr(addr));
                }
                else
                {
                    return new C1Config();
                }
            }
        }

    }
}

Last edited Aug 11, 2013 at 7:01 AM by iboxdb, version 4

Comments

No comments yet.