private void Test <TVTable>(
        byte[] vtable,
        int expectedMaxIndex,
        int actualMaxIndex,
        CreateCallback <TVTable> callback)
        where TVTable : struct, IVTable
    {
        var buffer = new ArrayInputBuffer(vtable);

        callback(buffer, 0, out TVTable vt);

        Assert.Equal(expectedMaxIndex, vt.MaxSupportedIndex);

        for (int i = 0; i <= Math.Max(actualMaxIndex, expectedMaxIndex); ++i)
        {
            if (i <= Math.Min(actualMaxIndex, expectedMaxIndex))
            {
                Assert.Equal(i, vt.OffsetOf(buffer, i));
            }
            else
            {
                Assert.Equal(0, vt.OffsetOf(buffer, i));
            }
        }

        Assert.Equal(0, vt.OffsetOf(buffer, vt.MaxSupportedIndex + 1));
        Assert.Equal(0, vt.OffsetOf(buffer, -1));
    }
Exemple #2
0
        private static void RunBenchmarkSet(
            string runName,
            ISerializer <FooBarContainer> serializer,
            FooBarContainer container)
        {
            int traversalCount = 5;

            byte[] destination = new byte[serializer.GetMaxSize(container)];

            SpanWriter  spanWriter  = new SpanWriter();
            InputBuffer inputBuffer = new ArrayInputBuffer(destination);

            serializer.Write(spanWriter, destination, container);

            (string, Action)[] items = new (string, Action)[]
Exemple #3
0
        public virtual void GlobalSetup()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;

            FooBar[]           fooBars   = new FooBar[this.VectorLength];
            FooBarNonVirtual[] fooBarsNV = new FooBarNonVirtual[this.VectorLength];
            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var fooNV = new FooNonVirtual
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var barNV = new BarNonVirtual
                {
                    Parent = fooNV,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                var fooBarNV = new FooBarNonVirtual
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barNV,
                };

                fooBars[i]   = fooBar;
                fooBarsNV[i] = fooBarNV;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            this.defaultContainerNonVirtual = new FooBarListContainerNonVirtual
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsNV,
            };

            Random rng = new Random();

            this.sortedIntContainer = new SortedVectorTable <int> {
                Vector = new List <SortedVectorTableItem <int> >()
            };
            this.sortedStringContainer = new SortedVectorTable <string> {
                Vector = new List <SortedVectorTableItem <string> >()
            };
            this.unsortedIntContainer = new UnsortedVectorTable <int> {
                Vector = new List <UnsortedVectorTableItem <int> >()
            };
            this.unsortedStringContainer = new UnsortedVectorTable <string> {
                Vector = new List <UnsortedVectorTableItem <string> >()
            };

            for (int i = 0; i < this.VectorLength; ++i)
            {
                this.sortedIntContainer.Vector.Add(new SortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.sortedStringContainer.Vector.Add(new SortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
                this.unsortedIntContainer.Vector.Add(new UnsortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.unsortedStringContainer.Vector.Add(new UnsortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
            }

            this.valueTableVector = new ValueTableVector
            {
                ValueTables = Enumerable.Range(0, this.VectorLength).Select(x => new ValueTable
                {
                    A = 1,
                    B = 2,
                    C = 3,
                    D = 4,
                    E = 5,
                    F = 6,
                    G = 7,
                    H = 8,
                    I = 9,
                    J = 10,
                    K = true,
                }).ToArray()
            };

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);

                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new ArrayInputBuffer(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            int googleLength = 0;

            {
                this.google_ByteBuffer       = new FlatBuffers.ByteBuffer(this.fs_readMemory.ToArray());
                this.google_defaultContainer = Google.FooBarContainer.GetRootAsFooBarContainer(this.google_ByteBuffer).UnPack();
                googleLength = this.Google_FlatBuffers_Serialize_ObjectApi();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }

            {
                this.MsgPack_Serialize_NonVirtual();
            }

            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

            Console.WriteLine($"Sizes: MsgPack: {this.msgPackWriteData.Length}");
            Console.WriteLine($"Sizes: FlatSharp: {this.fs_readMemory.Length}");
            Console.WriteLine($"Sizes: Google: {googleLength}");
            Console.WriteLine($"Sizes: Pbdn: {this.pbdn_writeBuffer.Length}");
        }
Exemple #4
0
        public virtual void GlobalSetup()
        {
            FooBar[] fooBars = new FooBar[this.VectorLength];
            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                fooBars[i] = fooBar;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            Random rng = new Random();

            this.sortedIntContainer = new SortedVectorTable <int> {
                Vector = new List <SortedVectorTableItem <int> >()
            };
            this.sortedStringContainer = new SortedVectorTable <string> {
                Vector = new List <SortedVectorTableItem <string> >()
            };
            this.unsortedIntContainer = new UnsortedVectorTable <int> {
                Vector = new List <UnsortedVectorTableItem <int> >()
            };
            this.unsortedStringContainer = new UnsortedVectorTable <string> {
                Vector = new List <UnsortedVectorTableItem <string> >()
            };

            for (int i = 0; i < this.VectorLength; ++i)
            {
                this.sortedIntContainer.Vector.Add(new SortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.sortedStringContainer.Vector.Add(new SortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
                this.unsortedIntContainer.Vector.Add(new UnsortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.unsortedStringContainer.Vector.Add(new UnsortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
            }

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);

                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new ArrayInputBuffer(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            {
                this.google_ByteBuffer       = new FlatBuffers.ByteBuffer(this.fs_readMemory.ToArray());
                this.google_defaultContainer = Google.FooBarContainer.GetRootAsFooBarContainer(this.google_ByteBuffer).UnPack();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }
        }