summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-02-27 16:02:07 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2019-02-27 16:02:07 +0100
commit7d0810ef4fc3e17b59207f9d5b7aabe131f4c6f1 (patch)
tree0742848152f559ddbbb29d541071a3bef362b47e
parent25beb1467448e68cd88803f8289e20c8423d760e (diff)
downloadpsutil-7d0810ef4fc3e17b59207f9d5b7aabe131f4c6f1.tar.gz
highlight top 6 slowest calls
-rw-r--r--scripts/internal/print_access_denied.py4
-rw-r--r--scripts/internal/print_api_speed.py23
2 files changed, 21 insertions, 6 deletions
diff --git a/scripts/internal/print_access_denied.py b/scripts/internal/print_access_denied.py
index 7e46f48e..d903f8b9 100644
--- a/scripts/internal/print_access_denied.py
+++ b/scripts/internal/print_access_denied.py
@@ -40,7 +40,7 @@ cwd 237 70.5% ACCESS DENIED
io_counters 237 70.5% ACCESS DENIED
open_files 237 70.5% ACCESS DENIED
environ 237 70.5% ACCESS DENIED
---------------------------
+-----------------------------------------------
total: 1736 (336 total processes)
"""
@@ -75,7 +75,7 @@ def main():
s += "ACCESS DENIED"
s = hilite(s, ok=False)
print(s)
- print("--------------------------")
+ print("-----------------------------------------------")
print("total: %19s (%s total processes)" % (tot_ads, tot_procs))
diff --git a/scripts/internal/print_api_speed.py b/scripts/internal/print_api_speed.py
index b88ab3c5..2266caa2 100644
--- a/scripts/internal/print_api_speed.py
+++ b/scripts/internal/print_api_speed.py
@@ -30,17 +30,25 @@ import inspect
import os
import psutil
+from scriptutils import hilite
SORT_BY_TIME = False
+TOP_SLOWEST = 7
timings = []
def print_timings():
+ slower = []
timings.sort(key=lambda x: x[1 if SORT_BY_TIME else 0])
+ for x in sorted(timings, key=lambda x: x[1], reverse=1)[:TOP_SLOWEST]:
+ slower.append(x[0])
while timings[:]:
title, elapsed = timings.pop(0)
- print("%-30s %f secs" % (title, elapsed))
+ s = " %-30s %f secs" % (title, elapsed)
+ if title in slower:
+ s = hilite(s, ok=False)
+ print(s)
def timecall(title, fun, *args, **kw):
@@ -50,6 +58,10 @@ def timecall(title, fun, *args, **kw):
timings.append((title, elapsed))
+def titlestr(s):
+ return hilite(s, ok=None, bold=True)
+
+
def run():
# --- system
@@ -60,7 +72,7 @@ def run():
if name not in ('wait_procs', 'process_iter'):
public_apis.append(name)
- print("SYSTEM APIS")
+ print(titlestr("SYSTEM APIS"))
for name in public_apis:
fun = getattr(psutil, name)
args = ()
@@ -75,7 +87,7 @@ def run():
# --- process
- print("\nPROCESS APIS")
+ print(titlestr("\nPROCESS APIS"))
ignore = ['send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait',
'as_dict', 'parent', 'parents', 'memory_info_ex', 'oneshot',
'pid', 'rlimit']
@@ -88,12 +100,15 @@ def run():
def main():
- global SORT_BY_TIME
+ global SORT_BY_TIME, TOP_SLOWEST
parser = argparse.ArgumentParser(description='Benchmark all API calls')
parser.add_argument('-t', '--time', required=False, default=False,
action='store_true', help="sort by timings")
+ parser.add_argument('-s', '--slowest', required=False, default=TOP_SLOWEST,
+ help="highlight the top N slowest APIs")
args = parser.parse_args()
SORT_BY_TIME = bool(args.time)
+ TOP_SLOWEST = int(args.slowest)
run()