Esempio n. 1
0
        public static StringBuilder PMain()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\n\r\n*Replication: Master-Master");
            try
            {
                DBPlatform.DeleteDB();
                using (var server = new ReplicableServer())
                {
                    var masterA = server.GetInstance(ReplicableServer.MasterA_DBAddress);
                    var masterB = server.GetInstance(ReplicableServer.MasterB_DBAddress);

                    // send to MasterB_Address
                    using (var box = masterA.Cube(ReplicableServer.MasterB_DBAddress))
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            box.Bind("Member").Insert(
                                new Member()
                            {
                                ID   = box.NewId(Member.IncTableID, 1) * 1000 + ReplicableServer.MasterA_DBAddress,
                                Name = "A" + i
                            }
                                );
                            //or Key = [ID,Address]
                            //m.ID =  box.NewId(Member.IncTableID, 1) ;
                            //m.Address = box.LocalAddress;
                            //box.Bind("Member").Insert(m);
                        }

                        box.Commit().Assert();
                    }
                    //send to MasterA_Address
                    using (var box = masterB.Cube(ReplicableServer.MasterA_DBAddress))
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            box.Bind("Member").Insert(
                                new Member()
                            {
                                ID   = box.NewId(Member.IncTableID, 1) * 1000 + ReplicableServer.MasterB_DBAddress,
                                Name = "B" + i
                            }
                                );
                        }
                        box.Commit().Assert();
                    }

                    List <Package> buffer;
                    var            recycler = (InMemoryBoxRecycler)masterA.GetBoxRecycler();
                    lock (recycler.GetPackage())
                    {
                        buffer = new List <Package>(recycler.GetPackage());
                        recycler.GetPackage().Clear();
                    }
                    recycler = (InMemoryBoxRecycler)masterB.GetBoxRecycler();
                    lock (recycler.GetPackage())
                    {
                        buffer.AddRange(recycler.GetPackage());
                        recycler.GetPackage().Clear();
                    }
                    foreach (var p in buffer)
                    {
                        if (p.Socket.DestAddress == ReplicableServer.MasterA_DBAddress)
                        {
                            (new BoxData(p.OutBox)).MasterReplicate(masterA);
                        }
                        if (p.Socket.DestAddress == ReplicableServer.MasterB_DBAddress)
                        {
                            (new BoxData(p.OutBox)).MasterReplicate(masterB);
                        }
                    }

                    // checking
                    sb.Append("\r\nMasterA Address is " + masterA.LocalAddress + " \r\n");
                    using (var box = masterA.Cube())
                    {
                        foreach (var o in box.Select <Member>("from Member", null))
                        {
                            sb.Append(o.Name + " , ");
                        }
                    }
                    sb.Append("\r\nMasterB Address is " + masterB.LocalAddress + " \r\n");
                    using (var box = masterB.Cube())
                    {
                        foreach (var o in box.Select <Member>("from Member", null))
                        {
                            sb.Append(o.Name + " , ");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                sb.Append(ex.ToString() + "\r\n");
            }
            return(sb);
        }
