Guest User

Untitled

a guest
Jan 9th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import itertools
  4. import os
  5. import pathlib
  6. import re
  7. import shlex
  8. import subprocess
  9. import time
  10.  
  11. file_ver1 = "./Brave-stable-v1.73.104-x86_64.AppImage"
  12. file_ver2 = "./Brave-stable-v1.73.105-x86_64.AppImage"
  13.  
  14. block_sizes = ["128k", "512k", "1M"]
  15. comps_squashfs = {
  16. "gzip": {"-Xcompression-level": ["1", "6", "9"]},
  17. "lz4": {},
  18. "xz": {
  19. "-Xdict-size": ["100%"],
  20. },
  21. "zstd": {
  22. "-Xcompression-level": ["1", "15", "22"],
  23. },
  24. }
  25.  
  26. output = open("results.csv", "w")
  27. output.write(
  28. "Compression Method,Compression Parameters,Block Size,Compression Time,"
  29. "FS Size,Startup Time (average of 5 runs),zsync download size"
  30. )
  31. output.flush()
  32.  
  33. try:
  34. os.mkdir("zsyncout")
  35. except FileExistsError:
  36. pass
  37.  
  38. if not os.path.exists("ver1.AppDir"):
  39. subprocess.Popen([file_ver1, "--appimage-extract"]).wait()
  40. os.rename("squashfs-root", "ver1.AppDir")
  41. if not os.path.exists("ver2.AppDir"):
  42. subprocess.Popen([file_ver2, "--appimage-extract"]).wait()
  43. os.rename("squashfs-root", "ver2.AppDir")
  44.  
  45. with open("uruntime-appimage-x86_64", "rb") as f:
  46. runtime = f.read()
  47.  
  48. for comp in comps_squashfs:
  49. # product all possible combinations of compression parameters
  50. comp_params = list(
  51. itertools.product(*list(list([i, j] for j in comps_squashfs[comp][i]) for i in comps_squashfs[comp]))
  52. )
  53. for comp_param in comp_params:
  54. for block_size in block_sizes:
  55. output.write("\n")
  56. output.write(f"SquashFS {comp},{shlex.join(sum(comp_param, []))},{block_size},")
  57. output.flush()
  58.  
  59. print(
  60. "Preparing AppImages for compression method %s with parameters %s and block size %s"
  61. % (comp, sum(comp_param, []), block_size)
  62. )
  63. # Prepare the AppImages
  64. command = ["mksquashfs", "ver1.AppDir", "file_ver1.squashfs", "-root-owned", "-noappend", "-comp", comp]
  65. command.extend(["-b", block_size])
  66. for i in comp_param:
  67. command.extend(i)
  68. # file_ver1 is only the base file, so do not measure efficiency
  69. p = subprocess.Popen(command)
  70. if p.wait() != 0:
  71. print("Error in compression")
  72. continue
  73. with open("file_ver1.squashfs", "rb") as f:
  74. squashfs = f.read()
  75. os.remove("file_ver1.squashfs")
  76. with open("file_ver1.AppImage", "wb") as f:
  77. f.write(runtime)
  78. f.write(squashfs)
  79. del squashfs
  80. try:
  81. os.chmod("file_ver1.AppImage", 0o755)
  82. except PermissionError:
  83. pass
  84.  
  85. # Compression time
  86. command[1] = "ver2.AppDir"
  87. command[2] = "file_ver2.squashfs"
  88. start = time.time()
  89. p = subprocess.Popen(command)
  90. if p.wait() != 0:
  91. print("Error in compression")
  92. continue
  93. output.write(f"{(time.time() - start) * 1000:.1f}ms,")
  94. output.flush()
  95. with open("file_ver2.squashfs", "rb") as f:
  96. squashfs = f.read()
  97. os.remove("file_ver2.squashfs")
  98. with open("file_ver2.AppImage", "wb") as f:
  99. f.write(runtime)
  100. f.write(squashfs)
  101. output.write(f"{len(squashfs)},")
  102. output.flush()
  103. print("SquashFS size: %d" % len(squashfs))
  104. del squashfs
  105. try:
  106. os.chmod("file_ver2.AppImage", 0o755)
  107. except PermissionError:
  108. pass
  109.  
  110. # Startup time
  111. print("Measuring startup time, 10 rounds, only last 5 rounds count")
  112. for _ in range(2):
  113. startup_times = []
  114. for i in range(5):
  115. start = time.time()
  116. p = subprocess.Popen(["./file_ver2.AppImage"])
  117. p.wait()
  118. used_time = time.time() - start
  119. startup_times.append(used_time)
  120. print("Startup time round %d: %f" % (i + 1, used_time))
  121. output.write(f"{sum(startup_times) * 1000 / 5:.1f} ms,")
  122. output.flush()
  123.  
  124. # zsync download size
  125. print("Creating zsync file")
  126. p = subprocess.Popen(["zsyncmake", "file_ver2.AppImage"])
  127. if p.wait() != 0:
  128. print("Error in zsync")
  129. continue
  130. print("Calculating zsync download size")
  131. p = subprocess.Popen(
  132. ["zsync", "-i", "file_ver1.AppImage", "file_ver2.AppImage.zsync", "-o", "./zsyncout"],
  133. stderr=subprocess.PIPE,
  134. )
  135. zsync_output = p.stderr.read().decode("utf-8")
  136. percentage = re.search(r"(\d+\.\d+)%", zsync_output).group(1)
  137. p.wait()
  138. try:
  139. os.remove("zsyncout.part")
  140. except FileNotFoundError:
  141. pass
  142. output.write(f"{(1 - float(percentage) / 100) * os.path.getsize('file_ver2.AppImage'):.0f}")
  143. output.flush()
  144.  
  145. output.close()
Advertisement
Add Comment
Please, Sign In to add comment