示例#1
0
    static Proto3Objects.Person SerializeSingleProto3(Person original)
    {
        Console.WriteLine("Google.Protobuf");

        Proto3Objects.Person copy   = default(Proto3Objects.Person);
        MemoryStream         stream = null;

        var person = new Proto3Objects.Person
        {
            Age       = original.Age,
            FirstName = original.FirstName,
            LastName  = original.LastName,
            Sex       = (Proto3Objects.Sex)(int) original.Sex
        };

        using (new Measure("Serialize"))
        {
            for (int i = 0; i < Iteration; i++)
            {
                using (var writeStream = new Google.Protobuf.CodedOutputStream(stream = new MemoryStream(), true))
                {
                    person.WriteTo(writeStream);
                }
            }
        }

        byte[] inputBytes = stream.ToArray();

        using (new Measure("Deserialize"))
        {
            for (int i = 0; i < Iteration; i++)
            {
                copy = Proto3Objects.Person.Parser.ParseFrom(inputBytes);
            }
        }

        using (new Measure("ReSerialize"))
        {
            for (int i = 0; i < Iteration; i++)
            {
                using (var writeStream = new Google.Protobuf.CodedOutputStream(stream = new MemoryStream(), true))
                {
                    copy.WriteTo(writeStream);
                }
            }
        }

        if (!dryRun)
        {
            Console.WriteLine(string.Format("{0,15}   {1}", "Binary Size", ToHumanReadableSize(stream.Position)));
        }

        return(copy);
    }
示例#2
0
    static void ValidateProto3(Person original, IList <Person> list, Proto3Objects.Person p, Proto3Objects.PersonVector l)
    {
        if (!(p.Age == original.Age && p.FirstName == original.FirstName && p.LastName == original.LastName && (sbyte)p.Sex == (sbyte)original.Sex))
        {
            throw new Exception("Validation failed");
        }

        for (int i = 0; i < list.Count; i++)
        {
            if (!(l.List[i].Age == list[i].Age && l.List[i].FirstName == list[i].FirstName && l.List[i].LastName == list[i].LastName && (sbyte)l.List[i].Sex == (sbyte)list[i].Sex))
            {
                throw new Exception("Validation failed");
            }
        }
    }