• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 using BenchmarkDotNet.Attributes;
18 using MyGame.Example;
19 
20 namespace FlatBuffers.Benchmarks
21 {
22     //[EtwProfiler] - needs elevated privileges
23     [MemoryDiagnoser]
24     public class FlatBufferBuilderBenchmark
25     {
26         private const int NumberOfRows = 10_000;
27 
28         [Benchmark]
BuildNestedMonster()29         public void BuildNestedMonster()
30         {
31             const string nestedMonsterName = "NestedMonsterName";
32             const short nestedMonsterHp = 600;
33             const short nestedMonsterMana = 1024;
34 
35             for (int i = 0; i < NumberOfRows; i++)
36             {
37                 // Create nested buffer as a Monster type
38                 var fbb1 = new FlatBufferBuilder(16);
39                 var str1 = fbb1.CreateString(nestedMonsterName);
40                 Monster.StartMonster(fbb1);
41                 Monster.AddName(fbb1, str1);
42                 Monster.AddHp(fbb1, nestedMonsterHp);
43                 Monster.AddMana(fbb1, nestedMonsterMana);
44                 var monster1 = Monster.EndMonster(fbb1);
45                 Monster.FinishMonsterBuffer(fbb1, monster1);
46                 var fbb1Bytes = fbb1.SizedByteArray();
47                 fbb1 = null;
48 
49                 // Create a Monster which has the first buffer as a nested buffer
50                 var fbb2 = new FlatBufferBuilder(16);
51                 var str2 = fbb2.CreateString("My Monster");
52                 var nestedBuffer = Monster.CreateTestnestedflatbufferVector(fbb2, fbb1Bytes);
53                 Monster.StartMonster(fbb2);
54                 Monster.AddName(fbb2, str2);
55                 Monster.AddHp(fbb2, 50);
56                 Monster.AddMana(fbb2, 32);
57                 Monster.AddTestnestedflatbuffer(fbb2, nestedBuffer);
58                 var monster = Monster.EndMonster(fbb2);
59                 Monster.FinishMonsterBuffer(fbb2, monster);
60             }
61         }
62 
63         [Benchmark]
BuildMonster()64         public void BuildMonster()
65         {
66             for (int i = 0; i < NumberOfRows; i++)
67             {
68                 var builder = new FlatBufferBuilder(16);
69                 var str1 = builder.CreateString("MonsterName");
70                 Monster.StartMonster(builder);
71                 Monster.AddName(builder, str1);
72                 Monster.AddHp(builder, 600);
73                 Monster.AddMana(builder, 1024);
74                 Monster.AddColor(builder, Color.Blue);
75                 Monster.AddTestbool(builder, true);
76                 Monster.AddTestf(builder, 0.3f);
77                 Monster.AddTestf2(builder, 0.2f);
78                 Monster.AddTestf3(builder, 0.1f);
79 
80                 var monster1 = Monster.EndMonster(builder);
81                 Monster.FinishMonsterBuffer(builder, monster1);
82             }
83         }
84 
85         [Benchmark]
TestTables()86         public void TestTables()
87         {
88             FlatBufferBuilder builder = new FlatBufferBuilder(1024 * 1024 * 32);
89             for (int x = 0; x < 500000; ++x)
90             {
91                 var offset = builder.CreateString("T");
92                 builder.StartObject(4);
93                 builder.AddDouble(3.2);
94                 builder.AddDouble(4.2);
95                 builder.AddDouble(5.2);
96                 builder.AddOffset(offset.Value);
97                 builder.EndObject();
98             }
99         }
100     }
101 }
102