diff options
Diffstat (limited to 'nemesis/nemesis.py')
| -rwxr-xr-x | nemesis/nemesis.py | 118 |
1 files changed, 8 insertions, 110 deletions
diff --git a/nemesis/nemesis.py b/nemesis/nemesis.py index 9d4014c..812cd2d 100755 --- a/nemesis/nemesis.py +++ b/nemesis/nemesis.py @@ -1,13 +1,7 @@ -#!/usr/bin/env python3 ''' - _/ _/ _/ - _/_/ _/ _/_/ _/_/_/ _/_/ _/_/ _/_/_/ _/_/_/ - _/ _/ _/ _/_/_/_/ _/ _/ _/ _/_/_/_/ _/_/ _/ _/_/ - _/ _/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/ - _/ _/ _/_/_/ _/ _/ _/ _/_/_/ _/_/_/ _/ _/_/_/ +Copyright: - -Copyright 2025 bdunahu + Copyright © 2025 bdunahu <bdunahu@operationnull.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -24,22 +18,20 @@ Copyright 2025 bdunahu Commentary: Code: ''' -from typing import Any, Self -from causal_event_loop import CausalEventLoop +from nemesis.causal_event_loop import CausalEventLoop from collections import defaultdict -from html_gen import plot_results -import argparse +from nemesis.html_gen import plot_results +from pathlib import Path +from typing import Any, Self import asyncio -import os import inspect +import os import random import signal import sys import threading import time -import traceback import types -from pathlib import Path CO_COROUTINE = inspect.CO_COROUTINE @@ -114,21 +106,6 @@ class Nemesis(object): Nemesis.signal_interval) @staticmethod - def print_results(): - for coro_name, x_values in Nemesis.results.items(): - print(f'Results for {coro_name:}') - for speedup, experiments in x_values.items(): - print(f' {speedup * 100}% speedup:') - for experiment in experiments: - num_callbacks = len(experiment) - if num_callbacks > 0: - total_wait = sum([cb[1] for cb in experiment]) - latency = total_wait / num_callbacks - print(f' latency: {latency}') - print(f' callbacks processed: {num_callbacks}') - print(f'') - - @staticmethod def stop() -> None: signal.setitimer(signal.ITIMER_REAL, 0) plot_results(Nemesis.results, Nemesis.filename, Nemesis.prog) @@ -144,7 +121,7 @@ class Nemesis(object): loops = Nemesis._get_event_loops() for loop in loops: if not isinstance(loop, CausalEventLoop): - raise RuntimeError(f"Nemesis requires a custom event loop to insert slowdowns. You must start the event loop with `asyncio.run(your_coro(), loop_factory=causal_loop_factory)'.") + raise RuntimeError(f"Nemesis requires a custom event loop to insert slowdowns. You must start the event loop with `asyncio.run(your_coro(), loop_factory=causal_loop_factory)'. Received: {type(loop)}") loop.set_speedup(speedup) Nemesis.experiment_data = Experiment(loops) @@ -313,82 +290,3 @@ class Nemesis(object): # Stop masking Cython bugs, expose them in a friendly way. coro_name = f'<{type(coro).__name__} without __name__>' return f'{coro_name}()' - -the_globals = { - '__name__': '__main__', - '__doc__': None, - '__package__': None, - '__loader__': globals()['__loader__'], - '__spec__': None, - '__annotations__': {}, - '__builtins__': globals()['__builtins__'], - '__file__': None, - '__cached__': None, -} - -def validate_dir(path_str: str) -> Path: - p = Path(path_str).expanduser().resolve() - if not p.exists(): - raise ValueError(f"Profile path does not exist: {p}") - if not p.is_dir(): - raise ValueError(f"Can't profile a non-dir: {p}") - return p - -def validate_html(filename: str) -> str: - if not filename.lower().endswith('.html'): - raise argparse.ArgumentTypeError(f"Did not name an HTML file: '{filename}'") - return filename - -if __name__ == "__main__": - # parses CLI arguments and facilitates profiler runtime. - parser = argparse.ArgumentParser( - usage='%(prog)s [args] -- prog' - ) - - parser.add_argument('-i', '--interval', - help='The minimum amount of time inbetween \ - samples in seconds.', - metavar='', - type=float, - default=0.01) - parser.add_argument('-e', '--experiment-duration', - help='The performance experiment duration. Defaults to 3 seconds.', - metavar='', - type=float, - default=3) - parser.add_argument('-f', '--filename', - help='The filename to write results to. Must name an HTML file.', - metavar='', - type=validate_html, - default="results.html") - parser.add_argument('--include-paths', - help='Specify the path(s) containing files to profile. If a file is in this path, it is a candidate for optimization.', - nargs="+", - type=validate_dir, - required=True) - parser.add_argument('--exclude-paths', - help='Specify the path(s) containing files to exclude profile. Takes priority over --include-paths.', - nargs="*", - type=validate_dir, - required=False, - default=[]) - parser.add_argument('prog', - type=str, - nargs='*', - help='Path to the python script and its arguments.') - args = parser.parse_args() - - sys.argv = args.prog - try: - with open(args.prog[0], 'r', encoding='utf-8') as fp: - code = compile(fp.read(), args.prog[0], "exec") - Nemesis(args.experiment_duration, - args.include_paths, - args.exclude_paths, - args.filename, - args.prog[0], - args.interval).start() - exec(code, the_globals) - Nemesis.stop() - except Exception: - traceback.print_exc() |
