Skip to content

Commit 0bcde88

Browse files
committed
Thrift serialization+deserialization benchmark
1 parent dbd5d10 commit 0bcde88

File tree

9 files changed

+369
-1
lines changed

9 files changed

+369
-1
lines changed

benchmarks/thrift_bench.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Adapted from https://raw.githubusercontent.com/Thriftpy/thriftpy2/master/benchmark/benchmark_apache_thrift_struct.py
2+
3+
import json
4+
import time
5+
6+
from thrift.TSerialization import serialize, deserialize
7+
from thrift.protocol.TBinaryProtocol import (
8+
TBinaryProtocolFactory,
9+
TBinaryProtocolAcceleratedFactory
10+
)
11+
12+
import os
13+
import sys
14+
sys.path.append(os.path.join(os.path.dirname(__file__), "../data/thrift"))
15+
from addressbook import ttypes
16+
17+
18+
def make_addressbook():
19+
phone1 = ttypes.PhoneNumber()
20+
phone1.type = ttypes.PhoneType.MOBILE
21+
phone1.number = '555-1212'
22+
phone2 = ttypes.PhoneNumber()
23+
phone2.type = ttypes.PhoneType.HOME
24+
phone2.number = '555-1234'
25+
person = ttypes.Person()
26+
person.name = "Alice"
27+
person.phones = [phone1, phone2]
28+
person.created_at = 1400000000
29+
30+
ab = ttypes.AddressBook()
31+
ab.people = {person.name: person}
32+
return ab
33+
34+
35+
def main():
36+
# proto_factory = TBinaryProtocolFactory()
37+
proto_factory = TBinaryProtocolAcceleratedFactory()
38+
39+
n = 1000
40+
if len(sys.argv) > 1:
41+
n = int(sys.argv[1])
42+
43+
times = []
44+
45+
for i in range(n):
46+
times.append(time.time())
47+
for j in range(100):
48+
ab = make_addressbook()
49+
encoded = serialize(ab, proto_factory)
50+
ab2 = ttypes.AddressBook()
51+
deserialize(ab2, encoded, proto_factory)
52+
times.append(time.time())
53+
54+
if len(sys.argv) > 2:
55+
json.dump(times, open(sys.argv[2], 'w'))
56+
57+
if __name__ == "__main__":
58+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
six==1.15.0
2+
thrift==0.13.0

data/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# From https://github.com/Thriftpy/thriftpy2/blob/master/benchmark/benchmark.rst
2+
thrift/__init__.py: addressbook.thrift
3+
mkdir -p thrift
4+
thrift --gen py -out thrift/ addressbook.thrift

data/addressbook.thrift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# from https://raw.githubusercontent.com/Thriftpy/thriftpy2/master/benchmark/addressbook.thrift
2+
3+
enum PhoneType {
4+
MOBILE,
5+
HOME,
6+
WORK,
7+
}
8+
9+
struct PhoneNumber {
10+
1: optional PhoneType type,
11+
2: optional string number,
12+
}
13+
14+
struct Person {
15+
1: optional string name,
16+
2: optional list<PhoneNumber> phones,
17+
}
18+
19+
typedef map<string, Person> PersonMap
20+
21+
struct AddressBook {
22+
1: optional PersonMap people,
23+
}

data/thrift/__init__.py

Whitespace-only changes.

data/thrift/addressbook/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__all__ = ['ttypes', 'constants']

data/thrift/addressbook/constants.py

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/thrift/addressbook/ttypes.py

Lines changed: 266 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

run_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set -x
1313
mkdir -p results
1414

1515
ENV=/tmp/macrobenchmark_env
16-
for bench in flaskblogging djangocms mypy_bench pylint_bench pycparser_bench pytorch_alexnet_inference; do
16+
for bench in flaskblogging djangocms mypy_bench pylint_bench pycparser_bench pytorch_alexnet_inference gunicorn aiohttp thrift_bench; do
1717
rm -rf $ENV
1818
virtualenv -p $BINARY $ENV
1919
$ENV/bin/pip install -r benchmarks/${bench}_requirements.txt

0 commit comments

Comments
 (0)