Skip to content

Instantly share code, notes, and snippets.

@grischard
Created October 31, 2023 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grischard/76ce50fa9e63aaaebad5522009223683 to your computer and use it in GitHub Desktop.
Save grischard/76ce50fa9e63aaaebad5522009223683 to your computer and use it in GitHub Desktop.
import subprocess
import time
import re
import signal
import psutil
# List of garbage collectors to test
garbage_collectors = [
"-XX:+UseG1GC",
"-XX:+UseParallelGC",
"-XX:+UseShenandoahGC",
"-XX:+UseSerialGC",
]
# Enable or disable string deduplication
string_deduplication = ["-XX:+UseStringDeduplication", "-XX:-UseStringDeduplication"]
# The Java command template
java_cmd_template = "/usr/bin/java {gc_option} {dedup_option} -jar /Users/stereo/Documents/code/josm/app/JOSM.app/Contents/app/josm-custom.jar 39.0628488,-108.5723019,39.0782418,-108.5500717"
# Log message to watch for
log_message_regex = re.compile(
r"Keystroke pressed B is already assigned to org.openstreetmap.josm.plugins.buildings_tools.DrawBuildingAction"
)
# Function to run the Java process and monitor the output
def benchmark_gc(gc_option, dedup_option):
java_cmd = java_cmd_template.format(gc_option=gc_option, dedup_option=dedup_option)
print(java_cmd)
process = subprocess.Popen(
java_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
start_time = time.time()
for line in iter(process.stdout.readline, ""):
match = log_message_regex.search(line)
if match:
end_time = time.time()
ps_process = psutil.Process(process.pid)
memory_info = ps_process.memory_info()
process.send_signal(signal.SIGINT) # Graceful interrupt
return end_time - start_time, memory_info
process.kill()
return None, None
# Main loop that runs the benchmarks
def main():
for gc in garbage_collectors:
for dedup in string_deduplication:
time_taken, memory_info = benchmark_gc(gc, dedup)
if time_taken is not None:
print(
f"Benchmark with {gc} and {dedup}: {time_taken:.2f} seconds, memory Usage: {memory_info.rss / 1024**2:.2f} MB"
)
else:
print(
f"Benchmark with {gc} and {dedup} failed to find the log message."
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment