bm_json.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python3
  2. # Copyright 2017 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Utilities for manipulating JSON data that represents microbenchmark results.
  16. import os
  17. # template arguments and dynamic arguments of individual benchmark types
  18. # Example benchmark name: "BM_UnaryPingPong<TCP, NoOpMutator, NoOpMutator>/0/0"
  19. _BM_SPECS = {
  20. 'BM_UnaryPingPong': {
  21. 'tpl': ['fixture', 'client_mutator', 'server_mutator'],
  22. 'dyn': ['request_size', 'response_size'],
  23. },
  24. 'BM_PumpStreamClientToServer': {
  25. 'tpl': ['fixture'],
  26. 'dyn': ['request_size'],
  27. },
  28. 'BM_PumpStreamServerToClient': {
  29. 'tpl': ['fixture'],
  30. 'dyn': ['request_size'],
  31. },
  32. 'BM_StreamingPingPong': {
  33. 'tpl': ['fixture', 'client_mutator', 'server_mutator'],
  34. 'dyn': ['request_size', 'request_count'],
  35. },
  36. 'BM_StreamingPingPongMsgs': {
  37. 'tpl': ['fixture', 'client_mutator', 'server_mutator'],
  38. 'dyn': ['request_size'],
  39. },
  40. 'BM_PumpStreamServerToClient_Trickle': {
  41. 'tpl': [],
  42. 'dyn': ['request_size', 'bandwidth_kilobits'],
  43. },
  44. 'BM_PumpUnbalancedUnary_Trickle': {
  45. 'tpl': [],
  46. 'dyn': ['cli_req_size', 'svr_req_size', 'bandwidth_kilobits'],
  47. },
  48. 'BM_ErrorStringOnNewError': {
  49. 'tpl': ['fixture'],
  50. 'dyn': [],
  51. },
  52. 'BM_ErrorStringRepeatedly': {
  53. 'tpl': ['fixture'],
  54. 'dyn': [],
  55. },
  56. 'BM_ErrorGetStatus': {
  57. 'tpl': ['fixture'],
  58. 'dyn': [],
  59. },
  60. 'BM_ErrorGetStatusCode': {
  61. 'tpl': ['fixture'],
  62. 'dyn': [],
  63. },
  64. 'BM_ErrorHttpError': {
  65. 'tpl': ['fixture'],
  66. 'dyn': [],
  67. },
  68. 'BM_HasClearGrpcStatus': {
  69. 'tpl': ['fixture'],
  70. 'dyn': [],
  71. },
  72. 'BM_IsolatedFilter': {
  73. 'tpl': ['fixture', 'client_mutator'],
  74. 'dyn': [],
  75. },
  76. 'BM_HpackEncoderEncodeHeader': {
  77. 'tpl': ['fixture'],
  78. 'dyn': ['end_of_stream', 'request_size'],
  79. },
  80. 'BM_HpackParserParseHeader': {
  81. 'tpl': ['fixture'],
  82. 'dyn': [],
  83. },
  84. 'BM_CallCreateDestroy': {
  85. 'tpl': ['fixture'],
  86. 'dyn': [],
  87. },
  88. 'BM_Zalloc': {
  89. 'tpl': [],
  90. 'dyn': ['request_size'],
  91. },
  92. 'BM_PollEmptyPollset_SpeedOfLight': {
  93. 'tpl': [],
  94. 'dyn': ['request_size', 'request_count'],
  95. },
  96. 'BM_StreamCreateSendInitialMetadataDestroy': {
  97. 'tpl': ['fixture'],
  98. 'dyn': [],
  99. },
  100. 'BM_TransportStreamSend': {
  101. 'tpl': [],
  102. 'dyn': ['request_size'],
  103. },
  104. 'BM_TransportStreamRecv': {
  105. 'tpl': [],
  106. 'dyn': ['request_size'],
  107. },
  108. 'BM_StreamingPingPongWithCoalescingApi': {
  109. 'tpl': ['fixture', 'client_mutator', 'server_mutator'],
  110. 'dyn': ['request_size', 'request_count', 'end_of_stream'],
  111. },
  112. 'BM_Base16SomeStuff': {
  113. 'tpl': [],
  114. 'dyn': ['request_size'],
  115. }
  116. }
  117. def numericalize(s):
  118. """Convert abbreviations like '100M' or '10k' to a number."""
  119. if not s:
  120. return ''
  121. if s[-1] == 'k':
  122. return float(s[:-1]) * 1024
  123. if s[-1] == 'M':
  124. return float(s[:-1]) * 1024 * 1024
  125. if 0 <= (ord(s[-1]) - ord('0')) <= 9:
  126. return float(s)
  127. assert 'not a number: %s' % s
  128. def parse_name(name):
  129. cpp_name = name
  130. if '<' not in name and '/' not in name and name not in _BM_SPECS:
  131. return {'name': name, 'cpp_name': name}
  132. rest = name
  133. out = {}
  134. tpl_args = []
  135. dyn_args = []
  136. if '<' in rest:
  137. tpl_bit = rest[rest.find('<') + 1:rest.rfind('>')]
  138. arg = ''
  139. nesting = 0
  140. for c in tpl_bit:
  141. if c == '<':
  142. nesting += 1
  143. arg += c
  144. elif c == '>':
  145. nesting -= 1
  146. arg += c
  147. elif c == ',':
  148. if nesting == 0:
  149. tpl_args.append(arg.strip())
  150. arg = ''
  151. else:
  152. arg += c
  153. else:
  154. arg += c
  155. tpl_args.append(arg.strip())
  156. rest = rest[:rest.find('<')] + rest[rest.rfind('>') + 1:]
  157. if '/' in rest:
  158. s = rest.split('/')
  159. rest = s[0]
  160. dyn_args = s[1:]
  161. name = rest
  162. assert name in _BM_SPECS, '_BM_SPECS needs to be expanded for %s' % name
  163. assert len(dyn_args) == len(_BM_SPECS[name]['dyn'])
  164. assert len(tpl_args) == len(_BM_SPECS[name]['tpl'])
  165. out['name'] = name
  166. out['cpp_name'] = cpp_name
  167. out.update(
  168. dict((k, numericalize(v))
  169. for k, v in zip(_BM_SPECS[name]['dyn'], dyn_args)))
  170. out.update(dict(zip(_BM_SPECS[name]['tpl'], tpl_args)))
  171. return out
  172. def expand_json(js, js2=None):
  173. if not js and not js2:
  174. raise StopIteration()
  175. if not js:
  176. js = js2
  177. for bm in js['benchmarks']:
  178. if bm['name'].endswith('_stddev') or bm['name'].endswith('_mean'):
  179. continue
  180. context = js['context']
  181. if 'label' in bm:
  182. labels_list = [
  183. s.split(':')
  184. for s in bm['label'].strip().split(' ')
  185. if len(s) and s[0] != '#'
  186. ]
  187. for el in labels_list:
  188. el[0] = el[0].replace('/iter', '_per_iteration')
  189. labels = dict(labels_list)
  190. else:
  191. labels = {}
  192. # TODO(jtattermusch): grabbing kokoro env values shouldn't be buried
  193. # deep in the JSON conversion logic.
  194. # Link the data to a kokoro job run by adding
  195. # well known kokoro env variables as metadata for each row
  196. row = {
  197. 'jenkins_build': os.environ.get('KOKORO_BUILD_NUMBER', ''),
  198. 'jenkins_job': os.environ.get('KOKORO_JOB_NAME', ''),
  199. }
  200. row.update(context)
  201. row.update(bm)
  202. row.update(parse_name(row['name']))
  203. row.update(labels)
  204. # TODO(jtattermusch): add a comment explaining what's the point
  205. # of merging values of some of the columns js2 into the row.
  206. # Empirically, the js contains data from "counters" config
  207. # and js2 contains data from the "opt" config, but the point of merging
  208. # really deserves further explanation.
  209. if js2:
  210. for bm2 in js2['benchmarks']:
  211. if bm['name'] == bm2['name'] and 'already_used' not in bm2:
  212. row['cpu_time'] = bm2['cpu_time']
  213. row['real_time'] = bm2['real_time']
  214. row['iterations'] = bm2['iterations']
  215. bm2['already_used'] = True
  216. break
  217. yield row