Пример #1
0
        static void Generate(ref AddressBook ab, ref NetAddressBook nab)
        {
            ab.List = new List<Person>();
            nab.List = new List<NetPerson>();
            //Generating structures
            for (int n = 0; n < 5000; n++)
            {
                Person p = new Person();
                p.Name = "Alice" + n;
                p.Id = 17532;
                p.Email = "Alice" + n + "@silentobit.com";
                p.Phone = new List<Person.PhoneNumber>();
                ab.List.Add(p);

                NetPerson np = new NetPerson();
                np.Name = p.Name;
                np.Id = p.Id;
                np.Email = p.Email;
                np.Phone = new List<NetPerson.NetPhoneNumber>();
                nab.List.Add(np);

                for (int m = 0; m < 1000; m++)
                {
                    Person.PhoneNumber pn = new Person.PhoneNumber();
                    pn.Type = Person.PhoneType.MOBILE;
                    pn.Number = m.ToString();
                    p.Phone.Add(pn);

                    NetPerson.NetPhoneNumber npn = new NetPerson.NetPhoneNumber();
                    npn.Type = Person.PhoneType.MOBILE;
                    npn.Number = pn.Number;
                    np.Phone.Add(npn);
                }
            }
        }
        public static void Run()
        {
            AddressBook ab = new AddressBook();
            NetAddressBook nab = new NetAddressBook();
            Console.Write("Generating data structures...");
            Generate(ref ab, ref nab);
            Console.WriteLine("done");

            Console.WriteLine("Starting speed tests...");

            RunTestSerialize(new AllocationStack(), ab);
            RunTestSerialize(new ThreadSafeStack(), ab);
            RunTestSerialize(new ThreadUnsafeStack(), ab);
            RunTestSerialize(new ConcurrentBagStack(), ab);

            using (MemoryStream ms = new MemoryStream())
            {
                //Serialize
                Console.Write("Speed test ConcurrentBagStack: Serialize " + ab.List.Count + " posts in   ");
                GCWait();
                var start = DateTime.Now;
                AddressBook.Serialize(ms, ab);
                var serialize = DateTime.Now - start;
                Console.WriteLine(serialize.TotalSeconds + " s");

                //Deserialize
                Console.Write("Speed test: Deserialize " + ab.List.Count + " posts in ");
                ms.Seek(0, SeekOrigin.Begin);
                GCWait();
                start = DateTime.Now;
                AddressBook.Deserialize(new PositionStream(ms));
                TimeSpan deserialize = DateTime.Now - start;
                Console.WriteLine(deserialize.TotalSeconds + " s");
            }

            using (MemoryStream ms = new MemoryStream())
            {
                //Serialize 
                Console.Write("Protobuf-net: Serialize " + nab.List.Count + " posts in   ");
                GCWait();
                DateTime start = DateTime.Now;
                ProtoBuf.Serializer.Serialize(ms, nab);
                TimeSpan serialize = DateTime.Now - start;
                Console.WriteLine(serialize.TotalSeconds + " s");

                //Deserialize
                Console.Write("Protobuf-net: Deserialize " + nab.List.Count + " posts in ");
                ms.Seek(0, SeekOrigin.Begin);
                GCWait();
                start = DateTime.Now;
                ProtoBuf.Serializer.Deserialize<NetAddressBook>(ms);
                TimeSpan deserialize = DateTime.Now - start;
                Console.WriteLine(deserialize.TotalSeconds + " s");
            }
        }
Пример #3
0
 /// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary>
 public static byte[] SerializeToBytes(AddressBook instance)
 {
     using (var ms = new MemoryStream())
     {
         Serialize(ms, instance);
         return ms.ToArray();
     }
 }
Пример #4
0
 /// <summary>Helper: Serialize with a varint length prefix</summary>
 public static void SerializeLengthDelimited(Stream stream, AddressBook instance)
 {
     var data = SerializeToBytes(instance);
     global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length);
     stream.Write(data, 0, data.Length);
 }
Пример #5
0
        /// <summary>Serialize the instance into the stream</summary>
        public static void Serialize(Stream stream, AddressBook instance)
        {
            if (instance.List != null)
            {
                foreach (var i1 in instance.List)
                {
                    // Key for field: 1, LengthDelimited
                    stream.WriteByte(10);
                    using (var ms1 = new MemoryStream())
                    {
                        Personal.Person.Serialize(ms1, i1);
                        // Length delimited byte array
                        uint ms1Length = (uint)ms1.Length;
                        global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms1Length);
                        stream.Write(ms1.GetBuffer(), 0, (int)ms1Length);
                    }

                }
            }
        }
Пример #6
0
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static AddressBook DeserializeLengthDelimited(Stream stream)
 {
     AddressBook instance = new AddressBook();
     DeserializeLengthDelimited(stream, instance);
     return instance;
 }
