py_benchmark.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from __future__ import print_function
  2. import sys
  3. import os
  4. import timeit
  5. import math
  6. import argparse
  7. import fnmatch
  8. import json
  9. parser = argparse.ArgumentParser(description="Python protobuf benchmark")
  10. parser.add_argument("data_files", metavar="dataFile", nargs="+",
  11. help="testing data files.")
  12. parser.add_argument("--json", action="store_const", dest="json",
  13. const="yes", default="no",
  14. help="Whether to output json results")
  15. parser.add_argument("--behavior_prefix", dest="behavior_prefix",
  16. help="The output json format's behavior's name's prefix",
  17. default="")
  18. # BEGIN CPP GENERATED MESSAGE
  19. parser.add_argument("--cpp_generated", action="store_const",
  20. dest="cpp_generated", const="yes", default="no",
  21. help="Whether to link generated code library")
  22. # END CPP GENERATED MESSAGE
  23. args = parser.parse_args()
  24. # BEGIN CPP GENERATED MESSAGE
  25. # CPP generated code must be linked before importing the generated Python code
  26. # for the descriptor can be found in the pool
  27. if args.cpp_generated != "no":
  28. sys.path.append( os.path.dirname( os.path.dirname( os.path.abspath(__file__) ) ) + "/.libs" )
  29. import libbenchmark_messages
  30. sys.path.append( os.path.dirname( os.path.dirname( os.path.abspath(__file__) ) ) + "/tmp" )
  31. # END CPP GENERATED MESSAGE
  32. import datasets.google_message1.proto2.benchmark_message1_proto2_pb2 as benchmark_message1_proto2_pb2
  33. import datasets.google_message1.proto3.benchmark_message1_proto3_pb2 as benchmark_message1_proto3_pb2
  34. import datasets.google_message2.benchmark_message2_pb2 as benchmark_message2_pb2
  35. import datasets.google_message3.benchmark_message3_pb2 as benchmark_message3_pb2
  36. import datasets.google_message4.benchmark_message4_pb2 as benchmark_message4_pb2
  37. import benchmarks_pb2 as benchmarks_pb2
  38. def run_one_test(filename):
  39. data = open(filename).read()
  40. benchmark_dataset = benchmarks_pb2.BenchmarkDataset()
  41. benchmark_dataset.ParseFromString(data)
  42. benchmark_util = Benchmark(full_iteration=len(benchmark_dataset.payload),
  43. module="py_benchmark",
  44. setup_method="init")
  45. result={}
  46. result["filename"] = filename
  47. result["message_name"] = benchmark_dataset.message_name
  48. result["benchmarks"] = {}
  49. benchmark_util.set_test_method("parse_from_benchmark")
  50. result["benchmarks"][args.behavior_prefix + "_parse_from_benchmark"] = \
  51. benchmark_util.run_benchmark(setup_method_args='"%s"' % (filename))
  52. benchmark_util.set_test_method("serialize_to_benchmark")
  53. result["benchmarks"][args.behavior_prefix + "_serialize_to_benchmark"] = \
  54. benchmark_util.run_benchmark(setup_method_args='"%s"' % (filename))
  55. return result
  56. def init(filename):
  57. global benchmark_dataset, message_class, message_list, counter
  58. message_list=[]
  59. counter = 0
  60. data = open(os.path.dirname(sys.argv[0]) + "/../" + filename).read()
  61. benchmark_dataset = benchmarks_pb2.BenchmarkDataset()
  62. benchmark_dataset.ParseFromString(data)
  63. if benchmark_dataset.message_name == "benchmarks.proto3.GoogleMessage1":
  64. message_class = benchmark_message1_proto3_pb2.GoogleMessage1
  65. elif benchmark_dataset.message_name == "benchmarks.proto2.GoogleMessage1":
  66. message_class = benchmark_message1_proto2_pb2.GoogleMessage1
  67. elif benchmark_dataset.message_name == "benchmarks.proto2.GoogleMessage2":
  68. message_class = benchmark_message2_pb2.GoogleMessage2
  69. elif benchmark_dataset.message_name == "benchmarks.google_message3.GoogleMessage3":
  70. message_class = benchmark_message3_pb2.GoogleMessage3
  71. elif benchmark_dataset.message_name == "benchmarks.google_message4.GoogleMessage4":
  72. message_class = benchmark_message4_pb2.GoogleMessage4
  73. else:
  74. raise IOError("Message %s not found!" % (benchmark_dataset.message_name))
  75. for one_payload in benchmark_dataset.payload:
  76. temp = message_class()
  77. temp.ParseFromString(one_payload)
  78. message_list.append(temp)
  79. def parse_from_benchmark():
  80. global counter, message_class, benchmark_dataset
  81. m = message_class().ParseFromString(benchmark_dataset.payload[counter % len(benchmark_dataset.payload)])
  82. counter = counter + 1
  83. def serialize_to_benchmark():
  84. global counter, message_list, message_class
  85. s = message_list[counter % len(benchmark_dataset.payload)].SerializeToString()
  86. counter = counter + 1
  87. class Benchmark:
  88. def __init__(self, module=None, test_method=None,
  89. setup_method=None, full_iteration = 1):
  90. self.full_iteration = full_iteration
  91. self.module = module
  92. self.test_method = test_method
  93. self.setup_method = setup_method
  94. def set_test_method(self, test_method):
  95. self.test_method = test_method
  96. def full_setup_code(self, setup_method_args=''):
  97. setup_code = ""
  98. setup_code += "from %s import %s\n" % (self.module, self.test_method)
  99. setup_code += "from %s import %s\n" % (self.module, self.setup_method)
  100. setup_code += "%s(%s)\n" % (self.setup_method, setup_method_args)
  101. return setup_code
  102. def dry_run(self, test_method_args='', setup_method_args=''):
  103. return timeit.timeit(stmt="%s(%s)" % (self.test_method, test_method_args),
  104. setup=self.full_setup_code(setup_method_args),
  105. number=self.full_iteration);
  106. def run_benchmark(self, test_method_args='', setup_method_args=''):
  107. reps = self.full_iteration;
  108. t = self.dry_run(test_method_args, setup_method_args);
  109. if t < 3 :
  110. reps = int(math.ceil(3 / t)) * self.full_iteration
  111. t = timeit.timeit(stmt="%s(%s)" % (self.test_method, test_method_args),
  112. setup=self.full_setup_code(setup_method_args),
  113. number=reps);
  114. return 1.0 * t / reps * (10 ** 9)
  115. if __name__ == "__main__":
  116. results = []
  117. for file in args.data_files:
  118. results.append(run_one_test(file))
  119. if args.json != "no":
  120. print(json.dumps(results))
  121. else:
  122. for result in results:
  123. print("Message %s of dataset file %s" % \
  124. (result["message_name"], result["filename"]))
  125. print("Average time for parse_from_benchmark: %.2f ns" % \
  126. (result["benchmarks"][ \
  127. args.behavior_prefix + "_parse_from_benchmark"]))
  128. print("Average time for serialize_to_benchmark: %.2f ns" % \
  129. (result["benchmarks"][ \
  130. args.behavior_prefix + "_serialize_to_benchmark"]))
  131. print("")