1ffe3c632Sopenharmony_cifrom __future__ import print_function
2ffe3c632Sopenharmony_cifrom __future__ import absolute_import
3ffe3c632Sopenharmony_ciimport argparse
4ffe3c632Sopenharmony_ciimport os
5ffe3c632Sopenharmony_ciimport re
6ffe3c632Sopenharmony_ciimport copy
7ffe3c632Sopenharmony_ciimport uuid
8ffe3c632Sopenharmony_ciimport calendar
9ffe3c632Sopenharmony_ciimport time
10ffe3c632Sopenharmony_ciimport datetime
11ffe3c632Sopenharmony_ci
12ffe3c632Sopenharmony_cifrom util import big_query_utils
13ffe3c632Sopenharmony_cifrom util import result_parser
14ffe3c632Sopenharmony_ci
15ffe3c632Sopenharmony_ci_PROJECT_ID = 'grpc-testing'
16ffe3c632Sopenharmony_ci_DATASET = 'protobuf_benchmark_result'
17ffe3c632Sopenharmony_ci_TABLE = 'opensource_result_v2'
18ffe3c632Sopenharmony_ci_NOW = "%d%02d%02d" % (datetime.datetime.now().year,
19ffe3c632Sopenharmony_ci                       datetime.datetime.now().month,
20ffe3c632Sopenharmony_ci                       datetime.datetime.now().day)
21ffe3c632Sopenharmony_ci
22ffe3c632Sopenharmony_ci_INITIAL_TIME = calendar.timegm(time.gmtime())
23ffe3c632Sopenharmony_ci
24ffe3c632Sopenharmony_cidef get_metadata():
25ffe3c632Sopenharmony_ci  build_number = os.getenv('BUILD_NUMBER')
26ffe3c632Sopenharmony_ci  build_url = os.getenv('BUILD_URL')
27ffe3c632Sopenharmony_ci  job_name = os.getenv('JOB_NAME')
28ffe3c632Sopenharmony_ci  git_commit = os.getenv('GIT_COMMIT')
29ffe3c632Sopenharmony_ci  # actual commit is the actual head of PR that is getting tested
30ffe3c632Sopenharmony_ci  git_actual_commit = os.getenv('ghprbActualCommit')
31ffe3c632Sopenharmony_ci
32ffe3c632Sopenharmony_ci  utc_timestamp = str(calendar.timegm(time.gmtime()))
33ffe3c632Sopenharmony_ci  metadata = {'created': utc_timestamp}
34ffe3c632Sopenharmony_ci
35ffe3c632Sopenharmony_ci  if build_number:
36ffe3c632Sopenharmony_ci    metadata['buildNumber'] = build_number
37ffe3c632Sopenharmony_ci  if build_url:
38ffe3c632Sopenharmony_ci    metadata['buildUrl'] = build_url
39ffe3c632Sopenharmony_ci  if job_name:
40ffe3c632Sopenharmony_ci    metadata['jobName'] = job_name
41ffe3c632Sopenharmony_ci  if git_commit:
42ffe3c632Sopenharmony_ci    metadata['gitCommit'] = git_commit
43ffe3c632Sopenharmony_ci  if git_actual_commit:
44ffe3c632Sopenharmony_ci    metadata['gitActualCommit'] = git_actual_commit
45ffe3c632Sopenharmony_ci
46ffe3c632Sopenharmony_ci  return metadata
47ffe3c632Sopenharmony_ci
48ffe3c632Sopenharmony_ci
49ffe3c632Sopenharmony_cidef upload_result(result_list, metadata):
50ffe3c632Sopenharmony_ci  for result in result_list:
51ffe3c632Sopenharmony_ci    new_result = {}
52ffe3c632Sopenharmony_ci    new_result["metric"] = "throughput"
53ffe3c632Sopenharmony_ci    new_result["value"] = result["throughput"]
54ffe3c632Sopenharmony_ci    new_result["unit"] = "MB/s"
55ffe3c632Sopenharmony_ci    new_result["test"] = "protobuf_benchmark"
56ffe3c632Sopenharmony_ci    new_result["product_name"] = "protobuf"
57ffe3c632Sopenharmony_ci    labels_string = ""
58ffe3c632Sopenharmony_ci    for key in result:
59ffe3c632Sopenharmony_ci      labels_string += ",|%s:%s|" % (key, result[key])
60ffe3c632Sopenharmony_ci    new_result["labels"] = labels_string[1:]
61ffe3c632Sopenharmony_ci    new_result["timestamp"] = _INITIAL_TIME
62ffe3c632Sopenharmony_ci    print(labels_string)
63ffe3c632Sopenharmony_ci
64ffe3c632Sopenharmony_ci    bq = big_query_utils.create_big_query()
65ffe3c632Sopenharmony_ci    row = big_query_utils.make_row(str(uuid.uuid4()), new_result)
66ffe3c632Sopenharmony_ci    if not big_query_utils.insert_rows(bq, _PROJECT_ID, _DATASET,
67ffe3c632Sopenharmony_ci                                        _TABLE + "$" + _NOW,
68ffe3c632Sopenharmony_ci                                        [row]):
69ffe3c632Sopenharmony_ci      print('Error when uploading result', new_result)
70ffe3c632Sopenharmony_ci
71ffe3c632Sopenharmony_ci
72ffe3c632Sopenharmony_ciif __name__ == "__main__":
73ffe3c632Sopenharmony_ci  parser = argparse.ArgumentParser()
74ffe3c632Sopenharmony_ci  parser.add_argument("-cpp", "--cpp_input_file",
75ffe3c632Sopenharmony_ci                      help="The CPP benchmark result file's name",
76ffe3c632Sopenharmony_ci                      default="")
77ffe3c632Sopenharmony_ci  parser.add_argument("-java", "--java_input_file",
78ffe3c632Sopenharmony_ci                      help="The Java benchmark result file's name",
79ffe3c632Sopenharmony_ci                      default="")
80ffe3c632Sopenharmony_ci  parser.add_argument("-python", "--python_input_file",
81ffe3c632Sopenharmony_ci                      help="The Python benchmark result file's name",
82ffe3c632Sopenharmony_ci                      default="")
83ffe3c632Sopenharmony_ci  parser.add_argument("-go", "--go_input_file",
84ffe3c632Sopenharmony_ci                      help="The golang benchmark result file's name",
85ffe3c632Sopenharmony_ci                      default="")
86ffe3c632Sopenharmony_ci  parser.add_argument("-node", "--node_input_file",
87ffe3c632Sopenharmony_ci                      help="The node.js benchmark result file's name",
88ffe3c632Sopenharmony_ci                      default="")
89ffe3c632Sopenharmony_ci  parser.add_argument("-php", "--php_input_file",
90ffe3c632Sopenharmony_ci                      help="The pure php benchmark result file's name",
91ffe3c632Sopenharmony_ci                      default="")
92ffe3c632Sopenharmony_ci  parser.add_argument("-php_c", "--php_c_input_file",
93ffe3c632Sopenharmony_ci                      help="The php with c ext benchmark result file's name",
94ffe3c632Sopenharmony_ci                      default="")
95ffe3c632Sopenharmony_ci  args = parser.parse_args()
96ffe3c632Sopenharmony_ci
97ffe3c632Sopenharmony_ci  metadata = get_metadata()
98ffe3c632Sopenharmony_ci  print("uploading results...")
99ffe3c632Sopenharmony_ci  upload_result(result_parser.get_result_from_file(
100ffe3c632Sopenharmony_ci      cpp_file=args.cpp_input_file,
101ffe3c632Sopenharmony_ci      java_file=args.java_input_file,
102ffe3c632Sopenharmony_ci      python_file=args.python_input_file,
103ffe3c632Sopenharmony_ci      go_file=args.go_input_file,
104ffe3c632Sopenharmony_ci      node_file=args.node_input_file,
105ffe3c632Sopenharmony_ci      php_file=args.php_input_file,
106ffe3c632Sopenharmony_ci      php_c_file=args.php_c_input_file,
107ffe3c632Sopenharmony_ci  ), metadata)
108