|
1 | 1 | """ |
2 | 2 | Helper methods for running and documenting processors |
3 | 3 | """ |
| 4 | +from os import environ |
4 | 5 | from time import perf_counter, process_time |
5 | 6 | import json |
6 | 7 | import inspect |
7 | 8 | from subprocess import run, PIPE |
| 9 | +from memory_profiler import memory_usage |
| 10 | +from sparklines import sparklines |
8 | 11 |
|
9 | 12 | from click import wrap_text |
10 | 13 | from ocrd_utils import getLogger |
@@ -85,7 +88,23 @@ def run_processor( |
85 | 88 | log.debug("Processor instance %s (%s doing %s)", processor, name, otherrole) |
86 | 89 | t0_wall = perf_counter() |
87 | 90 | t0_cpu = process_time() |
88 | | - processor.process() |
| 91 | + if any(x in environ.get('OCRD_PROFILE', '') for x in ['RSS', 'PSS']): |
| 92 | + backend = 'psutil_pss' if 'PSS' in environ['OCRD_PROFILE'] else 'psutil' |
| 93 | + mem_usage = memory_usage(proc=processor.process, |
| 94 | + # only run process once |
| 95 | + max_iterations=1, |
| 96 | + interval=.1, timeout=None, timestamps=True, |
| 97 | + # include sub-processes |
| 98 | + multiprocess=True, include_children=True, |
| 99 | + # get proportional set size instead of RSS |
| 100 | + backend=backend) |
| 101 | + mem_usage_values = [mem for mem, _ in mem_usage] |
| 102 | + mem_output = 'memory consumption: ' |
| 103 | + mem_output += ''.join(sparklines(mem_usage_values)) |
| 104 | + mem_output += ' max: %.2f MiB min: %.2f MiB' % (max(mem_usage_values), min(mem_usage_values)) |
| 105 | + logProfile.info(mem_output) |
| 106 | + else: |
| 107 | + processor.process() |
89 | 108 | t1_wall = perf_counter() - t0_wall |
90 | 109 | t1_cpu = process_time() - t0_cpu |
91 | 110 | logProfile.info("Executing processor '%s' took %fs (wall) %fs (CPU)( [--input-file-grp='%s' --output-file-grp='%s' --parameter='%s' --page-id='%s']" % ( |
|
0 commit comments