mirror of
https://github.com/capnproto/pycapnp.git
synced 2025-03-04 00:14:45 +01:00
adding benchmarks
This commit is contained in:
parent
59beea5108
commit
c44b5b3440
5 changed files with 336 additions and 0 deletions
24
benchmark/addressbook.capnp
Normal file
24
benchmark/addressbook.capnp
Normal file
|
@ -0,0 +1,24 @@
|
|||
@0x934efea7f017fff0;
|
||||
|
||||
struct Person {
|
||||
id @0 :UInt32;
|
||||
name @1 :Text;
|
||||
email @2 :Text;
|
||||
phones @3 :List(PhoneNumber);
|
||||
|
||||
struct PhoneNumber {
|
||||
number @0 :Text;
|
||||
type @1 :Type;
|
||||
|
||||
enum Type {
|
||||
mobile @0;
|
||||
home @1;
|
||||
work @2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AddressBook {
|
||||
people @0 :List(Person);
|
||||
}
|
||||
|
54
benchmark/addressbook.capnp.py
Normal file
54
benchmark/addressbook.capnp.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
from __future__ import print_function
|
||||
import os
|
||||
import capnp
|
||||
|
||||
this_dir = os.path.dirname(__file__)
|
||||
addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
|
||||
|
||||
print = lambda *x: x
|
||||
def writeAddressBook(fd):
|
||||
message = capnp.MallocMessageBuilder()
|
||||
addressBook = message.initRoot(addressbook.AddressBook)
|
||||
people = addressBook.init('people', 2)
|
||||
|
||||
alice = people[0]
|
||||
alice.id = 123
|
||||
alice.name = 'Alice'
|
||||
alice.email = 'alice@example.com'
|
||||
alicePhones = alice.init('phones', 1)
|
||||
alicePhones[0].number = "555-1212"
|
||||
alicePhones[0].type = 'mobile'
|
||||
|
||||
bob = people[1]
|
||||
bob.id = 456
|
||||
bob.name = 'Bob'
|
||||
bob.email = 'bob@example.com'
|
||||
bobPhones = bob.init('phones', 2)
|
||||
bobPhones[0].number = "555-4567"
|
||||
bobPhones[0].type = 'home'
|
||||
bobPhones[1].number = "555-7654"
|
||||
bobPhones[1].type = addressbook.Person.PhoneNumber.Type.WORK
|
||||
|
||||
capnp.writePackedMessageToFd(fd, message)
|
||||
|
||||
|
||||
def printAddressBook(fd):
|
||||
message = capnp.PackedFdMessageReader(f.fileno())
|
||||
addressBook = message.getRoot(addressbook.AddressBook)
|
||||
|
||||
for person in addressBook.people:
|
||||
print(person.name, ':', person.email)
|
||||
for phone in person.phones:
|
||||
print(phone.type, ':', phone.number)
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(10000):
|
||||
f = open('example', 'w')
|
||||
writeAddressBook(f.fileno())
|
||||
|
||||
f = open('example', 'r')
|
||||
printAddressBook(f.fileno())
|
||||
|
||||
os.remove('example')
|
23
benchmark/addressbook.proto
Normal file
23
benchmark/addressbook.proto
Normal file
|
@ -0,0 +1,23 @@
|
|||
package tutorial;
|
||||
|
||||
message Person {
|
||||
required string name = 1;
|
||||
required int32 id = 2;
|
||||
|
||||
enum PhoneType {
|
||||
MOBILE = 0;
|
||||
HOME = 1;
|
||||
WORK = 2;
|
||||
}
|
||||
|
||||
message PhoneNumber {
|
||||
required string number = 1;
|
||||
optional PhoneType type = 2 [default = HOME];
|
||||
}
|
||||
|
||||
repeated PhoneNumber phone = 3;
|
||||
}
|
||||
|
||||
message AddressBook {
|
||||
repeated Person person = 1;
|
||||
}
|
51
benchmark/addressbook.proto.py
Normal file
51
benchmark/addressbook.proto.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
from __future__ import print_function
|
||||
import addressbook_pb2 as addressbook
|
||||
import os
|
||||
|
||||
print = lambda *x: x
|
||||
|
||||
def writeAddressBook(fd):
|
||||
addressBook = addressbook.AddressBook()
|
||||
|
||||
alice = addressBook.person.add()
|
||||
alice.id = 123
|
||||
alice.name = 'Alice'
|
||||
alice.email = 'alice@example.com'
|
||||
alicePhones = [alice.phone.add()]
|
||||
alicePhones[0].number = "555-1212"
|
||||
alicePhones[0].type = addressbook.Person.MOBILE
|
||||
|
||||
bob = addressBook.person.add()
|
||||
bob.id = 456
|
||||
bob.name = 'Bob'
|
||||
bob.email = 'bob@example.com'
|
||||
bobPhones = [bob.phone.add(), bob.phone.add()]
|
||||
bobPhones[0].number = "555-4567"
|
||||
bobPhones[0].type = addressbook.Person.HOME
|
||||
bobPhones[1].number = "555-7654"
|
||||
bobPhones[1].type = addressbook.Person.WORK
|
||||
|
||||
message_string = addressBook.SerializeToString()
|
||||
fd.write(message_string)
|
||||
|
||||
|
||||
def printAddressBook(fd):
|
||||
addressBook = addressbook.AddressBook()
|
||||
addressBook.ParseFromString(fd.read())
|
||||
|
||||
for person in addressBook.person:
|
||||
print(person.name, ':', person.email)
|
||||
for phone in person.phone:
|
||||
print(phone.type, ':', phone.number)
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(10000):
|
||||
f = open('example', 'w')
|
||||
writeAddressBook(f)
|
||||
|
||||
f = open('example', 'r')
|
||||
printAddressBook(f)
|
||||
|
||||
os.remove('example')
|
184
benchmark/addressbook_pb2.py
Normal file
184
benchmark/addressbook_pb2.py
Normal file
|
@ -0,0 +1,184 @@
|
|||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: addressbook.proto
|
||||
|
||||
from google.protobuf import descriptor as _descriptor
|
||||
from google.protobuf import message as _message
|
||||
from google.protobuf import reflection as _reflection
|
||||
from google.protobuf import descriptor_pb2
|
||||
# @@protoc_insertion_point(imports)
|
||||
|
||||
|
||||
|
||||
|
||||
DESCRIPTOR = _descriptor.FileDescriptor(
|
||||
name='addressbook.proto',
|
||||
package='tutorial',
|
||||
serialized_pb='\n\x11\x61\x64\x64ressbook.proto\x12\x08tutorial\"\xda\x01\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x01(\t\x12+\n\x05phone\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM\n\x0bPhoneNumber\x12\x0e\n\x06number\x18\x01 \x02(\t\x12.\n\x04type\x18\x02 \x01(\x0e\x32\x1a.tutorial.Person.PhoneType:\x04HOME\"+\n\tPhoneType\x12\n\n\x06MOBILE\x10\x00\x12\x08\n\x04HOME\x10\x01\x12\x08\n\x04WORK\x10\x02\"/\n\x0b\x41\x64\x64ressBook\x12 \n\x06person\x18\x01 \x03(\x0b\x32\x10.tutorial.Person')
|
||||
|
||||
|
||||
|
||||
_PERSON_PHONETYPE = _descriptor.EnumDescriptor(
|
||||
name='PhoneType',
|
||||
full_name='tutorial.Person.PhoneType',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
values=[
|
||||
_descriptor.EnumValueDescriptor(
|
||||
name='MOBILE', index=0, number=0,
|
||||
options=None,
|
||||
type=None),
|
||||
_descriptor.EnumValueDescriptor(
|
||||
name='HOME', index=1, number=1,
|
||||
options=None,
|
||||
type=None),
|
||||
_descriptor.EnumValueDescriptor(
|
||||
name='WORK', index=2, number=2,
|
||||
options=None,
|
||||
type=None),
|
||||
],
|
||||
containing_type=None,
|
||||
options=None,
|
||||
serialized_start=207,
|
||||
serialized_end=250,
|
||||
)
|
||||
|
||||
|
||||
_PERSON_PHONENUMBER = _descriptor.Descriptor(
|
||||
name='PhoneNumber',
|
||||
full_name='tutorial.Person.PhoneNumber',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='number', full_name='tutorial.Person.PhoneNumber.number', index=0,
|
||||
number=1, type=9, cpp_type=9, label=2,
|
||||
has_default_value=False, default_value=unicode("", "utf-8"),
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='type', full_name='tutorial.Person.PhoneNumber.type', index=1,
|
||||
number=2, type=14, cpp_type=8, label=1,
|
||||
has_default_value=True, default_value=1,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
extension_ranges=[],
|
||||
serialized_start=128,
|
||||
serialized_end=205,
|
||||
)
|
||||
|
||||
_PERSON = _descriptor.Descriptor(
|
||||
name='Person',
|
||||
full_name='tutorial.Person',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='name', full_name='tutorial.Person.name', index=0,
|
||||
number=1, type=9, cpp_type=9, label=2,
|
||||
has_default_value=False, default_value=unicode("", "utf-8"),
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='id', full_name='tutorial.Person.id', index=1,
|
||||
number=2, type=5, cpp_type=1, label=2,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='email', full_name='tutorial.Person.email', index=2,
|
||||
number=3, type=9, cpp_type=9, label=1,
|
||||
has_default_value=False, default_value=unicode("", "utf-8"),
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='phone', full_name='tutorial.Person.phone', index=3,
|
||||
number=4, type=11, cpp_type=10, label=3,
|
||||
has_default_value=False, default_value=[],
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[_PERSON_PHONENUMBER, ],
|
||||
enum_types=[
|
||||
_PERSON_PHONETYPE,
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
extension_ranges=[],
|
||||
serialized_start=32,
|
||||
serialized_end=250,
|
||||
)
|
||||
|
||||
|
||||
_ADDRESSBOOK = _descriptor.Descriptor(
|
||||
name='AddressBook',
|
||||
full_name='tutorial.AddressBook',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='person', full_name='tutorial.AddressBook.person', index=0,
|
||||
number=1, type=11, cpp_type=10, label=3,
|
||||
has_default_value=False, default_value=[],
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
extension_ranges=[],
|
||||
serialized_start=252,
|
||||
serialized_end=299,
|
||||
)
|
||||
|
||||
_PERSON_PHONENUMBER.fields_by_name['type'].enum_type = _PERSON_PHONETYPE
|
||||
_PERSON_PHONENUMBER.containing_type = _PERSON;
|
||||
_PERSON.fields_by_name['phone'].message_type = _PERSON_PHONENUMBER
|
||||
_PERSON_PHONETYPE.containing_type = _PERSON;
|
||||
_ADDRESSBOOK.fields_by_name['person'].message_type = _PERSON
|
||||
DESCRIPTOR.message_types_by_name['Person'] = _PERSON
|
||||
DESCRIPTOR.message_types_by_name['AddressBook'] = _ADDRESSBOOK
|
||||
|
||||
class Person(_message.Message):
|
||||
__metaclass__ = _reflection.GeneratedProtocolMessageType
|
||||
|
||||
class PhoneNumber(_message.Message):
|
||||
__metaclass__ = _reflection.GeneratedProtocolMessageType
|
||||
DESCRIPTOR = _PERSON_PHONENUMBER
|
||||
|
||||
# @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
|
||||
DESCRIPTOR = _PERSON
|
||||
|
||||
# @@protoc_insertion_point(class_scope:tutorial.Person)
|
||||
|
||||
class AddressBook(_message.Message):
|
||||
__metaclass__ = _reflection.GeneratedProtocolMessageType
|
||||
DESCRIPTOR = _ADDRESSBOOK
|
||||
|
||||
# @@protoc_insertion_point(class_scope:tutorial.AddressBook)
|
||||
|
||||
|
||||
# @@protoc_insertion_point(module_scope)
|
Loading…
Add table
Reference in a new issue