binary_size 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. # Copyright 2023 The ChromiumOS Authors
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import argparse
  6. import json
  7. import tempfile
  8. import os
  9. import sys
  10. import time
  11. import pathlib
  12. sys.path.append(os.path.dirname(sys.path[0]))
  13. from impl.common import cwd, CROSVM_ROOT, cmd
  14. BOT_MASTERS = "crosvm.ci"
  15. METRIC_NAME = "file_size"
  16. TEST_SUITE_NAME = "crosvm.binary_output"
  17. CHROMEPERF_URL = "https://chromeperf.appspot.com"
  18. # TODO(zihanchen): upload binary size to db for backup
  19. # TODO(zihanchen): Add usage
  20. def main():
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument(
  23. "--base-dir",
  24. help="Base dir of cargo target, used to convert target path to shorter relative path, must "
  25. "be absolute",
  26. )
  27. parser.add_argument(
  28. "--target-name",
  29. help="Target name of binary",
  30. )
  31. parser.add_argument(
  32. "--target-path",
  33. help="Path to binary, must be absolute if base dir is provided",
  34. required=True,
  35. )
  36. parser.add_argument("--upload", action="store_true")
  37. parser.add_argument(
  38. "--catapult-tool-path",
  39. help="Path to catapult dashboard upload tool",
  40. default="/tools/catapult",
  41. )
  42. parser.add_argument(
  43. "--builder-name",
  44. help="Name of builder generating binaries",
  45. )
  46. parser.add_argument("--log-url", help="Url to build execution")
  47. parser.add_argument("--build-version", help="Version of crosvm corresponding to the results")
  48. args = parser.parse_args()
  49. if args.base_dir:
  50. if not os.path.isabs(args.base_dir):
  51. raise AssertionError("Base dir mush be and absolute path")
  52. if not os.path.isabs(args.target_path):
  53. raise AssertionError("When base dir is provided, target path must be an absolute path")
  54. if not os.path.commonpath([args.base_dir, args.target_path]) == os.path.normpath(
  55. args.base_dir
  56. ):
  57. raise AssertionError("Base dir must be a parent directory of target path")
  58. output_path = (
  59. os.path.relpath(args.target_path, start=args.base_dir)
  60. if args.base_dir
  61. else os.path.normpath(args.target_path)
  62. )
  63. # output path can contain duplicated target names, dedup before printing
  64. if len(output_path.split("/")) > 2 and output_path.split("/")[0] == output_path.split("/")[1]:
  65. output_path = "/".join(output_path.split("/")[1:])
  66. file_size = os.path.getsize(args.target_path)
  67. size_dict_for_gerrit = {output_path: file_size}
  68. print(json.dumps(size_dict_for_gerrit))
  69. if not args.upload:
  70. return
  71. fuchiaperf_fd, fuchiaperf_file_path = tempfile.mkstemp(suffix=".fuchisaperf.json", text=True)
  72. fuchiaperf_file = os.fdopen(fuchiaperf_fd, "w")
  73. fuchsiaperf_data = [
  74. {
  75. "test_name": output_path,
  76. "test_suite": TEST_SUITE_NAME,
  77. "unit": "bytes",
  78. "values": [file_size],
  79. "metric": METRIC_NAME,
  80. }
  81. ]
  82. json.dump(fuchsiaperf_data, fuchiaperf_file)
  83. fuchiaperf_file.close()
  84. with open(fuchiaperf_file_path, "r") as f:
  85. print(f.read(), file=sys.stderr)
  86. _, catapult_file_path = tempfile.mkstemp(suffix=".json", text=True)
  87. with cwd(CROSVM_ROOT / "tools" / "impl" / "catapult_converter"):
  88. cmd(
  89. "cargo",
  90. "run",
  91. "--",
  92. "--input",
  93. fuchiaperf_file_path,
  94. "--execution-timestamp-ms",
  95. str(int(time.time() * 1000)),
  96. "--masters",
  97. BOT_MASTERS,
  98. "--bots",
  99. args.builder_name,
  100. "--log-url",
  101. args.log_url,
  102. *(
  103. (
  104. "--product-versions",
  105. args.build_version,
  106. )
  107. if args.build_version
  108. else ()
  109. )
  110. ).write_to(pathlib.Path(catapult_file_path))
  111. # print catapult file to stderr so we retain a copy
  112. with open(catapult_file_path, "r") as f:
  113. print(f.read(), file=sys.stderr)
  114. print(
  115. cmd(
  116. args.catapult_tool_path,
  117. "upload",
  118. '--service-account-json=":gce"',
  119. "--url",
  120. CHROMEPERF_URL,
  121. catapult_file_path,
  122. ).stdout(),
  123. file=sys.stderr,
  124. )
  125. if __name__ == "__main__":
  126. main()