result_parser.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_ns": double,
  41. # "iterations": int,
  42. # "name: string,
  43. # "real_time_ns: double,
  44. # ...
  45. # },
  46. # ...
  47. # ],
  48. # ...
  49. # ]
  50. def __parse_cpp_result(filename):
  51. if filename == "":
  52. return
  53. if filename[0] != '/':
  54. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  55. with open(filename) as f:
  56. results = json.loads(f.read())
  57. for benchmark in results["benchmarks"]:
  58. data_filename = "".join(
  59. re.split("(_parse_|_serialize)", benchmark["name"])[0])
  60. behavior = benchmark["name"][len(data_filename) + 1:]
  61. if data_filename[:2] == "BM":
  62. data_filename = data_filename[3:]
  63. __results.append({
  64. "language": "cpp",
  65. "dataFilename": data_filename,
  66. "behavior": behavior,
  67. "throughput": benchmark["bytes_per_second"] / 2.0 ** 20
  68. })
  69. # Synthetic benchmark results example:
  70. # [
  71. # "benchmarks": [
  72. # {
  73. # "cpu_time_ns": double,
  74. # "iterations": int,
  75. # "name: string,
  76. # "real_time_ns: double,
  77. # ...
  78. # },
  79. # ...
  80. # ],
  81. # ...
  82. # ]
  83. def __parse_synthetic_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 = json.loads(f.read())
  90. for benchmark in results["benchmarks"]:
  91. __results.append({
  92. "language": "cpp",
  93. "dataFilename": "",
  94. "behavior": "synthetic",
  95. "throughput": 10.0**9 / benchmark["cpu_time_ns"]
  96. })
  97. # Python results example:
  98. # [
  99. # [
  100. # {
  101. # "filename": string,
  102. # "benchmarks": {
  103. # behavior: results,
  104. # ...
  105. # },
  106. # "message_name": STRING
  107. # },
  108. # ...
  109. # ], #pure-python
  110. # ...
  111. # ]
  112. def __parse_python_result(filename):
  113. if filename == "":
  114. return
  115. if filename[0] != '/':
  116. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  117. with open(filename) as f:
  118. results_list = json.loads(f.read())
  119. for results in results_list:
  120. for result in results:
  121. _, avg_size = __get_data_size(result["filename"])
  122. for behavior in result["benchmarks"]:
  123. __results.append({
  124. "language": "python",
  125. "dataFilename": __extract_file_name(result["filename"]),
  126. "behavior": behavior,
  127. "throughput": avg_size /
  128. result["benchmarks"][behavior] * 1e9 / 2 ** 20
  129. })
  130. # Java results example:
  131. # [
  132. # {
  133. # "id": string,
  134. # "instrumentSpec": {...},
  135. # "measurements": [
  136. # {
  137. # "weight": float,
  138. # "value": {
  139. # "magnitude": float,
  140. # "unit": string
  141. # },
  142. # ...
  143. # },
  144. # ...
  145. # ],
  146. # "run": {...},
  147. # "scenario": {
  148. # "benchmarkSpec": {
  149. # "methodName": string,
  150. # "parameters": {
  151. # defined parameters in the benchmark: parameters value
  152. # },
  153. # ...
  154. # },
  155. # ...
  156. # }
  157. #
  158. # },
  159. # ...
  160. # ]
  161. def __parse_java_result(filename):
  162. if filename == "":
  163. return
  164. if filename[0] != '/':
  165. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  166. with open(filename) as f:
  167. results = json.loads(f.read())
  168. for result in results:
  169. total_weight = 0
  170. total_value = 0
  171. for measurement in result["measurements"]:
  172. total_weight += measurement["weight"]
  173. total_value += measurement["value"]["magnitude"]
  174. avg_time = total_value * 1.0 / total_weight
  175. total_size, _ = __get_data_size(
  176. result["scenario"]["benchmarkSpec"]["parameters"]["dataFile"])
  177. __results.append({
  178. "language": "java",
  179. "throughput": total_size / avg_time * 1e9 / 2 ** 20,
  180. "behavior": result["scenario"]["benchmarkSpec"]["methodName"],
  181. "dataFilename": __extract_file_name(
  182. result["scenario"]["benchmarkSpec"]["parameters"]["dataFile"])
  183. })
  184. # Go benchmark results:
  185. #
  186. # goos: linux
  187. # goarch: amd64
  188. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Unmarshal-12 3000 705784 ns/op
  189. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Marshal-12 2000 634648 ns/op
  190. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Size-12 5000 244174 ns/op
  191. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Clone-12 300 4120954 ns/op
  192. # Benchmark/.././datasets/google_message2/dataset.google_message2.pb/Merge-12 300 4108632 ns/op
  193. # PASS
  194. # ok _/usr/local/google/home/yilunchong/mygit/protobuf/benchmarks 124.173s
  195. def __parse_go_result(filename):
  196. if filename == "":
  197. return
  198. if filename[0] != '/':
  199. filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename
  200. with open(filename) as f:
  201. for line in f:
  202. result_list = re.split("[\ \t]+", line)
  203. if result_list[0][:9] != "Benchmark":
  204. continue
  205. first_slash_index = result_list[0].find('/')
  206. last_slash_index = result_list[0].rfind('/')
  207. full_filename = result_list[0][first_slash_index+4:last_slash_index] # delete ../ prefix
  208. total_bytes, _ = __get_data_size(full_filename)
  209. behavior_with_suffix = result_list[0][last_slash_index+1:]
  210. last_dash = behavior_with_suffix.rfind("-")
  211. if last_dash == -1:
  212. behavior = behavior_with_suffix
  213. else:
  214. behavior = behavior_with_suffix[:last_dash]
  215. __results.append({
  216. "dataFilename": __extract_file_name(full_filename),
  217. "throughput": total_bytes / float(result_list[2]) * 1e9 / 2 ** 20,
  218. "behavior": behavior,
  219. "language": "go"
  220. })
  221. def get_result_from_file(cpp_file="",
  222. java_file="",
  223. python_file="",
  224. go_file="",
  225. synthetic_file=""):
  226. results = {}
  227. if cpp_file != "":
  228. __parse_cpp_result(cpp_file)
  229. if java_file != "":
  230. __parse_java_result(java_file)
  231. if python_file != "":
  232. __parse_python_result(python_file)
  233. if go_file != "":
  234. __parse_go_result(go_file)
  235. if synthetic_file != "":
  236. __parse_synthetic_result(synthetic_file)
  237. return __results