def BenchmarkVtableDeduplication(count): ''' BenchmarkVtableDeduplication measures the speed of vtable deduplication by creating `prePop` vtables, then populating `count` objects with a different single vtable. When count is large (as in long benchmarks), memory usage may be high. ''' prePop = 10 builder = flatbuffers.Builder(0) # pre-populate some vtables: for i in compat_range(prePop): builder.StartObject(i) for j in compat_range(i): builder.PrependInt16Slot(j, j, 0) builder.EndObject() # benchmark deduplication of a new vtable: def f(): builder.StartObject(prePop) for j in compat_range(prePop): builder.PrependInt16Slot(j, j, 0) builder.EndObject() duration = timeit.timeit(stmt=f, number=count) rate = float(count) / duration print(('vtable deduplication rate: %.2f/sec' % rate))
def BenchmarkVtableDeduplication(count): ''' BenchmarkVtableDeduplication measures the speed of vtable deduplication by creating `prePop` vtables, then populating `count` objects with a different single vtable. When count is large (as in long benchmarks), memory usage may be high. ''' prePop = 10 builder = flatbuffers.Builder(0) # pre-populate some vtables: for i in compat_range(prePop): builder.StartObject(i) for j in compat_range(i): builder.PrependInt16Slot(j, j, 0) builder.EndObject() # benchmark deduplication of a new vtable: def f(): builder.StartObject(prePop) for j in compat_range(prePop): builder.PrependInt16Slot(j, j, 0) builder.EndObject() duration = timeit.timeit(stmt=f, number=count) rate = float(count) / duration print(('vtable deduplication rate: %.2f/sec' % rate))
def CheckReadBuffer(buf, offset): ''' CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster. ''' def asserter(stmt): ''' An assertion helper that is separated from TestCase classes. ''' if not stmt: raise AssertionError('CheckReadBuffer case failed') monster = MyGame.Example.Monster.Monster.GetRootAsMonster(buf, offset) asserter(monster.Hp() == 80) asserter(monster.Mana() == 150) asserter(monster.Name() == b'MyMonster') # initialize a Vec3 from Pos() vec = monster.Pos() asserter(vec is not None) # verify the properties of the Vec3 asserter(vec.X() == 1.0) asserter(vec.Y() == 2.0) asserter(vec.Z() == 3.0) asserter(vec.Test1() == 3.0) asserter(vec.Test2() == 2) # initialize a Test from Test3(...) t = MyGame.Example.Test.Test() t = vec.Test3(t) asserter(t is not None) # verify the properties of the Test asserter(t.A() == 5) asserter(t.B() == 6) # verify that the enum code matches the enum declaration: union_type = MyGame.Example.Any.Any asserter(monster.TestType() == union_type.Monster) # initialize a Table from a union field Test(...) table2 = monster.Test() asserter(type(table2) is flatbuffers.table.Table) # initialize a Monster from the Table from the union monster2 = MyGame.Example.Monster.Monster() monster2.Init(table2.Bytes, table2.Pos) asserter(monster2.Name() == b"Fred") # iterate through the first monster's inventory: asserter(monster.InventoryLength() == 5) invsum = 0 for i in compat_range(monster.InventoryLength()): v = monster.Inventory(i) invsum += int(v) asserter(invsum == 10) asserter(monster.Test4Length() == 2) # create a 'Test' object and populate it: test0 = monster.Test4(0) asserter(type(test0) is MyGame.Example.Test.Test) test1 = monster.Test4(1) asserter(type(test1) is MyGame.Example.Test.Test) # the position of test0 and test1 are swapped in monsterdata_java_wire # and monsterdata_test_wire, so ignore ordering v0 = test0.A() v1 = test0.B() v2 = test1.A() v3 = test1.B() sumtest12 = int(v0) + int(v1) + int(v2) + int(v3) asserter(sumtest12 == 100) asserter(monster.TestarrayofstringLength() == 2) asserter(monster.Testarrayofstring(0) == b"test1") asserter(monster.Testarrayofstring(1) == b"test2") asserter(monster.Enemy() is None) asserter(monster.TestarrayoftablesLength() == 0) asserter(monster.TestnestedflatbufferLength() == 0) asserter(monster.Testempty() is None)
def check_once(self, fuzzFields, fuzzObjects): testValuesMax = 11 # hardcoded to the number of scalar types builder = flatbuffers.Builder(0) l = LCG() objects = [0 for _ in compat_range(fuzzObjects)] # Generate fuzzObjects random objects each consisting of # fuzzFields fields, each of a random type. for i in compat_range(fuzzObjects): builder.StartObject(fuzzFields) for j in compat_range(fuzzFields): choice = int(l.Next()) % testValuesMax if choice == 0: builder.PrependBoolSlot(int(j), self.boolVal, False) elif choice == 1: builder.PrependInt8Slot(int(j), self.int8Val, 0) elif choice == 2: builder.PrependUint8Slot(int(j), self.uint8Val, 0) elif choice == 3: builder.PrependInt16Slot(int(j), self.int16Val, 0) elif choice == 4: builder.PrependUint16Slot(int(j), self.uint16Val, 0) elif choice == 5: builder.PrependInt32Slot(int(j), self.int32Val, 0) elif choice == 6: builder.PrependUint32Slot(int(j), self.uint32Val, 0) elif choice == 7: builder.PrependInt64Slot(int(j), self.int64Val, 0) elif choice == 8: builder.PrependUint64Slot(int(j), self.uint64Val, 0) elif choice == 9: builder.PrependFloat32Slot(int(j), self.float32Val, 0) elif choice == 10: builder.PrependFloat64Slot(int(j), self.float64Val, 0) else: raise RuntimeError('unreachable') off = builder.EndObject() # store the offset from the end of the builder buffer, # since it will keep growing: objects[i] = off # Do some bookkeeping to generate stats on fuzzes: stats = defaultdict(int) def check(table, desc, want, got): stats[desc] += 1 self.assertEqual(want, got, "%s != %s, %s" % (want, got, desc)) l = LCG() # Reset. # Test that all objects we generated are readable and return the # expected values. We generate random objects in the same order # so this is deterministic. for i in compat_range(fuzzObjects): table = flatbuffers.table.Table(builder.Bytes, len(builder.Bytes) - objects[i]) for j in compat_range(fuzzFields): field_count = flatbuffers.builder.VtableMetadataFields + j f = N.VOffsetTFlags.py_type(field_count * N.VOffsetTFlags.bytewidth) choice = int(l.Next()) % testValuesMax if choice == 0: check(table, "bool", self.boolVal, table.GetSlot(f, False, N.BoolFlags)) elif choice == 1: check(table, "int8", self.int8Val, table.GetSlot(f, 0, N.Int8Flags)) elif choice == 2: check(table, "uint8", self.uint8Val, table.GetSlot(f, 0, N.Uint8Flags)) elif choice == 3: check(table, "int16", self.int16Val, table.GetSlot(f, 0, N.Int16Flags)) elif choice == 4: check(table, "uint16", self.uint16Val, table.GetSlot(f, 0, N.Uint16Flags)) elif choice == 5: check(table, "int32", self.int32Val, table.GetSlot(f, 0, N.Int32Flags)) elif choice == 6: check(table, "uint32", self.uint32Val, table.GetSlot(f, 0, N.Uint32Flags)) elif choice == 7: check(table, "int64", self.int64Val, table.GetSlot(f, 0, N.Int64Flags)) elif choice == 8: check(table, "uint64", self.uint64Val, table.GetSlot(f, 0, N.Uint64Flags)) elif choice == 9: check(table, "float32", self.float32Val, table.GetSlot(f, 0, N.Float32Flags)) elif choice == 10: check(table, "float64", self.float64Val, table.GetSlot(f, 0, N.Float64Flags)) else: raise RuntimeError('unreachable') # If enough checks were made, verify that all scalar types were used: self.assertEqual(testValuesMax, len(stats), "fuzzing failed to test all scalar types: %s" % stats)
def f(): builder.StartObject(prePop) for j in compat_range(prePop): builder.PrependInt16Slot(j, j, 0) builder.EndObject()
def CheckReadBuffer(buf, offset): ''' CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster. ''' def asserter(stmt): ''' An assertion helper that is separated from TestCase classes. ''' if not stmt: raise AssertionError('CheckReadBuffer case failed') monster = MyGame.Example.Monster.Monster.GetRootAsMonster(buf, offset) asserter(monster.Hp() == 80) asserter(monster.Mana() == 150) asserter(monster.Name() == b'MyMonster') # initialize a Vec3 from Pos() vec = monster.Pos() asserter(vec is not None) # verify the properties of the Vec3 asserter(vec.X() == 1.0) asserter(vec.Y() == 2.0) asserter(vec.Z() == 3.0) asserter(vec.Test1() == 3.0) asserter(vec.Test2() == 2) # initialize a Test from Test3(...) t = MyGame.Example.Test.Test() t = vec.Test3(t) asserter(t is not None) # verify the properties of the Test asserter(t.A() == 5) asserter(t.B() == 6) # verify that the enum code matches the enum declaration: union_type = MyGame.Example.Any.Any asserter(monster.TestType() == union_type.Monster) # initialize a Table from a union field Test(...) table2 = monster.Test() asserter(type(table2) is flatbuffers.table.Table) # initialize a Monster from the Table from the union monster2 = MyGame.Example.Monster.Monster() monster2.Init(table2.Bytes, table2.Pos) asserter(monster2.Name() == b"Fred") # iterate through the first monster's inventory: asserter(monster.InventoryLength() == 5) invsum = 0 for i in compat_range(monster.InventoryLength()): v = monster.Inventory(i) invsum += int(v) asserter(invsum == 10) asserter(monster.Test4Length() == 2) # create a 'Test' object and populate it: test0 = monster.Test4(0) asserter(type(test0) is MyGame.Example.Test.Test) test1 = monster.Test4(1) asserter(type(test1) is MyGame.Example.Test.Test) # the position of test0 and test1 are swapped in monsterdata_java_wire # and monsterdata_test_wire, so ignore ordering v0 = test0.A() v1 = test0.B() v2 = test1.A() v3 = test1.B() sumtest12 = int(v0) + int(v1) + int(v2) + int(v3) asserter(sumtest12 == 100) asserter(monster.TestarrayofstringLength() == 2) asserter(monster.Testarrayofstring(0) == b"test1") asserter(monster.Testarrayofstring(1) == b"test2") asserter(monster.Enemy() is None) asserter(monster.TestarrayoftablesLength() == 0) asserter(monster.TestnestedflatbufferLength() == 0) asserter(monster.Testempty() is None)
def check_once(self, fuzzFields, fuzzObjects): testValuesMax = 11 # hardcoded to the number of scalar types builder = flatbuffers.Builder(0) l = LCG() objects = [0 for _ in compat_range(fuzzObjects)] # Generate fuzzObjects random objects each consisting of # fuzzFields fields, each of a random type. for i in compat_range(fuzzObjects): builder.StartObject(fuzzFields) for j in compat_range(fuzzFields): choice = int(l.Next()) % testValuesMax if choice == 0: builder.PrependBoolSlot(int(j), self.boolVal, False) elif choice == 1: builder.PrependInt8Slot(int(j), self.int8Val, 0) elif choice == 2: builder.PrependUint8Slot(int(j), self.uint8Val, 0) elif choice == 3: builder.PrependInt16Slot(int(j), self.int16Val, 0) elif choice == 4: builder.PrependUint16Slot(int(j), self.uint16Val, 0) elif choice == 5: builder.PrependInt32Slot(int(j), self.int32Val, 0) elif choice == 6: builder.PrependUint32Slot(int(j), self.uint32Val, 0) elif choice == 7: builder.PrependInt64Slot(int(j), self.int64Val, 0) elif choice == 8: builder.PrependUint64Slot(int(j), self.uint64Val, 0) elif choice == 9: builder.PrependFloat32Slot(int(j), self.float32Val, 0) elif choice == 10: builder.PrependFloat64Slot(int(j), self.float64Val, 0) else: raise RuntimeError('unreachable') off = builder.EndObject() # store the offset from the end of the builder buffer, # since it will keep growing: objects[i] = off # Do some bookkeeping to generate stats on fuzzes: stats = defaultdict(int) def check(table, desc, want, got): stats[desc] += 1 self.assertEqual(want, got, "%s != %s, %s" % (want, got, desc)) l = LCG() # Reset. # Test that all objects we generated are readable and return the # expected values. We generate random objects in the same order # so this is deterministic. for i in compat_range(fuzzObjects): table = flatbuffers.table.Table(builder.Bytes, len(builder.Bytes) - objects[i]) for j in compat_range(fuzzFields): field_count = flatbuffers.builder.VtableMetadataFields + j f = N.VOffsetTFlags.py_type(field_count * N.VOffsetTFlags.bytewidth) choice = int(l.Next()) % testValuesMax if choice == 0: check(table, "bool", self.boolVal, table.GetSlot(f, False, N.BoolFlags)) elif choice == 1: check(table, "int8", self.int8Val, table.GetSlot(f, 0, N.Int8Flags)) elif choice == 2: check(table, "uint8", self.uint8Val, table.GetSlot(f, 0, N.Uint8Flags)) elif choice == 3: check(table, "int16", self.int16Val, table.GetSlot(f, 0, N.Int16Flags)) elif choice == 4: check(table, "uint16", self.uint16Val, table.GetSlot(f, 0, N.Uint16Flags)) elif choice == 5: check(table, "int32", self.int32Val, table.GetSlot(f, 0, N.Int32Flags)) elif choice == 6: check(table, "uint32", self.uint32Val, table.GetSlot(f, 0, N.Uint32Flags)) elif choice == 7: check(table, "int64", self.int64Val, table.GetSlot(f, 0, N.Int64Flags)) elif choice == 8: check(table, "uint64", self.uint64Val, table.GetSlot(f, 0, N.Uint64Flags)) elif choice == 9: check(table, "float32", self.float32Val, table.GetSlot(f, 0, N.Float32Flags)) elif choice == 10: check(table, "float64", self.float64Val, table.GetSlot(f, 0, N.Float64Flags)) else: raise RuntimeError('unreachable') # If enough checks were made, verify that all scalar types were used: self.assertEqual(testValuesMax, len(stats), "fuzzing failed to test all scalar types: %s" % stats)
def f(): builder.StartObject(prePop) for j in compat_range(prePop): builder.PrependInt16Slot(j, j, 0) builder.EndObject()