result_parser.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # This import depends on the automake rule protoc_middleman, please make sure
  2. # protoc_middleman has been built before run this file.
  3. import json
  4. import re
  5. import os.path
  6. # BEGIN OPENSOURCE
  7. import sys
  8. sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
  9. # END OPENSOURCE
  10. import tmp.benchmarks_pb2 as benchmarks_pb2
  11. __file_size_map = {}
  12. def __get_data_size(filename):
  13. if filename[0] != '/':
  14. filename = os.path.dirname(os.path.abspath(__file__)) + "/../" + filename
  15. if filename in __file_size_map:
  16. return __file_size_map[filename]
  17. benchmark_dataset = benchmarks_pb2.BenchmarkDataset()
  18. benchmark_dataset.ParseFromString(
  19. open(filename).read())
  20. size = 0
  21. count = 0
  22. for payload in benchmark_dataset.payload:
  23. size += len(payload)
  24. count += 1
  25. __file_size_map[filename] = (size, 1.0 * size / count)
  26. return size, 1.0 * size / count
  27. def __extract_file_name(file_name):
  28. name_list = re.split("[/\.]", file_name)
  29. short_file_name = ""
  30. for name in name_list:
  31. if name[:14] == "google_message":
  32. short_file_name = name
  33. return short_file_name
  34. __results = []
  35. # CPP results example:
  36. # [
  37. # "benchmarks": [
  38. # {
  39. # "bytes_per_second": int,
  40. # "cpu_time": int,
  41. # "name: string,
  42. # "time_unit: string,
  43. # ...
  44. # },
  45. # ...
  46. # ],
  47. # ...
  48. # ]
  49. def __parse_cpp_result(filename):
  50. if filename == "":
  51. return
  52. if filename[0] != '/':
  53. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  54. with open(filename) as f:
  55. results = json.loads(f.read())
  56. for benchmark in results["benchmarks"]:
  57. data_filename = "".join(
  58. re.split("(_parse_|_serialize)", benchmark["name"])[0])
  59. behavior = benchmark["name"][len(data_filename) + 1:]
  60. if data_filename[:2] == "BM":
  61. data_filename = data_filename[3:]
  62. __results.append({
  63. "language": "cpp",
  64. "dataFilename": data_filename,
  65. "behavior": behavior,
  66. "throughput": benchmark["bytes_per_second"] / 2.0 ** 20
  67. })
  68. # Python results example:
  69. # [
  70. # [
  71. # {
  72. # "filename": string,
  73. # "benchmarks": {
  74. # behavior: results,
  75. # ...
  76. # },
  77. # "message_name": STRING
  78. # },
  79. # ...
  80. # ], #pure-python
  81. # ...
  82. # ]
  83. def __parse_python_result(filename):
  84. if filename == "":
  85. return
  86. if filename[0] != '/':
  87. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  88. with open(filename) as f:
  89. results_list = json.loads(f.read())
  90. for results in results_list:
  91. for result in results:
  92. _, avg_size = __get_data_size(result["filename"])
  93. for behavior in result["benchmarks"]:
  94. __results.append({
  95. "language": "python",
  96. "dataFilename": __extract_file_name(result["filename"]),
  97. "behavior": behavior,
  98. "throughput": avg_size /
  99. result["benchmarks"][behavior] * 1e9 / 2 ** 20
  100. })
  101. # Java results example:
  102. # [
  103. # {
  104. # "id": string,
  105. # "instrumentSpec": {...},
  106. # "measurements": [
  107. # {
  108. # "weight": float,
  109. # "value": {
  110. # "magnitude": float,
  111. # "unit": string
  112. # },
  113. # ...
  114. # },
  115. # ...
  116. # ],
  117. # "run": {...},
  118. # "scenario": {
  119. # "benchmarkSpec": {
  120. # "methodName": string,
  121. # "parameters": {
  122. # defined parameters in the benchmark: parameters value
  123. # },
  124. # ...
  125. # },
  126. # ...
  127. # }
  128. #
  129. # },
  130. # ...
  131. # ]
  132. def __parse_java_result(filename):
  133. if filename == "":
  134. return
  135. if filename[0] != '/':
  136. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  137. with open(filename) as f:
  138. results = json.loads(f.read())
  139. for result in results:
  140. total_weight = 0
  141. total_value = 0
  142. for measurement in result["measurements"]:
  143. total_weight += measurement["weight"]
  144. total_value += measurement["value"]["magnitude"]
  145. avg_time = total_value * 1.0 / total_weight
  146. total_size, _ = __get_data_size(
  147. result["scenario"]["benchmarkSpec"]["parameters"]["dataFile"])
  148. __results.append({
  149. "language": "java",
  150. "throughput": total_size / avg_time * 1e9 / 2 ** 20,
  151. "behavior": result["scenario"]["benchmarkSpec"]["methodName"],
  152. "dataFilename": __extract_file_name(
  153. result["scenario"]["benchmarkSpec"]["parameters"]["dataFile"])
  154. })
  155. # Go benchmark results:
  156. #
  157. # goos: linux
  158. # goarch: amd64
  159. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Unmarshal-12 3000 705784 ns/op
  160. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Marshal-12 2000 634648 ns/op
  161. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Size-12 5000 244174 ns/op
  162. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Clone-12 300 4120954 ns/op
  163. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Merge-12 300 4108632 ns/op
  164. # PASS
  165. # ok _/usr/local/google/home/yilunchong/mygit/protobuf/benchmarks 124.173s
  166. def __parse_go_result(filename):
  167. if filename == "":
  168. return
  169. if filename[0] != '/':
  170. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  171. with open(filename) as f:
  172. for line in f:
  173. result_list = re.split("[\ \t]+", line)
  174. if result_list[0][:9] != "Benchmark":
  175. continue
  176. first_slash_index = result_list[0].find('/')
  177. last_slash_index = result_list[0].rfind('/')
  178. full_filename = result_list[0][first_slash_index+4:last_slash_index] # delete ../ prefix
  179. total_bytes, _ = __get_data_size(full_filename)
  180. behavior_with_suffix = result_list[0][last_slash_index+1:]
  181. last_dash = behavior_with_suffix.rfind("-")
  182. if last_dash == -1:
  183. behavior = behavior_with_suffix
  184. else:
  185. behavior = behavior_with_suffix[:last_dash]
  186. __results.append({
  187. "dataFilename": __extract_file_name(full_filename),
  188. "throughput": total_bytes / float(result_list[2]) * 1e9 / 2 ** 20,
  189. "behavior": behavior,
  190. "language": "go"
  191. })
  192. def get_result_from_file(cpp_file="", java_file="", python_file="", go_file=""):
  193. results = {}
  194. if cpp_file != "":
  195. __parse_cpp_result(cpp_file)
  196. if java_file != "":
  197. __parse_java_result(java_file)
  198. if python_file != "":
  199. __parse_python_result(python_file)
  200. if go_file != "":
  201. __parse_go_result(go_file)
  202. return __results