test_backend_sizes.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. """
  3. Test the sizes in the rclone binary of each backend by compiling
  4. rclone with and without the backend and measuring the difference.
  5. Run with no arguments to test all backends or a supply a list of
  6. backends to test.
  7. """
  8. all_backends = "backend/all/all.go"
  9. # compile command which is more or less like the production builds
  10. compile_command = ["go", "build", "--ldflags", "-s", "-trimpath"]
  11. import os
  12. import re
  13. import sys
  14. import subprocess
  15. match_backend = re.compile(r'"github.com/rclone/rclone/backend/(.*?)"')
  16. def read_backends():
  17. """
  18. Reads the backends file, returning a list of backends and the original file
  19. """
  20. with open(all_backends) as fd:
  21. orig_all = fd.read()
  22. # find the backends
  23. backends = []
  24. for line in orig_all.split("\n"):
  25. match = match_backend.search(line)
  26. if match:
  27. backends.append(match.group(1))
  28. return backends, orig_all
  29. def write_all(orig_all, backend):
  30. """
  31. Write the all backends file without the backend given
  32. """
  33. with open(all_backends, "w") as fd:
  34. for line in orig_all.split("\n"):
  35. match = re.search(r'"github.com/rclone/rclone/backend/(.*?)"', line)
  36. # Comment out line matching backend
  37. if match and match.group(1) == backend:
  38. line = "// " + line
  39. fd.write(line+"\n")
  40. def compile():
  41. """
  42. Compile the binary, returning the size
  43. """
  44. subprocess.check_call(compile_command)
  45. return os.stat("rclone").st_size
  46. def main():
  47. # change directory to the one with this script in
  48. os.chdir(os.path.dirname(os.path.abspath(__file__)))
  49. # change directory to the main rclone source
  50. os.chdir("..")
  51. to_test = sys.argv[1:]
  52. backends, orig_all = read_backends()
  53. if len(to_test) == 0:
  54. to_test = backends
  55. # Compile with all backends
  56. ref = compile()
  57. print(f"Total binary size {ref/1024/1024:.3f} MiB")
  58. print("Backend,Size MiB")
  59. for test_backend in to_test:
  60. write_all(orig_all, test_backend)
  61. new_size = compile()
  62. print(f"{test_backend},{(ref-new_size)/1024/1024:.3f}")
  63. # restore all file
  64. with open(all_backends, "w") as fd:
  65. fd.write(orig_all)
  66. if __name__ == "__main__":
  67. main()