Created
April 26, 2023 14:47
-
-
Save esweeney-cg/c685c20bd23c28ebd3240f68e8f1d5a7 to your computer and use it in GitHub Desktop.
create a cpu score for threads from a thread dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import re | |
| from collections import defaultdict | |
| def parse_thread_dump(content): | |
| thread_pattern =\ | |
| r'"(.*?)"\s+#\d+\s+.*?cpu=(.*?)ms\s+elapsed=(.*?)s\s+tid=.*' | |
| threads = re.findall(thread_pattern, content) | |
| summary = defaultdict(lambda: [0, 0, 0, 0]) | |
| for thread_name, cpu_time, elapsed_time in threads: | |
| summary[thread_name][0] += 1 | |
| summary[thread_name][1] += float(cpu_time) | |
| summary[thread_name][2] += float(elapsed_time) | |
| summary[thread_name][3] = (summary[thread_name][1] / | |
| (summary[thread_name][2] * 1000)) * 100 | |
| print("Thread Name,Instances,CPU Time (ms),Elapsed Time \ | |
| (s),Average CPU Usage (%)") | |
| sorted_summary = sorted(summary.items(), | |
| key=lambda x: x[1][3], reverse=True) | |
| for thread_name, (instances, cpu_time, | |
| elapsed_time, avg_cpu_usage) in sorted_summary: | |
| print(f"{thread_name},{instances},{cpu_time:.2f},{elapsed_time:.2f}, \ | |
| {avg_cpu_usage:.2f}") | |
| if __name__ == "__main__": | |
| content = sys.stdin.read() | |
| parse_thread_dump(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment