Beispiel #1
0
    static Proto3Objects.PersonVector SerializeArrayProto3(IList <Person> original)
    {
        Console.WriteLine("Google.Protobuf");

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

        var person = new Proto3Objects.PersonVector();

        person.List.AddRange(original.Select(x => new Proto3Objects.Person
        {
            Age       = x.Age,
            FirstName = x.FirstName,
            LastName  = x.LastName,
            Sex       = (Proto3Objects.Sex)(int) x.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.PersonVector.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);
    }
Beispiel #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");
            }
        }
    }