Ejemplo n.º 1
0
        public async Task OnRecieve(IChannelHandlerContext context, string msgstr)
        {
            //
            if (!string.IsNullOrEmpty(msgstr))
            {
                //RaftCommand  msgObj = Newtonsoft.Json.JsonConvert.DeserializeObject<RaftCommand>(msgstr, new JsonSerializerSettings()
                //{
                //    NullValueHandling = NullValueHandling.Ignore,
                //    TypeNameHandling = TypeNameHandling.All,
                //    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full
                //}
                //);
                int         index  = msgstr.IndexOf(',');
                string      num    = msgstr.Substring(0, index);
                string      base64 = msgstr.Substring(index + 1);
                byte[]      data   = Convert.FromBase64String(base64);
                RaftCommand cmd    = new RaftCommand();
                cmd.Code = Convert.ToInt32(num);
                switch (cmd.Code)
                {
                case RaftCommand.Handshake:
                    cmd.Message = TcpMsgHandshake.BiserDecode(data);
                    break;

                case RaftCommand.HandshakeACK:
                    cmd.Message = TcpMsgHandshake.BiserDecode(data);
                    break;

                case RaftCommand.RaftMessage:
                    cmd.Message = TcpMsgRaft.BiserDecode(data);
                    TcpMsgRaft t = (TcpMsgRaft)cmd.Message;
                    switch (t.RaftSignalType)
                    {
                    case eRaftSignalType.LeaderHearthbeat:
                        t.orginalObject = LeaderHeartbeat.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.CandidateRequest:
                        t.orginalObject = CandidateRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntryAccepted:
                        t.orginalObject = StateLogEntryApplied.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntryRequest:
                        t.orginalObject = StateLogEntryRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntrySuggestion:
                        t.orginalObject = StateLogEntrySuggestion.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogRedirectRequest:
                        t.orginalObject = StateLogEntryRedirectRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.VoteOfCandidate:
                        t.orginalObject = VoteOfCandidate.BiserDecode(t.Data);
                        break;
                    }
                    break;

                case RaftCommand.FreeMessage:
                    cmd.Message = TcpMsg.BiserDecode(data);
                    break;
                }
                this.packetParser(cmd as RaftCommand);
            }
        }
Ejemplo n.º 2
0
        static void t2()
        {
            /*
             * Start
             *  Protobuf obj length: 28
             *  Biser Binary obj length: 20
             *  NetJson obj length: 182
             *  Biser Json obj length: 182
             *
             *  Protobuf encode: 1367 ms
             *  Protobuf decode: 1909 ms
             *  Biser Binary encode: 464 ms
             *  Biser Binary decode: 271 ms
             *  NetJson encode: 1687 ms
             *  NetJson decode: 2383 ms
             *  Biser Json encode: 2871 ms
             *  Biser Json decode: 4748 ms
             * Press any key
             */

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            // It's an operational class from https://github.com/hhblaze/Raft.Net/blob/master/Raft/StateMachine/StateLogEntry.cs
            StateLogEntry sle = new StateLogEntry()
            {
                Data                 = new byte[] { 1, 2, 3, 4, 5 },
                Index                = 458,
                IsCommitted          = true,
                PreviousStateLogId   = 4789,
                PreviousStateLogTerm = 447,
                RedirectId           = 12,
                Term                 = 99
            };

            // It's an operational class from https://github.com/hhblaze/Raft.Net/blob/master/Raft/Objects/StateLogEntrySuggestion.cs
            StateLogEntrySuggestion obj = new StateLogEntrySuggestion()
            {
                IsCommitted   = true,
                LeaderTerm    = 77,
                StateLogEntry = sle
            };

            Console.WriteLine("t2----------------------------------");

            //Protobuf. Warming up, getting length
            var pBt = obj.SerializeProtobuf();

            Console.WriteLine($"Protobuf obj length: {pBt.Length}");
            var pObj = pBt.DeserializeProtobuf <StateLogEntrySuggestion>();

            //Biser. Getting length
            var bBt = new Biser.Encoder().Add(obj).Encode();

            Console.WriteLine($"Biser Binary obj length: {bBt.Length}");
            var bObj = StateLogEntry.BiserDecode(bBt);

            //NetJson. Getting length
            var njss = NetJSON.NetJSON.Serialize(obj);

            Console.WriteLine($"NetJson obj length: {System.Text.Encoding.UTF8.GetBytes(njss).Length}");
            var bnjss = NetJSON.NetJSON.Deserialize <StateLogEntrySuggestion>(njss);

            //Biser Json. Getting length
            var bjss = new Biser.JsonEncoder(obj).GetJSON();

            Console.WriteLine($"Biser Json obj length: {System.Text.Encoding.UTF8.GetBytes(bjss).Length}");
            var bbjss = StateLogEntrySuggestion.BiserJsonDecode(bjss);

            //Message Pack
            var mBt = MessagePackSerializer.Serialize(obj);

            Console.WriteLine($"Message Pack obj length: {mBt.Length}");
            var mc2 = MessagePackSerializer.Deserialize <StateLogEntrySuggestion>(mBt);

            Console.WriteLine("");

            byte[] tbt = null;
            StateLogEntrySuggestion tobj = null;

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                tbt = obj.SerializeProtobuf();
            }
            sw.Stop();
            Console.WriteLine($"Protobuf encode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                tobj = pBt.DeserializeProtobuf <StateLogEntrySuggestion>();
            }
            sw.Stop();
            Console.WriteLine($"Protobuf decode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                tbt = new Biser.Encoder().Add(obj).Encode();
            }
            sw.Stop();
            Console.WriteLine($"Biser Binary encode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                tobj = StateLogEntrySuggestion.BiserDecode(bBt);
            }
            sw.Stop();
            Console.WriteLine($"Biser Binary decode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();


            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                njss = NetJSON.NetJSON.Serialize(obj);
            }
            sw.Stop();
            Console.WriteLine($"NetJson encode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                bnjss = NetJSON.NetJSON.Deserialize <StateLogEntrySuggestion>(njss);
            }
            sw.Stop();
            Console.WriteLine($"NetJson decode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();


            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                bjss = new Biser.JsonEncoder(obj).GetJSON();
            }
            sw.Stop();
            Console.WriteLine($"Biser Json encode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                bbjss = StateLogEntrySuggestion.BiserJsonDecode(bjss);
            }
            sw.Stop();
            Console.WriteLine($"Biser Json decode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                mBt = MessagePackSerializer.Serialize(obj);
            }
            sw.Stop();
            Console.WriteLine($"MessagePack encode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                mc2 = MessagePackSerializer.Deserialize <StateLogEntrySuggestion>(mBt);
            }
            sw.Stop();
            Console.WriteLine($"MessagePack decode: {sw.ElapsedMilliseconds} ms");
            sw.Reset();
        }