Esempio n. 2
0
        public static StringBuilder ReplicationSpeed(int threadCount, int time)
        {
            StringBuilder sb = new StringBuilder();

            try
            {
                DBPlatform.DeleteDB();
                using (var server = new ReplicableServer())
                {
                    var masterA = server.GetInstance(ReplicableServer.MasterA_DBAddress);
                    var slaveA  = server.GetInstance(ReplicableServer.SlaveA_DBAddress);
                    var masterB = server.GetInstance(ReplicableServer.MasterB_DBAddress);

                    var data = ((InMemoryBoxRecycler)masterA.GetBoxRecycler()).GetBoxDataAndClear();

                    BoxData.SlaveReplicate(slaveA, data).Assert();
                    BoxData.MasterReplicate(masterB, data).Assert();

                    var objectCount = 10;

                    double slaveSec  = 0;
                    double masterSec = 0;
                    for (var t = 0; t < time; t++)
                    {
                        DBPlatform.For(0, threadCount,
                                       (i) =>
                        {
                            using (var box = masterA.Cube(ReplicableServer.MasterB_DBAddress))
                            {
                                for (var o = 0; o < objectCount; o++)
                                {
                                    var m = new Member
                                    {
                                        ID   = box.NewId(0, 1),
                                        Name = i.ToString() + "_" + o.ToString(),
                                        Age  = 1
                                    };
                                    box.Bind("TSpeed").Insert(m);
                                }
                                box.Commit().Assert();
                            }
                        }
                                       );


                        data = ((InMemoryBoxRecycler)masterA.GetBoxRecycler()).GetBoxDataAndClear();

                        DDebug.StartWatch();
                        BoxData.SlaveReplicate(slaveA, data).Assert();
                        slaveSec += DDebug.StopWatch().TotalSeconds;


                        DDebug.StartWatch();
                        BoxData.MasterReplicate(masterB, data).Assert();
                        masterSec += DDebug.StopWatch().TotalSeconds;
                    }
                    sb.Append("\r\n\r\n*Replicate " + (threadCount * time).ToString("#,#") + " transactions, totals " + (threadCount * objectCount * time).ToString("#,#") + " objects");
                    var avg = (threadCount * objectCount * time) / slaveSec;
                    sb.Append("\r\nSlaveReplicationSpeed " + slaveSec + "s, AVG " + avg.ToString("#,#") + " o/sec");

                    avg = (threadCount * objectCount * time) / masterSec;
                    sb.Append("\r\nMasterReplicationSpeed " + masterSec + "s, AVG " + avg.ToString("#,#") + " o/sec");

                    int count = 0;

                    DDebug.StartWatch();
                    for (var t = 0; t < time; t++)
                    {
                        DBPlatform.For(0, threadCount,
                                       (i) =>
                        {
                            for (var dbc = 0; dbc < 2; dbc++)
                            {
                                using (var box = dbc == 0 ? slaveA.Cube() : masterB.Cube())
                                {
                                    for (var o = 0; o < objectCount; o++)
                                    {
                                        long ID = i * objectCount + o + 1;
                                        ID     += (t * threadCount * objectCount);
                                        var mem = box.Bind("TSpeed", ID).Select <Member>();
                                        if (mem.ID != ID)
                                        {
                                            throw new Exception();
                                        }
                                        Interlocked.Add(ref count, mem.Age);
                                    }
                                }
                            }
                        }
                                       );
                    }

                    if (count != (threadCount * objectCount * time * 2))
                    {
                        throw new Exception();
                    }
                    masterSec = DDebug.StopWatch().TotalSeconds;
                    avg       = count / masterSec;
                    sb.Append("\r\nLookup just after replication " + masterSec + "s, AVG " + avg.ToString("#,#") + " o/sec");
                }
            }
            catch (Exception ex)
            {
                sb.Append(ex.ToString());
            }
            return(sb);
        }
Esempio n. 3
0
        public static StringBuilder PMain()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\n*Replication: Master-Slave");
            try
            {
                DBPlatform.DeleteDB();
                using (var server = new ReplicableServer())
                {
                    var masterA = server.GetInstance(ReplicableServer.MasterA_DBAddress);
                    var slaveA  = server.GetInstance(ReplicableServer.SlaveA_DBAddress);

                    using (var box = masterA.Cube())
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            box.Bind("Member").Insert(
                                new Member()
                            {
                                ID   = box.NewId(Member.IncTableID, 1),
                                Name = "LN " + i
                            }
                                );
                        }
                        box.Commit().Assert();
                    }

                    // Post database's changes to slave
                    var recycler = (InMemoryBoxRecycler)masterA.GetBoxRecycler();
                    lock (recycler.GetPackage())
                    {
                        foreach (var p in recycler.GetPackage())
                        {
                            if (p.Socket.SourceAddress == ReplicableServer.MasterA_DBAddress)
                            {
                                (new BoxData(p.OutBox)).SlaveReplicate(slaveA).Assert();
                            }
                        }
                        recycler.GetPackage().Clear();
                    }

                    // checking
                    sb.Append("\r\nMasterA Address is " + masterA.LocalAddress + " \r\n");
                    using (var box = masterA.Cube())
                    {
                        foreach (var o in box.Select <Member>("from Member", null))
                        {
                            sb.Append(o.Name + " , ");
                        }
                    }
                    sb.Append("\r\nSlaveA Address is " + slaveA.LocalAddress + " \r\n");
                    using (var box = slaveA.Cube())
                    {
                        foreach (var o in box.Select <Member>("from Member", null))
                        {
                            sb.Append(o.Name + " , ");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                sb.Append(ex.ToString() + "\r\n");
            }
            return(sb);
        }