Пример #7
0
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static AddressBook DeserializeLength(Stream stream, int length)
 {
     AddressBook instance = new AddressBook();
     DeserializeLength(stream, length, instance);
     return instance;
 }
Пример #8
0
 /// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary>
 public static AddressBook Deserialize(byte[] buffer)
 {
     AddressBook instance = new AddressBook();
     using (var ms = new MemoryStream(buffer))
         Deserialize(ms, instance);
     return instance;
 }
Пример #9
0
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static AddressBook Deserialize(Stream stream)
 {
     AddressBook instance = new AddressBook();
     Deserialize(stream, instance);
     return instance;
 }
        /// <summary>Serialize the instance into the stream</summary>
        public static void Serialize(Stream stream, AddressBook instance)
        {
            var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop();
            if (instance.List != null)
            {
                foreach (var i1 in instance.List)
                {
                    // Key for field: 1, LengthDelimited
                    stream.WriteByte(10);
                    msField.SetLength(0);
                    Personal.Person.Serialize(msField, i1);
                    // Length delimited byte array
                    uint length1 = (uint)msField.Length;
                    global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length1);
                    msField.WriteTo(stream);

                }
            }
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField);
        }
        static void RunTestSerialize(MemoryStreamStack stack, AddressBook ab)
        {
            ProtocolParser.Stack.Dispose();
            ProtocolParser.Stack = stack;

            using (MemoryStream ms = new MemoryStream())
            {
                //Serialize
                Console.Write("Speed test " + stack.GetType().Name + ": Serialize " + ab.List.Count + " posts in   ");
                GCWait();
                

                var start = DateTime.Now;
                AddressBook.Serialize(ms, ab);
                TimeSpan serialize = DateTime.Now - start;

                Console.WriteLine(serialize.TotalSeconds + " s");
            }
        }
Пример #12
0
        public static void Run()
        {
            Console.WriteLine("Starting speed test...");

            AddressBook ab = new AddressBook();
            NetAddressBook nab = new NetAddressBook();
            ab.List = new List<Person>();
            nab.List = new List<NetPerson>();
            //Generating structures
            for (int n = 0; n < 5000; n++)
            {
                Person p = new Person();
                p.Name = "Alice" + n;
                p.Id = 17532;
                p.Email = "*****@*****.**";
                p.Phone = new List<Person.PhoneNumber>();
                ab.List.Add(p);

                NetPerson np = new NetPerson();
                np.Name = p.Name;
                np.Id = p.Id;
                np.Email = p.Email;
                np.Phone = new List<NetPerson.NetPhoneNumber>();
                nab.List.Add(np);

                for (int m = 0; m < 1000; m++)
                {
                    Person.PhoneNumber pn = new Person.PhoneNumber();
                    pn.Type = Person.PhoneType.MOBILE;
                    pn.Number = m.ToString();
                    p.Phone.Add(pn);

                    NetPerson.NetPhoneNumber npn = new NetPerson.NetPhoneNumber();
                    npn.Type = Person.PhoneType.MOBILE;
                    npn.Number = pn.Number;
                    np.Phone.Add(npn);
                }
            }

            using (MemoryStream ms = new MemoryStream())
            {
                //Serialize
                DateTime start = DateTime.Now;
                AddressBook.Serialize(ms, ab);
                TimeSpan serialize = DateTime.Now - start;
                Console.WriteLine("Speed test: Serialize " + ab.List.Count + " posts in   " + serialize.TotalSeconds + " s");

                //Deserialize
                ms.Seek(0, SeekOrigin.Begin);
                start = DateTime.Now;
                var dab = AddressBook.Deserialize(new StreamRead(ms));
                TimeSpan deserialize = DateTime.Now - start;
                Console.WriteLine("Speed test: Deserialize " + dab.List.Count + " posts in " + deserialize.TotalSeconds + " s");
            }

            using (MemoryStream ms = new MemoryStream())
            {
                //Serialize
                DateTime start = DateTime.Now;
                ProtoBuf.Serializer.Serialize(ms, nab);
                TimeSpan serialize = DateTime.Now - start;
                Console.WriteLine("Protobuf-net: Serialize " + nab.List.Count + " posts in   " + serialize.TotalSeconds + " s");

                //Deserialize
                ms.Seek(0, SeekOrigin.Begin);
                start = DateTime.Now;
                var dab = ProtoBuf.Serializer.Deserialize<NetAddressBook>(ms);
                TimeSpan deserialize = DateTime.Now - start;
                Console.WriteLine("Protobuf-net: Deserialize " + dab.List.Count + " posts in " + deserialize.TotalSeconds + " s");
            }
        }